Module 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 107

Module 2

INTERPRETING THE SIGNIFICANCE OF THE LS –L OPTION


Basic File Attributes

• ls looks up files inode number to fetch the


attributes
• ls –l command lists the seven attributes of all
files in current directory
• $ls -l
Total 72 // totally 72 blocks occupied on disk
- rw-r--r-- 1 A B 500 march 6 2010 2.30 chap01
-rwxrw-r-x 2 A C 1000 may 2 2011 5.00 chap02
• d rw-r--r-- 1 A D 1500 april 12 2012 12.30 XYZ
• File – Type and Permissions : FIRST Column
• It indicates the type and permissions associated with
the each file .
• The first character represents the “ file type “
‘ – ‘ represents the ordinary file
‘ d ‘represents the directory file
‘ a / b/ c ‘represents the device file

Remaining character in first column represents the


read , write and execute permission to the
owner(USER) , group and others .
• LINKS : SECOND COLUMN
• This indicates the number of links associated with
a file.
• This is actually the number of filenames
maintained by the system for the single copy of a
file on disk .
• OWNERSHIP : THIRD column
when we create the file , automatically we are the
owner of this file.
Group ownership : 4th column represents the group
owner of the file .
File Size : The amount of data it contains ( i.e the
total number of characters it has stored in it ).
• Last Modification Time : 6th 7th 8th & 9th shows
the last modification time . A file is said to be
modified only if its content get changed , if we
change the ownership or permission the
modification time will remain unchanged .

• File name : last column indicates the filenames


arranged in ASCII collating sequence .
File permissions
• UNIX has a simple and well-defined system of
assigning permissions to files.
• E.g :
• - rw-r--r-- 1 A D 1500 april 12 2012 12.30 chap01
• permissions
• Here we have 3 groups(category).
• Each group is of 3 slots ( read, write , and execute in
that order )
• r –read, w-write , x-execute and ‘ – ‘ for absence of
corresponding permission
Changing the file permissions
• $umask : to know default settings of
permissions of a file / directory.

• The user can change the permission of any


file/dir by using chmod command.

• Change mode (chmod) command is used to


set the permissions of one or more files for all
3 categories of users. (User, Group, Others)
Chmod command can be used in 2 ways

1) In a relative manner by specifying the


changes to the current permissions.

2)In an absolute manner by specifying the final


permission.
RELATIVE PERMISSIONS
• In this mode of permission , chmod only changes
the permissions specified in command line and
leaves the other permission unchanged.

Chmod category operation permission filename(s)

Category ( user (u), group (g) , others (o) , all (a))


Operation ( assign(+) or remove (-) a permission)
Permission ( read (r), write (w) & execute (x) )
• E.g
• Before changing the permission
• ls –l chap01
• -rw-r--r-- 1 A B 100 may 10 2012 10.30 chap01

• After executing the chmod command


• $chmod u+x chap01
• $ls –l chap01
• -rwxr--r-- 1 A B 100 may 10 2012 10.30 chap01
• Only user permission got changed but the others are
remain unchanged
• ls –l chap01
• -rw-r--r-- 1 A B 100 may 10 2012 10.30
chap01

• $chmod ugo+x chap01


• ls –l chap01
• -rwxr-xr-x 1 A B 100 may 10 2012 10.30
chap01
• ls –l chap01
• -rw-r--r-- 1 A B 100 may 10 2012 10.30
chap01

• $chmod go+w chap01


• ls –l chap01
• -rwx--x--x 1 A B 100 may 10 2012 10.30
chap01
• ls –l chap01
• -rw-r--r-- 1 A B 100 may 10 2012 10.30
chap01

• $chmod go+wx , u+x chap01


• ls –l chap01
• -rwxrwxrwx 1 A B 100 may 10 2012 10.30
chap01
Absolute permission
• If we want to set all 9 bit permission without
considering the files current permission ,then we
have to make use of Absolute permission method
in chmod command.

• The expression used by chmod here is a string of


3 octal numbers.

• Set of 3 bits represents one octal number.

• Represent the permission of each category by


one octal digit
• In octal form Each permission is represents as follows
– Read permission ---- 4
– Write permission ---- 2
– Execute permission ---- 1

– For E.g :
Owner Group Others
[ R W E] [ R W E] [ R W E]
Binary 1 1 1 1 - 1 - - 1
Octal 7 5
1
• E.g
• before applying chmod
• Ls –I chap01
• -r w - - - - - - - 1 A B 100 12 may 2012 10.30 chap01

