UNIX PROGRAMMING (BCS515C)
Module 2
File attributes and permissions
The Shell Interpretive Cycle
Syllabus Module 2
File attributes and permissions: The ls command with options. Changing file
permissions: the relative and absolute permissions changing methods. Recursively
changing file permissions. Directory permissions.
The shells interpretive cycle: Wild cards. Removing the special meanings of wild
cards. Three standard files and redirection.
Connecting commands: Pipe. Basic and Extended regular expressions. The grep,
egrep. Typical examples involving different regular expressions.
Shell programming: Ordinary and environment variables. The. profile. Read and
read-only commands. Command line arguments. exit and exit status of a command.
Logical operators for conditional execution. The test command and its shortcut. The
if, while, for and case control statements. The set and shift commands and handling
positional parameters. The here (<<) document and trap command. Simple shell
program examples.
Text Book1: Chapter-6,8,13,14
The ls command with options
ls -l: LISTING FILE ATTRIBUTES
• ls command is used to obtain a list of all filenames in the current
directory. The output in UNIX ls is often referred to as the listing.
• Sometimes we combine this option with other options for
displaying other attributes, or ordering the list in a different
sequence.
• ls look up the file’s inode to fetch its attributes. It lists seven
attributes of all files in the current directory and they are:
File type and Permissions
• The file type and its permissions: The first column shows the type
and permissions associated with each file.
• The first character in this column is mostly a - which indicates that
the file is an ordinary one. In unix, file system has three types of
permissions- read, write and execute.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 3
chikmagalur
• Links: The second column indicates the number of links
associated with the file. This is actually the number of
filenames maintained by the system of that file.
• Ownership: The third column shows the owner of files. The
owner has full authority to tamper with files content and
permissions. Similarly, you can create, modify or remove files
in a directory if you are the owner of the directory.
• Group ownership: The fourth column represents the group
owner of the file. When opening a user account, the system
admin also assigns the user to some group. The concept of
a group of users also owning a file has acquired
importance today as group members often need to work
on the same file.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 4
chikmagalur
• File size: The fifth column shows the size of the file in
bytes. The important thing to remember here is that it only a
character count of the file and not a measure of the disk space
that it occupies.
• Last modification time: The sixth, seventh and
eighth columns indicate the last modification time of the file,
which is stored to the nearest second. A file is said to be
modified only if its content have changed in any [Link]
the file is less than one year old since it is created then year
will not be displayed.
• Filename: The last column displays the filename
arranged in ASCII collating sequence.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 5
chikmagalur
For example,
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 1 kumar metal 4174 may 10 15:01 chap02
-rw-rw-rw- 1 kumar metal 84 feb 12 12:30 [Link]
-rw-r--r-- 1 kumar metal 9156 mar 12 1999 [Link]
drwxr-xr-x 2 kumar metal 512 may 09 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 09 09:57 progs
File Permissions
• UNIX has a simple and well defined system of assigning
permissions to files.
• Lets issue the ls -l command once again to view the
permissions of a few lines .
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 6
chikmagalur
$ls -l chap02 [Link] [Link]
-rwxr-xr-- 1 kumar metal 25000 May 10 19:21 chap02
-rwxr-xr-x 1 kumar metal 890 Jan 10 23:17 [Link]
-rw-rw-rw- 1 kumar metal 84 Feb 18 12:20 [Link]
• Consider the first column. - rwx r-x r --
• Each group here represents the category and contain
three slots representing the read, write and execute
permissions of the file.
• r indicates the read permission; w indicates write
permission; x indicates execute permission - (hyphen)
indicates the absence of the corresponding permission.
• In the above example, the file permissions of chap02 file
is - rwx r-x r - - File owner, user group and others
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 7
chikmagalur
• First group(rwx) has all the three permissions. The file is
readable, writable and executable by the owner of the file,
kumar. The third column shows the owner of the file. The first
permissions group applies to kumar. You have to login with
the name kumar for the privileges to apply to you.
• Second group(r-x): has a hyphen in the middle slot, which
indicates the absence of write permissions by the group
owner of the file. The group owner is metal and all users
belonging to group metal has only read and execute
permissions.
• Third group(r--): has the write and execute bits absent. This
set is applicable to others i,e those who are neither the owner
nor group. This category is referred to as the world.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 8
chikmagalur
CHANGING FILE PERMISSIONS:RELATIVE AND
ABSOLUTE PERMISSIONS
• chmod command.
• A file or a directory is created with a default set of
permissions, which can be determined by umask.
• Let us assume that the file permission for the created file is
-rw-r--r--.
• Using chmod command, we can change the file
permissions.
• The command can be used in two ways:
1. In a relative manner by specifying the changes to the
current permissions
2. In an absolute manner by specifying the final permissions
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 9
chikmagalur
RELATIVE PERMISSIONS
• chmod only changes the permissions specified in the
command line and leaves the other permissions
unchanged.
• Its syntax is:
chmod category operation permission filename(s)
• chmod takes an expression as its argument which
contains:
• user category (user, group, others)
• operation to be performed (assign or remove a
permission)
• type of permission (read, write, execute)
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 10
chikmagalur
• The below shows the abbreviations used by
chmod command
Category operation permission
u - user + assign r - read
g - group - remove w - write
o - others = absolute x - execute
a- all (ugo)
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 11
chikmagalur
Ex 1:
$ls -l xstart
-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart
• Here user is having the only read and execute
permission .
• Using relative file permission need to add the
execute permission to user
chmod category operation(+,-) permission filename
$chmod u + x xstart
$chmod u+x xstart
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 12
chikmagalur
Ex 2: To remove execute permission from all
and assign read permission to group and
others
$chmod a-x, go+r xstart
• To remove execute permission from all(a) ie
user, group, others
• To assign read permission to group and others
(go+r)
Ex 3: To assign write and execute permissions
for others.
$chmod o+wx xstart
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 13
chikmagalur
ABSOLUTE PERMISSIONS
• A string of three octal digits is used as an expression.
• The permission can be represented by one octal digit
for each category.
• For each category, we add octal digits.
• If we represent the permissions of each category by
one octal digit, this is how the permission can be
represented:
Read permission 4 (octal 100)
Write permission 2 (octal 010)
Execute permission 1 (octal 001)
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 14
chikmagalur
Octal Permissions Significance
0 --- no permissions
1 --x execute only
2 -w- write only
3 -wx write and execute
4 r-- read only
5 r-x read and execute
6 rw- read and write
7 rwx read, write and execute
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 15
chikmagalur
Ex 1: To assign read,write permissions to all .
• Using relative permission, we have,
$chmod a+rw xstart
• Using absolute permission, we have,
$chmod 666 xstart
Ex 2: To assign read and write for user and remove
write, execute permissions from group and others
• Here to assign rw- corresponds to digit 6
• Remove write , execute permissions is nothing but
assigning only read option to group and others
• Only read permission is r corresponds to 4
$chmod 644 xstart
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 16
chikmagalur
Ex 3: To assign all permissions to the owner, read
and write to group and only execute for
others.
$chmod 761 xstart
Ex 4 : To assign all permissions to all categories.
$chmod 777 xstart
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 17
chikmagalur
The Security Implications
• Let the default permission for the file xstart is -rw-r r–
$chmod u-rw, go-r xstart or
$chmod 000 xstart
• After Executing above any one command the output
will be removes the all permission from all categories
as shown below
----------
• This is simply useless but still the user can delete this
file
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 18
chikmagalur
• On the other hand,
chmod a+rwx xstart or
chmod 777 xstart
• After Executing either of the one command it adds
the all permission to all categories as shown below
rwxrwxrwx
• The UNIX system by default, never allows this
situation as you can never have a secure system.
Hence, directory permissions also play a very vital
role here.
RECURSIVELY CHANGING FILE PERMISSIONS
use chmod Recursively.(-R)
• It i s possible to make chmod descend
directory hierarchy and apply the expression to
every file and sub directory it finds. This is done by
using -R option
$chmod -R a+x shell_scripts
• This makes all the files and subdirectories found in
the shell_scripts directory, executable by all users.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 20
chikmagalur
DIRECTORY PERMISSIONS
• Directories also have their own permissions and the
significance of these permissions differ from those of ordinary
files.
• The default permissions of a directory are, rwxr-xr-x (755)
• A directory must never be writable by group and others
Ex1:
$mkdir c_progs ; ls -l c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
• Here the c_progs directory is created (mkdir c_progs) and
then the attributes of directory is listed out(ls -ld c_progs)
• If a directory has write permission for group and others
also, be assured that every user can remove every file in the
directory.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 21
chikmagalur
CHANGING FILE OWNERSHIP
• There are two commands meant to change the
ownership of a file or directory.
• chown changing file owner and
• chgrp changing group owner
chown : Changing file owner
• The syntax is
chown options owner [:group] file(s)
• For changing ownership requires super user permission.
So first change our status to that of super user with the su
command:
$su
Password: ******
#_
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 22
chikmagalur
• After the password is successfully entered, su returns a #
prompt, the prompt used by root.
• su lets us acquire super user status if we know the root
password.
• Consider the file note owned by kumar. To now renounce
the ownership of the file note to Sharma, use chown in
the following way:
# ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30 note
#chown sharma note; ls -l note
-rwxr----x 1 sharma metal 347 may 10 20:30 note
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 23
chikmagalur
• Once ownership of the file has been given away to
sharma, the user file permissions that previously
applied to Kumar now apply to sharma.
• Thus, Kumar can no longer edit note since there is no
write privilege for group and others. He cannot get
back the ownership either.
• But he can copy the file to his own directory, in
which case he becomes the owner of the copy.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 24
chikmagalur
chgrp :Changing Group Owner
• This command changes the file’s group owner.
No superuser permission is required.
$ ls l [Link]
-rw-r--r-- 1 kumar metal 139 jun 8 16:43 [Link]
• Here the group owner of [Link] is metal
$chgrp dba [Link] ; ls -l [Link]
-rw-r--r-- 1 kumar dba 139 jun 8 16:43 [Link]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 25
chikmagalur
• The group owner of the file [Link] is changed
from metal to dba by issuing the command
$chgrp dba [Link]
• The file attributes of [Link] is listed by issuing
the command
$ls -l [Link]
• Using chown to change both file owner and
group :
• The syntax requires two arguments to be
separated by :
chown sharma:dba [Link]
• Here the ownership of [Link] is changed to
sharma and group to dba
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 26
chikmagalur
• When you log onto a UNIX machine , you see a prompt.
• This prompt remains until you key in something. Even
though it may appear that the system is idling, a UNIX
command is in fact running at the terminal.
• But this command is special its with you all the time
and never terminate unless you log out. This command
is shell.
• If you provide the input in the form of ps command
(that shows processes owned by you), you will see shell
running
$ps
PID TTY TIME CMD
328 pts/2 0:00 bash
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 27
chikmagalur
• The bash shell is running at the terminal /pts/2.
• When you key in the command it goes to shell as input.
• The shell scans the command line for metacharacters.
These are the characters that mean nothing to the
command but has special meaning to the shell.
• For example if the shell encounters metacharacters like *,|
etc in the command line.
• If the metacharacter is *, then shell replaces it with all the
filenames in the current directory.
• When all preprocessing is complete , the shell passes on
the command to the kernel for ultimate execution.
• While the command is running, the shell has to wait for
notice of its termination from the kernel.
• After the command is complete with its execution, then
shell once again issues the prompt to take up your next
command.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 28
chikmagalur
THE SHELL- INTERPRETIVE CYCLE.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 29
chikmagalur
ACTIVITIES PERFORMED BY THE SHELL IN ITS
INTERPRETIVE CYCLE.
1. The shell issues the prompt and waits for you to enter a
command.
2. After a command is entered, the shell scans the command line
for meta-characters and expands abbreviations (like * in rm *)
to re-create the simplified command line.
3. It then passes on the command line to the kernel for execution
4. The shell waits for the command to complete and normally
cant do any work while the command is running.
5. After command execution is complete, the prompt re-appers and
shell returns to its waiting role to start the next cycle. You
can now enter the next command.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 30
chikmagalur
WILD CARDS
• The metacharacters that are used to construct the generalized
pattern for matching filenames belong to the category called wild
cards.
The * and ?
• The meta-character * is one of the characters of the shell wild card
set. It matches any number of characters including none.
For example : to match filenames chap chap01 chap02 chap03 chap04
$ ls chap*
chap
chap01
chap02
chap03
chap04
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 31
chikmagalur
• The metacharacter ? matches a single character
• For example: to match filenames chapx chapy chapz
$ ls chap?
chapx
chapy
chapz
Matching the dot(.)
• The . dot metachacter can be used to match all the hidden files in your
directory.
• Example: To list all hidden files in your directory having at least three
characters after the dot.
$ ls .???*
.bash_profile
.exrc
.netscape
.profile
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 32
chikmagalur
The character class [ ]
• This class comprises a set of characters enclosed by the
rectangular brackets [ and ],but it matches only a single
character in the class.
• The pattern [abcd] is a character class and it matches a
single character a, b, c or d
• For example to match chap01 chap02 chap03 chap04
$ ls chap0[1234]
Chap01
chap02
chap03
chap04
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 33
chikmagalur
Negating the character class(!)
• It is used to reverse the matching criteria.
For example: To match all the filenames with single character extensions
but not .c or .o files
$ ls *.[!co]
• * to match any filename
• . extension
• ! except
• [!co]---> .c extension or .o extension
• To match filenames that does not begin with a digit
$ ls [!0-9]*
• To match filename with 3 character that does not begin with an Upper
Case letter
$ ls [!A-Z]??
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 34
chikmagalur
REMOVING THE SPECIAL MEANINGS OF WILD
CARDS
ESCAPING and QUOTING
• Escaping: providing a \(backslash character)
before the wild card to remove or escape its
special meaning.
• Quoting: enclosing the wild card or even the
entire pattern within quotes ( 'chap* ' ).
Anything within the quotes are left alone by
the shell and not interpreted
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 35
chikmagalur
ESCAPING
• Placing a \ immediately before a meta-character turns
off its special meaning.
• For instance \* , matches * itself. Its special meaning
of matching zero or more occurrences of character is
turned off.
Ex 1:
$rm chap*
• removes all the filenames starting with chap. Chap,
chap01,chap02 and chap03 are removed.
$rm chap\*
• removes the filename with chap*
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 36
chikmagalur
Ex 2:
• If there are files with names chap01, chap02,chap03.
To list the filenames starting with chap0
$ ls chap0[1-3]
chap01
chap02
chap03
Ex 3:
• To match the file named as chap0[1-3]
$ ls chap0\[1-3\]
chap0[1-3]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 37
chikmagalur
Escaping the space: To remove the file My
[Link], which has space embedded,
$rm My\ [Link]
Escaping the newline character.
$ echo The newline character is \\n enter the
command
The newline character is \n enter the command
Escaping the \ itself
$echo \\
\
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 38
chikmagalur
QUOTING
• This is the another way of turning off the meaning of meta-
character.
• When a command argument is enclosed within quotes, the
meaning of all enclosed special characters are turned off
$rm 'chap*'
• removes the filename with chap*
$rm "My\ [Link]“
• remove the file My [Link], which has space
embedded.
$echo '\‘
\
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 39
chikmagalur
REDIRECTION: THE THREE STANDARD FILES
• Redirection is the process by which we specify that a file is to be
used in place of one of the standard files.
1. With input files, we call it input redirection;
2. With output file, we call it output redirection
3. With the error file, we call it error redirection.
• Standard input: The file representing the input, which is
connected to the keyboard
• Standard output: The file representing the output, which is
connected to the display.
• Standard error: the file representing the error messages that
emanate from the command or shell. This is also connected to
display.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 40
chikmagalur
• Each of the three standard files are
represented by a number called a file
descriptor.
• The first three slots are generally allocated to
three standard streams in this manner
0: standard input
1: standard output
2: standard error
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 41
chikmagalur
STANDARD INPUT
• This file is special file. The keyboard, the default
source a file using redirection with the < symbol (
redirection operator is less than symbol)
• When you use wc without an argument , it
prompts you to provide the input from standard
input keyboard.
$ wc
Unix is a multiuser multitasking OS
[ctrl-d]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 42
chikmagalur
• When wc is used with argument. Filename is
passed as an argument i,e wc takes the input
from the filename we have specified
For example: Create a file with name [Link]
$ vi [Link]
Unix is a multiuser multitasking OS
:wq
$ wc < [Link]
1 6 36
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 43
chikmagalur
STANDARD OUTPUT
• All commands displaying the output on the
terminal actually write to the standard output
file as a stream of characters and not directly
to the terminal as such.
• There are three possible destinations of this
stream
1. The terminal, the default destination
2. A file using the redirection symbol > and >>
3. As input to another program using a pipeline
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 44
chikmagalur
• There are two basic redirection operators for
standard output.
1. greater than character(>):
• If you want the file to contain only the output
from this execution of the command, you can use
greater than token.
Ex: consider the file [Link]
$ cat [Link]
Unix is a multiuser multitasking OS
$ wc [Link] > newfile
$ cat newfile
1 6 37
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 45
chikmagalur
2. Two greater than characters>> (append)
• If you want the output of the command to be appended
without overwriting the existing content. Use Two
greater than characters>> as output redirection
operator.
$who >> newfile
$cat newfile
1 6 37
root console aug 1 07:51 (:0)
kumar pts/10 aug 1 02:51 (:0)
sharma pts/6 aug 1 03:51 (:0)
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 46
chikmagalur
STANDARD ERROR
• When you enter an incorrect command or try to
open a non existent file, certain diagnostic
messages show up on the screen. This is the
standard error stream whose default destination
is the terminal.
Standard output and error on monitor
Ex 1: Consider two files file1 and file2 , where file1
exist and file2 do not exist
$ ls -l file1 file2
-rwxr--r-- 1 gilberg staff 1234 oct file1
Cannot access file2: no such file or directory
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 47
chikmagalur
Ex 2: To redirect standard output to same file
$ls -l file1 file2 1>filelist 2>filelist
• The 1st argument is file1 and 2nd argument is
file2.
• The output and errors are sent to file named
filelist
$cat filelist
-rwxr--r-- 1 gilberg staff 1234 oct file1
Cannot access file2: no such file or directory
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 48
chikmagalur
Ex 3: To redirect standard output to different files
$ls -l file1 file2 1>stdout 2>stderr
• The 1st argument is file1 and 2nd argument is file2.
• The output of 1st file is sent to filename stdout
and errors are sent to file named stderr.
$cat stdout
-rwxr--r-- 1 gilberg staff 1234 oct file1
$cat stderr
Cannot access file2: no such file or directory
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 49
chikmagalur
CONNECTING COMMANDS: PIPE.
• We often need to use a series of commands to
complete a task.
• For example, if we need to see list of users logged
into the system, we use who command. However if
we need a hard copy of the list, we need two
commands.
• First use who command to get the list and store the
result in file using redirection, then use lpr command
to print the file
• We can avoid the creation of intermediate file by
using a pipe
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 50
chikmagalur
• Pipe is an operator that temporarily saves the output of
one command in a buffer that is being used at the same
time as the input of the next command.
• The first command must be able to send its output to
standard output. The second command must be able to
read its input from standard input. The token for a pipe
is vertical bar( | )
$ who | lpr
root console aug 1 07:51 (:0) //uses connected printer to
kumar pts/10 aug 1 02:51 (:0) // create hard copy
sharma pts/6 aug 1 03:51 (:0)
rajath pts/8 aug 1 06:51 (:0)
vikas pts/14 aug 1 09:51 (:0)
$who | wc -l
5
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 51
chikmagalur
The grep,egrep
• grep(globally search regular expression and print)
• Unix has a special family of commands for handling search
requirements and the principal member of the family is the grep
command.
• grep scans its input for a pattern and displays lines containing
the pattern, the line numbers or filename where the pattern
occurs.
Syntax:
• grep options pattern filename(s)
• grep searches for pattern in one or more filename or the
standard input if no filename is specified.
• The first argument(barring the options) is the pattern and the
remaining arguments are filenames.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 52
chikmagalur
• Consider three files [Link], [Link], [Link]
$cat [Link]
$cat [Link]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 53
chikmagalur
$cat [Link]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 54
chikmagalur
• Ex 2: To search the pattern director from 2
files i,e [Link] and [Link]
$grep “director” [Link] [Link]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 55
chikmagalur
Options Used by grep
Options Significance
-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. Can use multiple times. Also used for
matching expression beginning with a hyphen.
-x Matches pattern with entire line (doesn't match embedded patterns)
-f file Takes patterns from file, one per line
-E Treats pattern as an extended regular expression (ERE)
-F Matches multiple fixed strings (in fgrep-style)
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 56
chikmagalur
• Ignoring case(-i) : when you look for a name but are not sure of
the case, use the i option to ignore case for pattern matching.
• Deleting lines(-v): The v option selects all lines except those
containing the pattern
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 57
chikmagalur
Dislaying line numbers(-n).
• The n option displays the line numbers containing the pattern
along with the line
• Counting lines containing patter(-c)
• The c option counts the number of lines containing the
pattern.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 58
chikmagalur
Matching multiple patterns( | , ( ) )
• The | is the delimiter for the multiple
patterns. Consider the file [Link] with two
lines of employee details.
• To locate both sengupta and dasgupta from
file [Link]
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 59
chikmagalur
• The parenthesis ( ) lets us group patterns and
use of | inside the parenthesis, we can frame
more compact pattern.
Ashwini Kamath, Asst. Prof., ISE, AIT
21-12-2022 60
chikmagalur
UNIX PROGRAMMING (18CS56)
Module 2
SHELL PROGRAMMING
SHELL PROGRAMMING
ORDINARY AND ENVIRONMENT VARIABLES
• A local variable is a variable that is present within the current
instance of the shell. It is not available to programs that are started
by the shell. They are set at command prompt.
VARIABLE NAMES
• A variable is a character string to which we assign a value. The value
assigned could be a number, text, filename, device, or any other
type of data.
• A variable is nothing more than a pointer to the actual data. The
shell enables you to create, assign, and delete variables.
• The name of a variable can contain only letters ( a to z or A to Z),
numbers ( 0 to 9) or the underscore character ( _).
• By convention, Unix Shell variables would have their names in
UPPERCASE.
Ex: VAR_1, VAR_2, TOKEN _A
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 2
chikmagalur
DEFINING VARIABLES:
Variables are defined as follows
variable_name=variable_value
Example:
NAME="Sumitabha Das“
ACCESSING VARIABLES:
• To access the value stored in a variable, prefix its name with
the dollar sign ( $)
• For example, following script would access the value of
defined variable NAME and would print it on STDOUT
#!/bin/sh
NAME= “ Sumithabha Das”
echo $NAME
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 3
chikmagalur
ENVIRONMENT VARIABLES
• An environment variable is a variable that is available in the user’s
environment- the sub shell that runs shell scripts and mail
commands and editors.
• The local variables are more restricted in scope.
Ex: DOWNLOAD_DIR is a local variable
$ DOWNLOAD_DIR=/home/kumar/download
$echo $DOWNLOAD_DIR
/home/kumar/download
Create a Bourne sub shell
$sh
$echo $DOWNLOAD_DIR //not visible in child shell
$PATH
PATH=/bin:/usr/bin:.:/usr/ccs/bin //visible in entire enviroment
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 4
chikmagalur
Common Environment Variables
Variable Significance
HOME Home directory-the directory a user is placed on logging in
PATH List of directories searched by shell to locate a command
LOGNAME Login name of user
USER Login name of user
MAIL Absolute pathname of user's mailbox file
MAILCHECK Mail checking interval for incoming mail
TERM Type of terminal
PWD Absolute pathname of current directory (Korn and Bash only)
CDPATH List of directories searched by cd when used with a nonabsolute
pathname
PS1 Primary prompt string
PS2 Secondary prompt string
SHELL User's login shell and one invoked by programs having shell
escapes
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 5
chikmagalur
PS1(Prompt String one) and PS2 Environment
Variables
• The characters that the shell displays as your
command prompt are stored in the variable
PS1.
• We can change this variable to be anything we
want. As soon as we change it, it'll be used by
the shell from that point on.
$PS1=‘ [$PWD] ’
[/home/kumar] _
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 6
chikmagalur
• When we issue a command that is incomplete, the shell will
display a secondary prompt and wait for us to complete the
command and hit Enter again.
• The default secondary prompt is > (the greater than sign),
but can be changed by re-defining the PS2 shell variable.
$ echo "this is a // default secondary prompt
> test"
this is a
test
$
$PS2= '-->‘
$ echo "this is a
--> test"
$
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 7
chikmagalur
The .profile File
• The file /etc/profile is maintained by the system
administrator of our UNIX machine and contains shell
initialization information required by all users on a system.
• The file .profile is under our control. We can add as much
shell customization information as we want to this file.
• The minimum set of information that we need to configure
includes
1. The type of terminal you are using
2. A list of directories in which to locate commands
3. A list of variables effecting look and feel of our terminal.
• We can check our .profile available in our home directory.
Open it using vi editor and check all the variables set for
our environment.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 8
chikmagalur
• A shell script is a text file that contains a sequence of
commands for a UNIX-based operating system. It is
called a shell script because it combines a sequence of
commands, that would otherwise have to be typed
into the keyboard one at a time, into a single script.
• Structure of shell script:
#!/bin/sh
# [Link]: Sample shell script
echo “Todays date:`date`”
echo “This month calendar:”
echo “`cal`”
echo “My shell $SHELL”
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 9
chikmagalur
output:
$chmod a+x [Link]
$sh [Link]
Todays date : Wed Sep 3 10:24:19 IST 2025
This month calendar:
September 2025
Su Mo Tu We Th Fr Sa 1 2 3
4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
My shell: /bin/sh
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 10
chikmagalur
• Use your vi editor to create the shell script [Link].
• The script runs three echo commands and shows the
use of variable evaluation and command
• substitution. It also prints the calendar of the current
month.
• Note that the # is comment character, that can be
placed anywhere in a line; the shell ignores all
characters placed on its right.
• The first line, which begins with a #! is interpreter line .
• It always begins with #! and is followed by pathname of
the shell to be used for running the script.
• This line specifies the pathname of the bourne shell.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 11
chikmagalur
• To run the script, make it executable first and
then invoke the script name
$chmod a+x [Link]
$sh [Link]
• Shell scripts are executed in a separate child shell
process and this sub shell need not be of the
same type as your login shell.
• By default child and parent shell belongs to the
same type, but you can provide a interpreter line
in the first line of the script to specify a different
shell for the script
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 12
chikmagalur
read and readonly commands.
read: MAKING SRIPTS INTERACTIVE
• The read statement is the shell internal tool for taking
the input from the user ie making scripts interactive.
• It is used with one or more variables. Input is supplied
through the standard input is read into these variables.
• When you use statement like:
read name
• the script pauses at that point to take input from the
keyboard.
• whatever you enter is stored in the variable name.
• since this is a form of assignment , no $ is used before
the name.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 13
chikmagalur
• A single read statement can be used with one or
more variables to let you enter multiple
arguments.
read pname filename
• The script asks for a pattern to be entered. Input
the string director, which is assigned to the
variable pname.
• Next the script asks for the filename enter the
string [Link] which is assigned to the variable
filename.
• grep runs with these two variables as arguments
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 14
chikmagalur
#!/bin/sh
#[Link]
echo "Enter the pattern to be searched : \c"
read pname
echo " Enter the file to be used : \c"
read filename
echo " Searching for $pname from file $filename"
grep "$pname" $filename
echo "Selected rows shown above"
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 15
chikmagalur
Output:
$sh [Link]
Enter the pattern to be searched: director
Enter the file to be used: [Link]
Searching for director from file [Link]
101 sharma|director|production|12/03/70|7000
102|barun|director|marketing|11/06/67|7800
selected rows shown above
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 16
chikmagalur
COMMAND LINE ARGUMENTS
• When arguments are specified with a shell script
they are assigned to certain special variables
positional parameters.
• $* store the complete set of positional parameters as
a single string.
• $# is set to the number of arguments specified.
• $0 holds the command name itself.
• When arguments are specified in this way the first
word (the command itself) is assigned to $0, the
second word(the first argument) to $1, the third
word(the second argument) to $2.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 17
chikmagalur
#!/bin/sh
#[Link]
#
echo "Program:$0
The number of arguments specified is $#
The arguments are $*“
grep "$1" $2
echo "\n job over"
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 18
chikmagalur
Output:
$ sh [Link] director [Link]
Program: [Link]
The number of arguments specified is:2
The arguments are director [Link]
101|sharma|director|production|12/03/70|7000
102|barun|director|marketing|11/06/67|7800
job over
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 19
chikmagalur
SPECIAL PARAMETERS USED BY THE SHELL
Shell Significance
Parameter
$1, $2... Positional parameters representing command line arguments
$# Number of arguments specified in command line
$0 Name of executed command
$* Complete set of positional parameters as a single string
"$@" Each quoted string treated as separate argument
$? Exit status of last command
$$ PID of the current shell
$! PID of the last background job
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 20
chikmagalur
Exit and exit status of Command
The shells exit command
exit 0 Used when everything went fine.
exit 1 Used when something went wrong
• Its through the exit command or function that every
command returns an exit status to the caller.
• A command is said to return true exit status if it executes
successfully and false if its fails.
• THE PARAMETER $? stores the exit status of the last
command.
• It has the value 0 if the command succeeds and a non zero
value if it fails.
• This parameter is set by exit’s argument. If no exit status is
specified then $? is set to zero(true).
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 21
chikmagalur
• Consider two files file1 which exist in current
directory and file2 which does not exist
$ ls -l file1; echo $? //file1 attributes are listed
-rwxr-xr-- 1 kumar metal 25000 May 10 19:21 file1
0 //exit status $?=0
$ ls l- file2; echo $? //error since file2 doesnot exist
1 //exit status $?=1
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 22
chikmagalur
THE LOGICAL OPERATORS && and ||
CONDITIONAL EXECUTION
• The shell provides two operators that allow conditional
[Link] && and ||.
The syntax:
cmd1 && cmd2
cmd1 || cmd2
• Consider a file [Link]
$cat [Link]
1066| sharma | director |sales |03/09/66 | 7000
1098| Kumar |director| production|0/08/67 | 8200
1082|sumith| manager|marketing|09/09/73| 7090
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 23
chikmagalur
• The && delimits two commands ; the command cmd2 is
executed only when cmd1 succeeds.
$grep “director” [Link] && echo “Pattern found in file”
1066| sharma | director |sales |03/09/66 | 7000
1098| Kumar |director| production|0/08/67 | 8200
Pattern found in file
$
• The || operator plays inverse role. The second command is
executed only when the first fails.
$grep “deputy director” [Link] || echo “Pattern not found ”
Pattern not found
$grep “manager” [Link] || echo “Pattern not found”
1082|sumith| manager|marketing|09/09/73| 709
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 24
chikmagalur
CONDITIONAL STATEMENTS:
The if CONDITIONAL
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 25
chikmagalur
CONDITIONAL STATEMENTS:
The if CONDITIONAL
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 26
chikmagalur
#!/bin/sh
# [Link]
Output:
a=10
b=20
$sh [Link]
if [ $a==$b ] a is lesser than b
Then
echo “a is equal to b”
elif [ $a -gt $b ]
Then
echo “a is greater than b”
elif [ $a -lt $b ]
Then
echo “a is less than b”
else
fi
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 27
chikmagalur
The case CONDITIONAL
• The case statement is the second conditional
offered by the shell
• The statement matches an expression for more
than one alternative and uses a compact
• construct to permit multiway branching.
• The general syntax is
case expression in
pattern1 ) commands1 ;;
pattern2 ) commands2 ;;
pattern3 ) commands3 ;;
esac
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 28
chikmagalur
#!/bin/sh
#[Link]
echo “MENU \n
[Link] of files\n [Link] of user\n [Link] date\n
[Link] of system\n [Link]\n
Enter your option: \n” Output:
read choice $ sh [Link]
MENU
case “$choice” in
1. List of files
1 ) ls -l ;;
2. Processes of user
2 ) ps -f ;;
3. Todays date
3 ) date ;;
[Link] of system
4 ) who ;; 5. Quit
5 ) exit ;; Enter your option : 3
*) echo “Invalid Option” Sun Nov 25 18:03:06 IST 2022
esac
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 29
chikmagalur
Matching multiple patterns:
• case can also specify same action for more than one
pattern.
• For example the expression y|Y can be used to match y
in both upper and lower case letters.
echo “Do you wish to continue:\c”
read answer
case “answer” in
y|Y ) ;;
n|N ) exit ;;
esac
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 30
chikmagalur
Wild cards: case uses them
• case has a string matching feature that uses wild
cards.
• It uses the filename matching meta characters *, ?
and the character class to match strings.
echo “Do you wish to continue:\c”
read answer
case “answer” in
[yY][eE][sS] * ) ;;
[nN][oO] ) exit ;;
esac
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 31
chikmagalur
USING test command and its shortcut
• USING test and [] to evaluate the expressions.
• Test uses the certain operators to evaluate the
condition on its right and returns either true or false
exit status which is then used by if for making
decision .
Test works in 3 ways:
1. Compares two numbers (NUMERIC COMPARISION)
2. compares two strings or a single one for a null
value.(STRING COMPARISION)
3. checks a file attributes. (FILE TEST)
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 32
chikmagalur
NUMERIC COMPARISION:
• The numeric comparison operators used by
test are
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 33
chikmagalur
• Numeric comparison in the shell is confined to integer
values only , decimal values are simply truncated.
$ x=5; y=7; z=7.2
$ test $x -eq $y ; echo $?
1
$ test $x -lt $y ; echo $?
0
$ test $z -gt $y ; echo $?
1
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 34
chikmagalur
STRING COMPARISION
• test can be used to compare strings with yet
another set of operators.
Test True if
s1=s2 String s1=s2
s1!=s2 String s1 is not equal to s2
-n stg String stg is not a null string
-z stg String stg is null string
Stg String stg is assigned and not null
s1==s2 String s1= s2(Korn and bash only)
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 35
chikmagalur
#!/bin/sh
# [Link]
a=“abc”
b=“efg”
if [ $a = $b ] Output:
then $sh [Link]
a is not equal to b
echo “a is equal to b”
else
echo “a is not equal to b”
fi
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 36
chikmagalur
FILE TESTS
• Test can be used to test the various file attributes
like its type(file, directory or symbolic link) or its
permissions (read, write, execute)
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 37
chikmagalur
$ls -l [Link]
-rw-rw-rw- 1 kumar group 870 Sep 8 15:52 [Link]
$ [ -f [Link] ] ; echo $? // -f ( [Link] file exist and its regular file)
1 // Yes
$ [ -x [Link] ] ; echo $? // -x ([Link] file is executable or not)
1 //No
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 38
chikmagalur
while Looping
• The while statement repeatedly performs a set of
instructions until the control command return a
true exit status .
The general syntax is
while condition is true
do
commands
done
• The commands enclosed by do and done are
executed repeatedly as long as condition remains
• true.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 39
chikmagalur
Using while to wait for a file
• Consider the situation when a program needs to read a file that
is created by another program, but it has to wait until the file is
created.
• The script, [Link] periodically monitors the disk for the
existence of the file.
• And then executes the program once the file has been located.
• It makes use of the external sleep command that makes the
script pauses for the duration in seconds as specified in its
arguments .
• The loop executes repeatedly as long as the file [Link]
cannot be read.
• If the file becomes readable the loop is terminated and the
program [Link] is executed.
• We use the sleep command to check every 60 seconds for the
existence of the file.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 40
chikmagalur
#!/bin/sh
#[Link]: waits for file to be created
#
while [ ! –r [Link] ] //until [Link] can’t be read
do
sleep 60 //sleeps for 60 seconds
done
[Link] //execute this program after exiting the loop
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 41
chikmagalur
Setting up an infinite loop
• Suppose you as system administrator want to see the
free space available on your disk every five minutes
while true
do
df -h
sleep 300
done &
• df reports free space on disk . sleep command is used
to hek for every 300 seconds(5 minutes).
• -h option prints the output in human readable format
• & after done runs loop in background
• To terminate in the background job call
kill $!
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 42
chikmagalur
for : LOOPING WITH A LIST
• The shells for loop differs in structure from the ones
used in other programming languages. There is no
three part structure.
for variables in list
do
commands
done
• The loop body also uses the keyword do and done. But
the additional parameters here are variable and list.
• Each whitespace separated word in list is assigned to
variable and commands are executed until list is
executed .
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 43
chikmagalur
$for file in chap20 chap21 chap22
do
cp $file {$file}.bak
echo $file copied to $[Link]
done
Output:
chap20 copied to [Link]
chap21 copied to [Link]
chap22 copied to [Link]
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 44
chikmagalur
set and shift: MANIPULATING THE POSITIONAL
PARAMETERS
• set assigns its argument to positional parameters $1,$2
and so on.
$set 989 878 779
$_
• This assigns the value 989 to the positional parameter
$1, 878 to the positional parameter $2 and 779 to $3
• Ex:
$echo “\$1 is $1, \$2 is $2, \$3 is $3”
$echo “ $# arguments are $* ”
Output:
$1 is 989, $2 is 878, $3 is 779
The 3 arguments are 989 878 779
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 45
chikmagalur
Shift : Shifting Arguments left
• Shift transfers the contents of a positional parameter to its immediate
lower numbered one.
$set `date`
$echo $*
Output: Wed Nov 9 09:04:30 IST 2016
$shift
$ echo $1 $2 $3 $4 $5
Output: Nov 9 09:04:30 IST 2016
$shift 2
$echo $1 $2 $3 $4
Output: 9 09:04:30 IST 2016
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 46
chikmagalur
The HERE DOCUMENT (<<)
• The shell uses << symbols to read data from the
same file containing the script.
• This is referred to as here document , signifying
that the data is here rather than in a separate
file .
• If the message is short you can have both the
command and message in the same script.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 47
chikmagalur
Ex: mail sharma << MARK
Your program for printing the invoices has
been executed on `date`. The updated file is
$flname MARK
• The here document symbol(<<) followed by three
lines of data and a delimiter (the string MARK)
• The shell treats every line following the command
and delimited by MARK as input to the command.
• Sharma at the other end will see the three lines
of message text with the date inserted by
command substitution and the evaluated
filename.
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 48
chikmagalur
trap: INTERRUPTING A PROGRAM
• By default shell scripts terminate whenever the
interrupt key is pressed.
• It may leave a lot of temporary files on disk .
• The trap statement lets you do things you want in
case the script receives a signal.
• The statement is normally placed at the
beginning of a shell script and uses two lists
trap ‘command_list’ signal_list
• When a script is sent any of the signals in
signal_list, trap executes the commands in
command_list
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 49
chikmagalur
• The signal_list can contain the integer values or
names of one or more signals.
trap ‘rm $*; echo “ program interrupted” ; exit’ HUP INT TERM
• trap is a signal handler.
• Here it first removes all the files expanded from $$*,
echoes a message and finally terminates the script
when the signals SIGHUP(1), SIGINT(2), SIGTERM(15)
are sent to the shell process running the script.
• When the interrupt key is pressed it sends the signal
number 2.
trap ‘ ‘ 1 2 15 //ignore all the signals, don’t terminate script
Ashwini Kamath, Asst. Prof., ISE, AIT
12/23/2022 50
chikmagalur