• Applying chmod in Absolute manner

• $chmod 7 5 1 chap01 //changing permission


• $ls –l chap01
• -r w x r - x - - x 1 A B 100 12 may 2012 10.30 chap01
Using chmod recursively (-R)
• Its possible for chmod command descend a directory
hierarchy and apply the expression to every file and
subdirectory it finds. This is done with the –R
(recursive ) option.
• For e.g.
• $chmod –R a+x XYZ //XYZ is directory

• Assume we are in home dir


• $chmod –R u+rwx , g+rx , o+x .
It applies the permission to all hidden files
Directory permissions
• Directories also have their own permissions and the
significance of these permissions differ a great deal
from those of ordinary files.

• The read & write access to an ordinary files are also


influenced by the permissions of the directory
housing them.

• So it’s possible that a file can’t be accessed even


though it has read operation , a file can be deleted
even though if it has write operation.
The Shell
• The Shell and its interpretive cycle
• Pattern Matching – The wild-cards
• Escaping and Quoting
• Redirection – The three standard files
• Filters – Using both standard input and standard
output
• /dev/null and /dev/tty – The two special files
• Pipes
• tee – Creating a tee
• Command Substitution
• Shell Variables
The shell and its interpretive cycle
• The shell sits between you and the operating system, acting as a
command interpreter.

• It reads your terminal input and translates the commands into


actions taken by the system.

• When you log into the system you are given a default shell.

• When the shell starts up it reads its startup files and may set
environment variables, command search paths, and command
aliases, and executes any commands specified in these files.

• The original shell was the Bourne shell, sh. Every Unix platform will
either have the Bourne shell, or a Bourne compatible shell
available.
• Numerous other shells are available.

• Some of the more well known of these may be on


your Unix system:

• The Korn shell, ksh, by David Korn, C shell, csh, by


Bill Joy and The Bourne Again Shell, bash, from the
Free Software Foundation GNU project, both based
on sh, the T-C shell, tcsh, and the extended C shell,
cshe, both based on csh.
• Even though the shell appears not to be doing
anything meaningful when there is no activity at the
terminal, it swings into action the moment you key in
something.
• The following activities are typically performed by the
shell in its interpretive cycle:
❑The shell issues the prompt and waits for you to enter
a command.
❑After a command is entered, the shell scans the
command line for metacharacters and expands
abbreviations (like the * in rm *) to recreate a
simplified command line.
❑It then passes on the command line to the kernel for
execution.
• The shell waits for the command to complete
and normally can’t do any work while the
command is running

• After the command execution is complete, the


prompt reappears and the shell returns to its
waiting role to start the next cycle.

• You are free to enter another command.


Pattern Matching – The Wild-Cards
• A pattern is framed using ordinary characters
and a metacharacter (like *) using well-defined
rules.

• The pattern can then be used as an argument to


the command, and the shell will expand it
suitably before the command is executed.
The metacharacters that are used to construct the generalized pattern for
matching filenames belong to a category called wild-cards. The following table
lists them:
Examples:
• To list all files that begin with chap, use
$ ls chap*

• To list all files whose filenames are six character long


and start with chap, use
$ ls chap??

Note: Both * and ? operate with some restrictions. for


example, the * doesn’t match all files beginning with a
. (dot) or the / of a pathname.
• If you wish to list all hidden filenames in your directory
having at least three characters after the dot, the dot
must be matched explicitly. $ ls .???*
• However, if the filename contains a dot
anywhere but at the beginning, it need not be
matched explicitly.

• Similarly, these characters don’t match the /


in a pathname. So, you cannot use
$ cd /usr?local to change to /usr/local.
The character class
• You can frame more restrictive patterns with the
character class.
• The character class comprises a set of characters
enclosed by the rectangular brackets, [ and ], but it
matches a single character in the class.
• The pattern [abd] is character class, and it matches a
single character – an a,b or d.

Examples:
• $ls chap0[124]
• Matches chap01, chap02, chap04 and lists if found.
• $ ls chap[x-z]
• Matches chapx, chapy, chapz and lists if found.
• You can negate a character class to reverse a
matching criteria.

• For example, To match all filenames with a


single-character extension but not the .c or .o
files, use *.[!co]

• To match all filenames that don’t begin with an


alphabetic character, use [!a-zA-Z]*
Matching totally dissimilar patterns
• This feature is not available in the Bourne shell.

• To copy all the C and Java source programs from


another directory, we can delimit the patterns with a
comma and then put curly braces around them.
• $ cp $HOME/prog_sources/*.{c,java} .

• The Bourne shell requires two separate invocations


of cp to do this job.
• $ cp /home/srm/{project,html,scripts/* .
• The above command copies all files from three
directories (project, html and scripts) to the current
directory.
Escaping and Quoting
• Escaping is providing a \ (backslash) before the wild-
card to remove (escape) its special meaning.

• For instance, if we have a file whose filename is chap*


(Remember a file in UNIX can be names with virtually
any character except the / and null), to remove the file,
it is dangerous to give command as rm chap*, as it will
remove all files beginning with chap.

• Hence to suppress the special meaning of *, use the


command rm chap\*
• To list the contents of the file chap0[1-3], use
• $ cat chap0\[1-3\]

• A filename can contain a whitespace character also.


Hence to remove a file named My Document.doc,
which has a space embedded, a similar reasoning
should be followed:
• $ rm My\ Document.doc
• Quoting is enclosing the wild-card, or even the entire
pattern, within quotes.
• Anything within these quotes (barring a few
exceptions) are left alone by the shell and not
interpreted.
• When a command argument is enclosed in quotes,
the meanings of all enclosed special characters are
turned off.
• Examples:
• $ rm ‘chap*’ Removes file chap*
• $ rm “My Document.doc” Removes file
My Document.doc
Redirection : The three standard files
• The shell associates three files with the terminal – two for
display and one for the keyboard.
• These files are streams of characters which many commands
see as input and output.
• When a user logs in, the shell makes available three files
representing three streams.

• Each stream is associated with a default device:


• Standard input: The file (stream) representing input, connected
to the keyboard.
• Standard output: The file (stream) representing output,
connected to the display.
• Standard error: The file (stream) representing error messages
that emanate from the command or shell, connected to the
display.
The standard input can represent three input
sources:
• The keyboard, the default source.
• A file using redirection with the < symbol.
• Another program using a pipeline.
The standard output can represent three possible
destinations:
• The terminal, the default destination.
• A file using the redirection symbols > and >>.
• As input to another program using a pipeline
• A file is opened by referring to its pathname, but
subsequent read and write operations identify
the file by a unique number called a file
descriptor.
• The kernel maintains a table of file descriptors
for every process running in the system.
• The first three slots are generally allocated to
the three standard streams as,
• 0 – Standard input
• 1 – Standard output
• 2 – Standard error
• These descriptors are implicitly prefixed to the
redirection symbols.

Example:
• Assuming file2 doesn’t exist, the following
command redirects the standard output to file
myOutput and the standard error to file myError.
• $ ls –l file1 file2 1>myOutput 2>myError
• To redirect both standard output and standard
error to a single file use:
• $ ls – l file1 file2 1>| myOutput 2>| myError
OR
• $ ls –l file1 file2 1> myOutput 2>&
Filters: Using both standard input and standard
output
• UNIX commands can be grouped into four
categories

1. Directory-oriented commands like mkdir, rmdir and


cd, and basic file handling commands like cp, mv
and rm use neither standard input nor standard
output.

2. Commands like ls, pwd, who etc. don’t read


standard input but they write to standard output.

3. Commands like lp that read standard input but


don’t write to standard output.
• Commands in the fourth category are called
filters.
• Note that filters can also read directly from files
whose names are provided as arguments.

Example: To perform arithmetic calculations that


are specified as expressions in input file calc.txt
and redirect the output to a file result.txt, use
• $ bc < calc.txt > result.txt
Pipes
• With piping, the output of a command can be used
as input (piped) to a subsequent command.
• $ command1 | command2
• Output from command1 is piped into input for
command2.
• This is equivalent to, but more efficient than:
• $command1 > temp
• $ command2 < temp
• $ rm temp

• Example
• $ ls -a | more
• $ who | sort | lpr
When a command needs to be ignorant of its source

• If we wish to find total size of all C programs


contained in the working directory, we can use the
command,
• $ wc –c *.c
• However, it also shows the usage for each file(size of
each file).
• We are not interested in individual statistics, but a
single figure representing the total size.
• To be able to do that, we must make wc ignorant of
its input source.
• We can do that by feeding the concatenated output
stream of all the .c files to wc –c as its input:
• $ cat *.c | wc –c
Creating a tee
• tee is an external command that handles a
character stream by duplicating its input.
• It saves one copy in a file and writes the other to
standard output.
• It is also a filter and hence can be placed
anywhere in a pipeline.
• Example: The following command sequence uses
tee to display the output of who and saves this
output in a file as well.
• $ who | tee users.lst
Command substitution
• The shell enables the connecting of two commands in
yet another way.

• A pipe enables a command to obtain its standard input


from the standard output of another command, the
shell enables one or more command arguments to be
obtained from the standard output of
anothercommand.

• This feature is called command substitution.

Example:
• $ echo Current date and time is `date`
• Observe the use of back quotes around date in the
previous command.
• Here the output of the command execution of date is
taken as argument of echo.
• The shell executes the enclosed command and
replaces the enclosed command line with the output
of the command.
• Similarly the following command displays the total
number of files in the working directory.
• $ echo “There are `ls | wc –l` files in the current
directory”
• Observe the use of double quotes around the
argument of echo. If single quotes are used, the
backquote is not interpreted by the shell if enclosed in
single quotes.
Shell variables
• Environmental variables are used to provide
information to the programs you use.
• You can have both global environment and local shell
variables.

• Global environment variables are set by your login


shell and new programs and shells inherit the
environment of their parent shell.

• Local shell variables are used only by that shell and


are not passed on to other processes.
• A child process cannot pass a variable back to its
parent process.
• To declare a local shell variable we use the form
variable=value (no spaces around =) and its
evaluation requires the $ as a prefix to the
variable.
• $ count=5
• $ echo $count 5
• A variable can be removed with unset and
protected from reassignment by readonly.
• Both are shell internal commands.
Regular Expression
• To search a file for a pattern, either to see the
lines containing (or not containing) it or to have it
replaced with something else.
• Two important filters that are specially suited for
these tasks – grep and sed.
• grep takes care of all search requirements we may
have.
• sed goes further and can even manipulate the
individual characters in a line.
• In fact sed can do several things, some of them
quite well.
grep – searching for a pattern
grep – searching for a pattern
• It scans the file / input for a pattern and displays lines
containing the pattern, the line numbers or filenames
where the pattern occurs.
• It’s a command from a special family in UNIX for handling
search requirements.
• $grep options pattern filename(s)
• $grep “sales” emp.lst
• will display lines containing sales from the file emp.lst.

• Patterns with and without quotes is possible. It’s generally


safe to quote the pattern.
• Quote is mandatory when pattern involves more than one
word.
• It returns the prompt in case the pattern can’t be located.
• $grep president emp.lst
• When grep is used with multiple filenames, it
displays the filenames along with the output.
• $grep “director” emp1.lst emp2.lst
• Where it shows filename followed by the
contents
grep options
• -i ignores case for matching
• -v doesn’t display lines matching expression
• -n displays line numbers along with lines
• -c displays count of number of occurrences
• -l displays list of filenames only
• -e exp specifies expression with this option
• -x matches pattern with entire line
• -f file takes patterns from file, one per line
• -E treats pattern as an extended RE
• -F matches multiple fixed strings
$who | grep kumar > foo
No quoting and no president found
Here quoting is required ( more than one word)
-i ignores case
v – deleting lines
-n displaying line numbers
-c counting lines containing
pattern
-l (el) displaying filenames
- e matching multiple patterns

Takes pattern from file.


Employee id that begins with 2
Salary lies between 7000 and 7999
Essential Shell Programming
Shell Programming
ORDINARY AND ENVIRONMENT VARIABLES

Environment variable
⚫ Environment variables control the behaviour of
the system. They determine the environment in
which user work.
Ordinary Variable
⚫ Ordinary variables are local to a particular user’s
shell. These variables exist only for a short time
during the execution of a shell script.
⚫ They are local to the user’s shell environment
and are not available for the other scripts or
processes.
⚫ As these variables are defined and used by
specific users, they are also called user-defined
variables.
⚫ For example, one could set a variable called sum
⚫ Variablesare defined using an equal to (=) operator
without any spaces on either side of it.
⚫ The general format of variable declaration is :
variable=value.
⚫ The value of variables are stored in the ASCII
format.

Evaluating a shell variable:


⚫ Variables are evaluated by prefixing the variable
name with a $.
⚫ When the shell reads a command line, all the words
that are preceded by a $ are identified and
evaluated as variables unless otherwise the $ is
despecialized.
⚫ Ex: $x=50
$echo $x
$echo $a
THE .PROFILE FILE

⚫ This file is a shell script that will be present in the


home directory of every user. As this file resides in
the HOME directory, it gets executed as soon as the
user logs in.

⚫ The .profile file is specific for every individual user


and is responsible for user environment.

⚫ The system administrator provides each user with a


profile that will be sufficient to have a minimum
working environment, the user can then edit and
customize the same according to their convenience.
⚫ This file is automatically executed on login, it is
called the AUTOEXE.BAT file of unix.
read : making scripts interactive

Read can be used with one or more variables, ex: read pname flname
Readonly commands
[root@localhost ~]# function hello() {
• Variables and functions can be made echo "hello"; }
readonly. [root@localhost ~]# readonly -f hello
[root@localhost ~]# readonly a=10 [root@localhost ~]# function hello() {
[root@localhost ~]# echo $a echo "hello world"; }
10 bash: hello: readonly function
[root@localhost ~]# set a=20
[root@localhost ~]# echo $a • Can unset readonly function
10 [root@localhost ~]# unset hello
• Cannot unset a readonly variable, [root@localhost ~]# function hello() {
solution is to kill the shell echo "hello world"; }
[root@localhost ~]#
[root@localhost ~]# unset a
bash: unset: a: cannot unset: readonly
variable
Using Command line arguments
exit and exit status of command
• exit 0 when everything went fine
• exit 1 when something went wrong
$cat foo
cat: can’t open foo
• The shell offers $? and test that evaluates a commands exit
status.
$grep director emp.lst > /dev/null; echo $?
0 success
$grep manager emp.lst > /dev/null; echo $?
1 failure in finding pattern
$grep manager emp3.lst > /dev/null; echo $?
grep : can’t open emp3.lst failure in opening
file
2
The Logical Operators && and ||

$ grep ‘director’ emp.lst && echo “pattern found”


1006|singhvi | director | sales | 09/08/1980|8000
Pattern found

$ grep ‘manager’ emp.lst || echo “Pattern not


found”
Pattern not found
The if conditional
The if conditional
test
• Compares two numbers.
• Compares two strings or a single one for a null value.
• Checks a file attributes.
Shorthand for test
• Following two are equivalent
test $x –eq $y

[ $x –eq $y ]
▪ White spaces are required.
String comparison
File Tests
The case conditional
The case conditional
The while loop
The while loop

$emp5.sh
Enter the code and description: 03 analgesics
Enter any more (y/n)? n
for : looping with a list
for : looping with a list
#!/bin/sh
#emp6.sh -- using for loop with positional parameters

for pattern in "$@" ;


do
grep "$pattern" emp.lst || echo "$Pattern not found"
done
set and shift: Manipulating positional parameters

• set assigns its arguments to the positional


parameters $1, $2 and so on...
• Useful in picking individual fields from output of a
program.

$set 9876 2345 6213


$
$echo “\$1 is $1 , \$2 is $2 , \$3 is $3”
$1 is 9876, $2 is 2345, $3 is 6213
$echo “The $# arguments are $*”
The 3 arguments are 9876 2345 6213
$set `date`
$echo $*
Mon Oct 23 10:20:25 IST 2017
$echo “ The date today is $2 $3, $6”
The date today is Oct 23, 2017
shift: shifting arguments left
$echo “$@”
Mon Oct 23 10:20:25 IST 2017
$echo $1 $2 $3
Mon Oct 23
$shift
$echo $1 $2 $3
Oct 23 10:20:25
$shift 2 shifts 2
places
$echo $1 $2 $3
10:20:25 IST 2017
The here document (<<)
• Signifies that the data is here rather than in a separate
file.
• Any command using standard input can also take input
from a here document.
• This feature is helpful when used with commands that
don't accept a filename as argument(mailx command).
• Ex:
$emp1.sh << END
>director
>emp.lst
>END
trap : interrupting a program
• The trap statement lets you do the things you want
in case the script receives a signal.
• The statement is normally placed at the beginning of
a shell script.
trap “command_list” signal_list
• When a script is sent any of the signals in
“signal_list”, trap executes the commands in
“command_list”.
• The signal list can contain the integer values or
names. (without SIG prefix).
SIGHUP(1) SIGINT(2) SIGTERM(15)
Write a shell script to find the sum of n given
numbers.
#!/bin/sh
i=1
sum=0
echo "enter the the n value"
read n
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo “The sum is $sum”

You might also like