0% found this document useful (0 votes)
67 views47 pages

Module 2 - USP

The lecture notes cover key concepts in UNIX programming, focusing on file attributes, permissions, and the use of the 'ls' command for listing files and directories. It explains how to change file permissions using the 'chmod' command, including both relative and absolute methods, as well as the implications of file ownership and directory permissions. Additionally, it discusses recursive permission changes and the importance of maintaining secure directory permissions to prevent unauthorized access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views47 pages

Module 2 - USP

The lecture notes cover key concepts in UNIX programming, focusing on file attributes, permissions, and the use of the 'ls' command for listing files and directories. It explains how to change file permissions using the 'chmod' command, including both relative and absolute methods, as well as the implications of file ownership and directory permissions. Additionally, it discusses recursive permission changes and the importance of maintaining secure directory permissions to prevent unauthorized access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LECTURE NOTES – MODULE 2


SUBJECT : UNIX PROGRAMMING
SUBJECT CODE: 18CS56
SEMESTER : V

Prepared By:-
Mrs Saranya Babu,
Asst. Professor, Dept of CS&E
UNIX PROGRAMMING 18CS56- Module 2 MODULE-2

File attributes and permissions


----------------------------------------------------------------------------------------------------------------
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 readonly 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
.---------------------------------------------------------------------------------------------------------------

- 2.1 The ls command with options


ls list all filenames and directories in the current directory. It displays the files by using ASCII
collating sequence (num first, uppercase and lowercase), with one filename in each line. In linux the
files and directories are listed(differentiated) with different color.
Syntax: ls [options] [arguments]
$ ls
1prg.c Numbers First
FIRST.C Uppercase Next
One
two Lower case
ls: OPTIONS
Output in multiple columns(-x) : It displays the filenames in multiple columns. So it produces the
multicolumnar output. Modern version of ls does that by default.
$ ls –x

Department of CS&E, AJIET Page 2


UNIX PROGRAMMING 18CS56- Module 2
Identifying directories and executables (-F) : To identify directory files and executable files, -F
option can be used with ls command. Combining this option with –x produces the multicolumnar
output.
Example 1:-
$ ls –xF
a.out* ch1 ch2 ch3 CSA/ isb/ st1 st2 SYLLABUS text book/
example 2:-

⚫ Two symbols * and / are type indicators

⚫ * -> file contains executable codes

⚫ / -> refers to a directory

Showing hidden files (-a) : The hidden files residing in the home directory or any other directory are
normally don’t show up in the listing. –a option lists all hidden files as well:
Listing directory contents : If you specify directory name as an argument to ls command then it will
list the contents of the directory. We can also specify multiple directory names with ls command.
Consider the following example where CSA and isb are two directories
$ ls CSA isb
CSA:
marks list student list
isb:
absent present
Recursive listing (-R) : The –R option lists all files and subdirectories in a directory tree. It traverse
the directory tree until there are no subdirectories or files left.

Department of CS&E, AJIET Page 3


UNIX PROGRAMMING 18CS56- Module 2
$ ls -xR

The list shows the filenames in three sections – the one under the home directory and those under the
subdirectories helpdir and progs.
./helpdir indicates that helpdir is a subdirectory under . (current directory)

2.1.1 ls –l : LISTING FILE ATTRIBUTES


ls command is used to obtain a list of all filenames in the current directory. ls look up the file‘s inode
to fetch its attributes.

For example,
$ ls -l total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir
drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs
It lists seven attributes of all files in the current directory and they are:

• File type and Permissions: First column indicates the file type and its permissions that
are associated with each file. The first – indicates that file is ordinary file and a “d”
indicates its a directory.
• Links: Second column indicates number of links associated with file. Links indicate the
number of file names maintained by the system. This does not mean that there are so
many copies of the file.
• Ownership: File is created by the owner. The one who creates the file is the owner of
that file. In the above example third column shows kumar as the owner of the files • Group
ownership: Fourth column indicates the group owner of the file. Every user is attached to
a group owner. Every member of that group can access the file depending on the
permission assigned.
• File size: File size in bytes is displayed in fifth column. It is the number of character in
the file rather than the actual size occupied on disk.

Department of CS&E, AJIET Page 4


UNIX PROGRAMMING 18CS56- Module 2

• Last Modification date and time: Last modification time is the next field(6 th,7th and 8th
column). If you change only the permissions or ownership of the file, the modification
time remains unchanged. If at least one character is added or removed from the file
then this field will be updated.
• File name:In the last field, it displays the file names arranged in ASCII collating
sequence.

The –d option : Listing Directory Attributes


⚫ To force ls to list attributes of a directory, Use –d option to list the attributes of a directory,
rather than its contents

For example,

$ls –ld helpdirprogs

drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir


drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs

• Directories are easily identified in the listing by the first character of the first column, which
here shows a d.
• The significance of the attributes of a directory differs a good deal from an ordinary file. • To
see the attributes of a directory rather than the files contained in it, use ls –ld with the directory
name. Note that simply using ls –d will not list all subdirectories in the current directory. Strange
though it may seem, ls has no option to list only directories.

2.2 CHANGING THE FILE PERMISSIONS :


File Ownership
When you create a file, you become its owner. Every owner is attached to a group owner. Several
users may belong to a single group, but the privileges of the group are set by the owner of the file
and not by the group members. When the system administrator creates a user account, he has to
assign these parameters to the user:
The user-id (UID) – both its name and numeric representation
The group-id (GID) – both its name and numeric representation

FILE PERMISSIONS
UNIX follows a three-tiered file protection system that determines a file‘s access rights. It is
displayed in the following format: Filetype owner (rwx) groupowner (rwx) others (rwx)

Department of CS&E, AJIET Page 5


UNIX PROGRAMMING 18CS56- Module 2

Consider an example
$ls -l
-rwxr-xr-- 1 kumar metal 19514 may 10 13:45 chap01

⚫ Here first column represents file permission. UNIX follows a three-tiered file protection
system that determines a file‘s access rights. The initial – represents it as an ordinary file.

⚫ Lets break up the permission in above example as

r w x r-x r--

• The first group has all three permissions. The file is readable, writable and executable by the
owner of the file.

• The second group has a hyphen in the middle slot, which indicates the absence of write
permission by the group owner of the file.

• The third group has the write and execute bits absent. This set of permissions is applicable to
others.

• You can set different permissions for the three categories of users – owner, group and others.
It‘s important that you understand them because a little learning here can be a dangerous
thing. Faulty file permission is a sure recipe for disaster.

2.2.1 chmod: Changing File Permissions


• 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 and allow the owner to execute his file.
• The command can be used in two ways:

o In a relative manner by specifying the changes to the current permissions


o In an absolute manner by specifying the final permissions

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)

Department of CS&E, AJIET Page 6


UNIX PROGRAMMING 18CS56- Module 2

• Category :
o u – user
o g – group
o o – others
o a - all (ugo)

• operations :
o + assign
o - remove
o = absolute

• permissions:
o r – read
o w – write
o x - execute

Let us discuss some examples:


Initially,
• Ex 1:- to assign execute permission for user(owner). The command assigns (+) execute (x)
permission to the user (u), other permissions remain unchanged.
$ ls -l start
-rw-r--r-- 1 kumar metal 1906 sep 23:38 start
$ chmod u+x start
$ ls -l start
-rwxr--r-- 1 kumar metal 1906 sep 23:38 start

⚫ Ex 2:- to enable execute permission to all


$ chmod ugo+x start or chmod a+x start
$ ls –l start
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 start

• chmod accepts multiple file names in command line


$chmod u+x note note1 note3
⚫ Permissions are removed with – operator
Ex:- to remove read permission from group and others
$ ls –l start
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 start

$ chmod go-r start


$ ls –l start

Department of CS&E, AJIET Page 7


UNIX PROGRAMMING 18CS56- Module 2

-rwx--x--x 1 kumar metal 1906 sep 23:38 start

Absolute Permissions
• Here, we don’t need to know the current file permissions.

• All nine permissions can be set explicitly. 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)

Binary Octal Permissions Significance


000 0 --- no permissions
001 1 --x execute only
010 2 -w- write only
011 3 -wx write and execute
100 4 r-- read only
101 5 r-x read and execute
110 6 rw- read and write
111 7 Rwx read, write and execute

• We have three categories and three permissions for each category, so three octal digits
can describe a file‘s permissions completely. The most significant digit represents user
and the least one represents others. chmod can use this three-digit string as the
expression.
• Using relative permission, we have,
• For example octal value of 644 ( 110 100 100) means:
o User (6) can read and write
o Group (4) can read only
o Others (4) can read only

• Example 1:- to give all permission for user and only read permission for group and others
chmod 744 start is same as chmod u+x start (relative)

Department of CS&E, AJIET Page 8


UNIX PROGRAMMING 18CS56- Module 2

$ls –l start
-rwxr--r-- 1 kumar metal 1906 sep 23:38 start
• Example 2: to assign read,write execute for user and execute permission for group and others,
chmod 711 start
$ls –l start
-rwx--x--x 1 kumar metal 1906 sep 23:38 start
• Example 3: to assign read write permission for all
$ chmod 666 start;
$ls –l start
-rw-rw-rw- 1 kumar metal 1906 sep 23:38 start
• Example 4: to assign all permission to the owner, read and write to group and only execute for
others
$ chmod 761 start
Security implications
• Consider the default permission of the file start as
-rw-r--r-- 1 kumar metal 1906 sep 23:38 start
These permissions are safe as only user can edit the file.
• Consider the case where we remove all permissions as :-
chmod u-rw,go-r start or
chmod 000 start
now , $ ls –l start
---------- 1 kumar metal 1906 sep 23:38 start
This permission makes the file useless, only user can delete this file

• Next if we set all permissions for all categories of users as:-


chmod a+rwx start or
chmod 777 start
ls -l start
-rwxrwxrwx 1 kumar metal 1906 sep 23:38 start
• 777 signify all permissions for all categories, the universal write permission here is the
highest concern.
• 000 signifies absence of all permissions for all categories, but still we can delete a file.
It is the directory permissions that determine whether a file can be deleted or not.
Only owner can change the file permissions. User cannot change other user‘s file‘s • But
the system administrator can do anything.
2.3 RECURSIVELY CHANGING FILE PERMISSIONS
Using chmod recursively (-R)
• It is possible to make chmod descend a directory hierarchy and apply the expression to every
file and subdirectory it finds. This is done with (-R)
chmod –R a+x shell_scripts

Department of CS&E, AJIET Page 9


UNIX PROGRAMMING 18CS56- Module 2
o this makes all files and subdirectories found in the tree walk(that commences from
shell_scripts directory) executable by all users.
• To use chmod on home directory , then cd to it and use it as :-
chmod –R 755 . //works on hidden files also
chmod –R a+x * //leaves out hidden files

2.4 DIRECTORY PERMISSIONS


The read and write access to an ordinary file are influenced by the permission of the directory
housing them. It is possible that a file cannot be accessed even though it has read permission, and
can be removed even when it is write protected. The default permissions of a directory are, rwxr-xr-
x (755)
A directory must never be writable by group and others
Example:
mkdir c_progs
ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 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. As a rule, you must not make directories universally writable unless you
have definite reasons to do so

2.5 CHANGING FILE OWNERSHIP


• Usually, on BSD and AT&T systems, there are two commands meant to change the ownership of a
file or directory-chown,chgrp. Let kumar be the owner and metal be the group owner. If sharma
copies a file of kumar, then sharma will become its owner and he can manipulate the attributes.
• chown changing file owner and chgrp changing group owner

• On BSD, only system administrator can use chown

• On other systems, only the owner can change both

chown: Changing file owner


The command can be used as:-
chown options owner [:group] file(s)
chown transfers ownership of a file to a user, the command requires the user id of the
recipient followed by one or more filenames
Changing ownership requires super user permission, so use su command
$ su
Password: ******
#_
# ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30 note

Department of CS&E, AJIET Page 10


UNIX PROGRAMMING 18CS56- Module 2
#chown sharma note
# ls -l note
-rwxr----x 1 sharma metal 347 may 10 20:30 note
# exit //switches from superuser shell to login shell
$_

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..
chgrp: Changing group owner
• This command changes the file‘s group owner. No super user permission is required.

$ls –l dept.lst

-rw-r--r-- 1 kumar metal 139 jun 8 16:43 dept.lst

$chgrp dba dept.lst


ls –l dept.lst
-rw-r--r-- 1 kumar dba 139 Jun 8 16:43 dept.lst

Now the group owner is changed from metal to dba

THE SHELLS INTERPRETIVE CYCLE


Shell acts as both a command interpreter as well as a programming facility.
2.6 THE SHELL AND ITS INTERPRETIVE CYCLE

⚫ When you log on to a unix machine , you first see a prompt. Even though it may appear that
the system is idling, a UNIX command is running at the terminal. This command is the shell.
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.

⚫ When a command is keyed in, it goes as input to the shell. The shell scans for metacharacters.
After pre-processing, the shell passes on the command line to the kernel for ultimate
execution. After the execution, the shell once again issues the prompt to take up your next
command

The following activities are typically performed by the shell in its interpretive cycle:
Department of CS&E, AJIET Page 11
UNIX PROGRAMMING 18CS56- Module 2

• 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 meta characters 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.

2.7 WILD-CARDS
A pattern is framed using ordinary characters and a meta character (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 meta characters that are used to construct the generalized
pattern for matching filenames belong to a category called wild-cards. The following table lists
them:

Wild-card Matches

* Any number of characters including none

? A single character

[ijk] A single character – either an i, j or k

[x-z] A single character that is within the ASCII range of characters x and z [!ijk] A

single character that is not an i, j or k (Not in C shell)

[!x-z] A single character that is not within the ASCII range of the characters x and z (Not in C Shell)

{pat1,pat2...} Pat1, pat2, etc. (Not in Bourne shell)

The * and ?

⚫ To list all files that begin with chap, use,


$ls chap*
chap01 chap03 chap15 chapx chapy
⚫ The metacharacter * matches any number of characters including none. When shell
encounters this command line, it identifies the * immediately as a wild card. It then looks
into the current directory and recreates the command line from the filenames that match
pattern chap*

Department of CS&E, AJIET Page 12


UNIX PROGRAMMING 18CS56- Module 2
⚫ The next wild character is ? Which matches a single character
$ls chap?
chapx chapy
⚫ It matches all five character filenames beginning with chap

⚫ To list all files whose filenames are six character long and start with chap, use ,
$ls chap??
chap01 chap03 chap15
Matching the dot
⚫ 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 .???*
.bash_profile .exrc .netscape
⚫ However, if the filename contains a dot anywhere but at the beginning, it need not be matched
explicitly.
$ ls emp*lst
emp.lst emp1.lst emp221lst emp2.lst
⚫ 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


⚫ 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]
chap01 chap02 chap04
⚫ Range specification is also possible inside the class with a hyphen (-)
$ls chap[x-z] - Matches chapx, chapy, chapz and lists if found. $ls chap0[1-
4] - Matches chap01, chap02, chap03 and chap04
and lists if found

Negating the character class(!)


⚫ You can negate a character class to reverse 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 pattern


⚫ This feature, not available in Bourne shell, enables us to match totally dissimilar pattern.
Department of CS&E, AJIET Page 13
UNIX PROGRAMMING 18CS56- Module 2

⚫ To copy all the c and java source program from another directory, Delimit the pattern with
comma, and then put ({}) curly braces around them (no space).
cp $HOME/prog_sources/*.{c,java}
⚫ To access multiple directories
cp home/kumar/{projects,html,scripts}
these copies all the three files from directories to the current directory(home/kumar/).
Rounding up
⚫ Some of the wild-card character have different meaning depending on where they are placed
in the pattern.
⚫ The * and ? loose their meaning when they are inside the class, and are matched literally.
Similarly, - and ! Also lose their significance when placed outside the class.
⚫ To summarize sample set of command line presented below:
ls *.c lists all files with .c extension
cp foo foo* copies foo to foo*(* loses meaning here)
cp ???? progs copies all files with 4 character names to
progs directory

2.8 REMOVING THE SPECIAL MEANINGS OF WILD CARDS


The two methods are escaping and quoting

⚫ Escaping - providing a \ (backslash) before the wild-card to remove (escape) its special
meaning.

⚫ Quoting - enclosing the wild-card, or even the entire pattern, within quotes.

2.8.1 Escaping

⚫ 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*

⚫ 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\]
Escaping the space
⚫ A filename can contain a whitespace character also. Hence to remove a file named My
Document.doc
$ rm My\ Document.doc

Department of CS&E, AJIET Page 14


UNIX PROGRAMMING 18CS56- Module 2

⚫ Documend.doc, which has a space embedded, a similar reasoning should be followed.

Escaping the \ itself

⚫ Sometimes it is required to interpret the \ itself literally. You need another \ before it.

2.8.2 Quoting
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 files chap*


$rm “My Document.doc” //Removes file My Document.doc
Single quotes protects all special characters.Double quotes are more permissive, they don’t protect $
and `(backquote). They interpret a pair of backquotes (``) as command substitution and $ as a
variable prefix
Example 1:-
$ echo “Command substitution uses `` while TERM is evaluated using
$TERM” Command substitution uses while TERM is evaluated using vt100

Here the pair of `` evaluated as null command and $TERM as vt100 inside double quotes
Example 2:-

$ echo ‘ Command substitution uses `` while TERM is evaluated using


$TERM’ Command substitution uses `` while TERM is evaluated using
$TERM

Escaping with echo

⚫ We used echo with escape sequence like \n and \t.

⚫ The \ has a reverse meaning there, it treats the character n and t as special rather than remove
their special meanings.

⚫ Because n and t don’t have special meaning.

⚫ These escape sequence is always used within the quotes to keep the shell out.
2.9 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,

Department of CS&E, AJIET Page 15


UNIX PROGRAMMING 18CS56- Module 2

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.
Standard input:

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.

When you use wc without an argument and have no special symbol like the < and | in the
command line, wc obtains its input from the default source. This input has to be provided from
the keyboard and mark the end of input.

$wc

Standard input can be redirected

It can come from a file

Or a pipeline

[ctrl –d]

3 14 71

Shell can reassign the standard input file to a disk file. This means it can redirect the standard input
to originate from the file on the disk. This reassignment or redirection requires the < symbol.

$wc < sample.txt

3 14 71

Execution of Command: $wc < sample.txt


⚫ On seeing the <, the shell opens the this file, sample.txt, for reading.

Department of CS&E, AJIET Page 16


UNIX PROGRAMMING 18CS56- Module 2

⚫ It unplugs the standard input file from its default source and assigns it to sample.txt ⚫ wc

reads from standard input which has earlier been reassigned by the shell to sample.txt. Taking

input from both file and Standard Input:

⚫ When command takes input from multiple sources- say file and standard input, the – symbol
must be indicate the sequence of taking input. The meaning of the following sequence should
be quite obvious:

cat - foo // first from standard input and then from foo

cat foo - bar // first from foo and then standard input and then bar

Standard Output

All commands displaying output on the terminal actually write to the standard output file as a stream
of character, and no directly to the terminal. 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.

The shell can effect the redirection of this stream when its sees the > or >> symbols in the command
line. You can replace the default destination (the terminal) with any file by using >(right chevron)
operator, followed by the filename:

$ wc sample.txt > newfile

$cat newfile

3 14 71 sample.txt

The first command sends the word count of sample.txt to newfile; nothing appears on the terminal
screen. If the output file doesn’t exit, the shell creates it before executing the command.If it exist, the
shell overwrites it, so use this operator with caution. The shell also provides the >> symbol to
append the file:

$wc sample.txt >> newfile // doesn’t disturb exiting


Execution of Command: $wc sample.txt >newfile

Department of CS&E, AJIET Page 17


UNIX PROGRAMMING 18CS56- Module 2

⚫ On seeing the >, the shell opens the disk file, newfile, for writing.

⚫ It unplugs the standard ouput file from its default destination and assigns it to newfile.

⚫ wc opens the file sample.txt for reading.

⚫ wc writes to standard output which has earlier been reassigned by the shell to newfile.

Standard error

Each of the 3 standard files is represented by a number called file descriptor.A file is opened by
referring to its pathname, but subsequent read and write operations identify the file by this file
descriptor.The kernel maintains a table of file descriptors for every process running in the system

The first 3 slots are allocated to three standard streams:-

0- standard input

1- standard output

2– standard error

We need to explicitly use one of these descriptors when handling the standard error stream. When an
incorrect command is entered or if we try to open a non existing file, certain diagnostic messages are
shown on the screen.This is the standard error stream whose default destination is the terminal

$ cat foo

cat : cannot open foo

Here cat fails to open the file and writes to standard error

Standard error cannot be redirected in the same way of standard output, even though both uses
terminal as the default destination .Redirecting standard error requires the use of 2> symbols

$ cat foo 2> errorfile

cat errorfile

cat : cannot open foo

We can also append diagnostic output


cat foo 2>> errorfile

Department of CS&E, AJIET Page 18


UNIX PROGRAMMING 18CS56- Module 2
Filters: Using Both Standard Input And Standard Output

All commands don’t use the features of standard input and standard output.In this viewpoint UNIX
commands can be grouped into four categories viz.

• 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.
• Commands like ls, pwd, who etc. don’t read standard input but they write to standard output. •

Commands like lp that read standard input but don’t write to standard output. • Commands like
cat, wc, cmp etc. that use both standard input and 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
$ cat calc.txt
2^32
25*50
30*25 + 15^2
We can redirect bc’s standard input to come from this file and save the output in yet another
$ bc < calc.txt > result.txt

$ cat result.txt
4294967296
1250
975
bc obtained the expression from redirected standard input, processed them and sent out the result to a
redirected output stream.Though we used < first and then >, there is no restriction in the sequence
we use or in the use of whitespaces around it.
bc > result.txt < calc.txt
bc>result.txt<calc.txt
Department of CS&E, AJIET Page 19
UNIX PROGRAMMING 18CS56- Module 2

2.10 CONNECTING COMMANDS

Pipes
With piping, the output of a command can be used as input (piped) to a subsequent command.
$ command1 | command2
⚫ For example :- who command produces a list of users

$who root console aug 1 07:51 (:0)

kumar pts/10 aug 1 02:51 (:0)

sharma pts/6 aug 1 03:51 (:0)

rajath pts/8 aug 1 06:51 (:0)

In order to count the number of lines in the above output, we need to redirect this output to a file
$ who > user.txt
Now wc command is used to count the number of lines only
$wc -l < user.txt
5
Using an intermediate file(user.txt) , we counted the number of users. The 2 disadvantages of using
intermediate file are:-
• Process can be slow, second command cant act unless the fist has completed its job •
Intermediate file has to be removed after completion of the job
The shell can connect these streams using a special operator, the |(pipe) and avoid the creation of
disk file.
$ who | wc –l
5 //no intermediate file is created
Here the output of who has been passed directly to the input of wc, and who is said to be piped to wc
.
Examples
$ ls -l | wc –l Displays number of file in current directory $ who | wc –l
Displays number of currently logged in users

When a command needs to be ignorant :-


If we wish to find the total size of all c program files in current directory, we can use the command:
$wc –c *.c
However it also shows the usage of each file which we are not interested, so we need to make wc
ignorant of its input source, so feed the concatenatedoutput stream of all the.c files to wc –c as its
input
$ cat *.c|wc -c

Department of CS&E, AJIET Page 20


UNIX PROGRAMMING 18CS56- Module 2

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
root console aug 1 07:51 (:0)
kumar pts/10 aug 1 02:51 (:0)
sharma pts/6 aug 1 03:51 (:0)
Above command displays currently logged in users on standard output and writes a copy to users.lst

2.11 BASIC & EXTENDED REGULAR EXPRESSION

2.11.1 grep : SEARCHING FOR A PATTERN


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.

⚫ Example 1:- display the contents of file emp.lst

⚫ Now to search the pattern sales in emp.lst file

Department of CS&E, AJIET Page 21


UNIX PROGRAMMING 18CS56- Module 2

⚫ Grep is also a filter, it can search its standard input for the pattern, and saves the standard
output in a file
$ who | grep kumar> foo
⚫ When grep is used with multiple filenames it displays the filenames along with the output

gr
ep options:
The below table shows all the options used by grep.

Option 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 Matches multiple patterns

-f filename Takes patterns from file, one per line

-E Treats patterns as an ERE

-F Matches multiple fixed strings

Ignore case (-i option)

⚫ Used to search all lines containing a pattern regardless of uppercase and lowercase distinction

Deleting lines(-v)

The –v option selects all lines except those containing the pattern

Department of CS&E, AJIET Page 22


UNIX PROGRAMMING 18CS56- Module 2

The lines containing the pattern director are deleted in the output

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.

Dislayingfilenames(-l)
The –l option displays only the names of the files containing the pattern. Here the pattern manager is
searched in all files ending with .lst (*.lst)

Matching multiple patterns(-e)


By using –e option, you can match multiple patterns

2.11.2 BASIC REGULAR EXPRESSION


If an expression uses metacharacters it is termed as regular expression.POSIX identifies regular
expression as belonging to 2 categories- basic and extended.Grep supports basic regular

Department of CS&E, AJIET Page 23


UNIX PROGRAMMING 18CS56- Module 2

expressions(BRE) by default and extended regular expression with –E option. The below table
shows the BASIC REGULAR EXPRESSION(BRE) character set

The character class


A Regular expression lets you specify a group of characters enclosed within a pair of rectangular
brackets, [ ], in which case the match is performed for a single character in the group.
$ grep '[aA]g[ar][ar]wal' emp.lst
3564|sudhir Agarwal |executive |personnel |06/07/47|7500
0110|v. k. agrawal |g. m. |marketing |31/12/40|9000
[aA]- matches A or a
[ar]- matches a or r
Negating a class(^)
Regular expressions use the caret(^) to negate the character class, while the shell uses bang(!)
Ex: [^a-zA-Z] matches a non-alphabetic character

The *
The * (asterisk) refers to the immediately preceding character.
Here, it indicates that the previous character can occur many times, or not at all.
$ grep '[aA]gg*[ar][ar]wal' emp.lst

2476|anil aggarwal |manager |sales |01/05/59|5000

Department of CS&E, AJIET Page 24


UNIX PROGRAMMING 18CS56- Module 2
3564|sudhir Agarwal |executive |personnel |06/07/47|7500
0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

The Dot
A . matches a single character. The pattern 2... matches a four-character pattern beginning with a 2.
The shells equivalent pattern is 2???. The pattern .* matches any number of characters, or none.
$ grep 'j.*saxena' emp.lst
2345 |j. b. saxena |g. m. |marketing |12/03/45|8000

Specifying pattern locations ( ^ and $ )


Anchoring a pattern is necessary when it can occur in more than one place in a line and you are
interested in its occurrence only at a particular location.
The 2 characters that are used are:-
^ (carat) – For matching at the beginning of a line
$ (dollar) – For matching at the end of a line
Ex 1:- To extract those lines where emp-id begins with a 2
$ grep '^2' emp.lst
2233|a. k. shukla |g. m. |sales |12/12/52|6000
2365|barun sengupta |director |personnel |11/05/47|7800
2476|anil aggarwal |manager |sales |01/05/59|5000
2345|j. b. saxena |g. m. |marketing |12/03/45|8000

Ex 2:-To select the lines where salary is between 7000 and 7999
$ grep '7...$' emp.lst
9876|jai sharma |director |production |12/03/50|7000
2365|barun sengupta |director |personnel |11/05/47|7800
3564|sudhir Agarwal |executive |personnel |06/07/47|7500

Ex 3:-To display all lines that don't begins with a 2


$ grep '^[^2]' emp.lst

Department of CS&E, AJIET Page 25


UNIX PROGRAMMING 18CS56- Module 2
9876|jai sharma |director |production |12/03/50|7000
5678|sumit chakrobarty |d. g. m. |marketing |19/04/43|6000
5423|n. k. gupta |chairman |admin |30/08/56|5400
1006|chanchal singhvi |director |sales |03/09/38|6700

2.11.3 EXTENDED REGULAR EXPRESSION(ERE) AND egrep


ERE make it possible to match dissimilar patterns with a single expression. grep uses ERE
characters with -E option. egrep is another alternative to use all the ERE characters without -E
option.
This ERE uses some additional characters set shown in below table

The + and ?

• + - Matches one or more occurrences of the previous character

• ? - Matches zero or one occurrence of the previous character.

$ grep -E “[aA]gg?arwal” emp.lst


2476|anil aggarwal |manager |sales |01/05/59|5000

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

Here gg? Restricts the expansion to one or two gs only

Incase for a string like #include<stdio.h>, if we don’t know how many spaces to give , use the
following expression:

#include +<stdio.h>

# ?include +<stdio.h>

Department of CS&E, AJIET Page 26


UNIX PROGRAMMING 18CS56- Module 2
Matching multiple patterns( |, ( and ) )

⚫ The | is the delimiter for the multiple patterns

⚫ To locate sengupta and dasgupta without using –E twice:-

$ grep -E 'sengupta|dasgupta' emp.lst


2365|barun sengupta |director |personnel |11/05/47|7800
1265|s. n. dasgupta |manager |sales |12/09/63|5600
The characters ( and ), lets you group patterns and when you use the | inside paranthesis, it makes a
compact pattern.
$ grep -E '(sen|das)gupta' emp.lst
2365|barun sengupta |director |personnel |11/05/47|7800
1265|s. n. dasgupta |manager |sales |12/09/63|5600

SHELL PROGRAMMING
• A shell script contains a list of commands which have to be executed regularly. Shell script is
also known as shell program. The user can execute the shell script itself to execute
commands in it.
• A shell script runs in interpretive mode. i.e. the entire script is compiled internally in memory
and then executed. Hence, shell scripts run slower than the high-level language
programs.".sh" is used as an extension for shell scripts.
• Example: A shell script (program1.sh) to execute few commands.

#!/bin/sh
echo “Welcome to Shell Programming” # print message
echo “Today’s date : `date`” # print date
echo “My Shell :$SHELL” # print shell name

• The hash symbol # indicates the comments in the script. The shell ignores all the characters
that follow the # symbol. However, this does not apply to the first line.
• The first line "#! /bin/sh" indicates the path where the shell script is available.

There are 2 ways to execute a shell script:

1) Execute Shell Script Using File Name

• By default, script is not executable. So, the chmod command can be used to make the script
executable.

Department of CS&E, AJIET Page 27


UNIX PROGRAMMING 18CS56- Module 2

• The scripts are executed in a separate child shell process.

• The child shell reads and executes each statement in interpretive mode.

Run:
$ chmod +x program1.sh // add executable permission
$ program1.sh // execute the script program1.sh
Output:
Welcome to Shell Programming
Today’s date: Mon Nov 4 11:02:45 IST 2017
My Shell: /bin/sh
2) Execute Shell Script by Specifying the Interpreter
The user can also execute a shell script by specifying the interpreter in the command line. Here,
the script neither requires a executable permission nor an interpreter line.
Run:
$ sh program1.sh //Execute using sh interpreter
$ bash program1.sh //Execute using bash interpreter
Output:
Welcome to Shell Programming
Today’s date: Mon Nov 4 11:02:45 IST 2017
My Shell: /bin/sh

2.12 ORDINARY AND ENVIRONMENT VARIABLES


Shell variables are of two types:
• Environment variables

• ordinary variable

Environment variables

• Environment variable Environment variables control the behavior of the system. They
determine the environment in which user work. If environment variables are not set properly,
the users may not be able to use some commands.
• Environment variables are so called because they are available in the user's total environment

i.e. the sub-shells that run shell scripts and mail commands and editors. • Some variables are set
by the system, others by the users, others by the shell programs.

Department of CS&E, AJIET Page 28


UNIX PROGRAMMING 18CS56- Module 2

• env command can be used to display environment variables.

• For example:

$ env
HOME=home/kumar
IFS=' '
LOGNAME=kumar
MAIL= /var/mail/kumar
MAILCHECK=60
PATH=/bin:/usr/bin
PS1='$'
PS2='>'
SHELL=/usr/bin/bash
TERM= tty1

• HOME:- This variable indicates the home directory of the current user.This variable is set for
a user by the system admin in /etc/passwd.
• IFS:-This variable contains a string of characters that are used as word separator in the

command line. The string normally consists of the space, tab and newline characters. •
LOGNAME:- This variable shows the username.
• MAIL:-This variable specifies the path to user’s mailbox.

• MAILCHECK:-This variable determines how often the shell checks the file for the arrival of
new mail.
• PATH:-This variable specifies the locations in which the shell should look for commands.
Usually, the PATH variable can be set as follows:
o $PATH=/bin:/usr/bin

• PS1 and PS2:- The shell has 2 prompts:


o The primary prompt $ is the one the user normally sees on the monitor. $ is stored in
PS1.The user can change the primary prompt asfollows:
o $ PS1="C>"
o C> //similar to windows
o The secondary prompt > is stored in PS2.

• SHELL:-This variable specifies the current shell being used by the users.Different types of
shells are: Bourne shell /bin/sh
▪ C-shell /bin/csh

▪ Korn shell /bin/ksh


This variable is set for a user by the system admin in /etc/passwd.

Department of CS&E, AJIET Page 29


UNIX PROGRAMMING 18CS56- Module 2

• TERM:-This variable indicates the terminal type that is used.Every terminal has certain
characteristics that are defined in a separate control file in the terminfo directory. If TERM is
not set correctly, vi will not work and the display will be faulty

Ordinary Variable
A variable is a character string to which the user assigns a value.The value assigned can be a
number, text, filename, device, or any other type of data.
Syntax:
variable = value // variable definition
The value of variables are stored in the ASCII format.
For example:
$ x=50
$ echo $x //displays 50
In command line, all words that are preceded by a $ are identified and evaluated as variables. A
variable can be removed with unset and protected from reassignment by readonly. Both are shell
internal commands.
$ set count=5
$ readonly size = 10
The variables exist only for a short time during the execution of a shell script. As the variables are
defined and used by specific users, they are also called user-defined variables.
Uses of Local variables
• Setting pathnames: If a pathname is used several times in a script, we can assign it to a variable
and use it as an argument to any command.
• Using command substitution: We can assign the result of execution of a command to a
variable. The command to be executed must be enclosed in backquotes.
• Concatenating variables and strings: Two variables can be concatenated to form a new variable.

Example:
$ base=foo ; ext=.c
$ file=$base$ext
$ echo $file // prints foo.c

Department of CS&E, AJIET Page 30


UNIX PROGRAMMING 18CS56- Module 2

2.13 THE .PROFILE FILE


A profile file is a start-up file of an UNIX user. This file gets executed as soon as the user logs in.
This file is a shell script that will be present in the home directory of each user.The system admin
provides each user with a profile with a minimum working environment. However, the user can
customize the profile as per their requirement.i.e. The user can

→ assign suitable values to the environment variables.

→ add and modify statements in the profile file.

This file can be any one of the two:

• A specific file for each individual user with responsibility for the user environment. •
A universal file for all users with responsibility for the general environment.
The user can view his ".profile" as follows:

2.14 Read & readonly Command


Read command

• read command can be used for taking input from the keyboard.It is shell’s internal tool for
making scripts interactive.
• Syntax:

▪ read var_name
• It is used with one or more variables.The variables are used to hold inputs given with the
standard input.

• Example: A shell script (program4.sh) to read a name and print it

Read
Only

Department of CS&E, AJIET Page 31


UNIX PROGRAMMING 18CS56- Module 2

Readonly command can be used to make variables readonly, the user cannot change the value of
variables.
Syntax : variable =value
Example:-
$ readonly pi=3.14
$ echo $pi //displays 3.14
$ pi=6.12 //this will result in error

2.15 COMMAND LINE ARGUMENTS


Shell script also accepts arguments from the command line. They can, therefore, run non interactively
and be used with redirection and pipeline---s. When arguments are specified with a shell script, they
are assigned to positional parameters.
The shell uses following parameters to handle command line arguments

Shell parameter Significance

$# Number of arguments specified in command line

$0 Name of executed command (filename of the current script)

$1, $2, … Positional parameters representing command line arguments

$* Complete set of positional parameters as a single string

“$@” Each quoted string is treated as a separate arguments, same as $*

$? Stores the exit status of the last command that was executed.
$$ Stores PID of the current shell

$! Stores PID of the last background job

Example: A shell script to read and display various shell parameters from the command line.
#!/bin/sh

echo "Total Number of Parameters : $#"


echo "File Name: $0"

Department of CS&E, AJIET Page 32


UNIX PROGRAMMING 18CS56- Module 2
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $*"
echo "Quoted Values: $@"
$echo "Exit value: $?"
echo "PID of current shell: $$"
OUTPUT:-

2.16 exit and exit status of a command


C program and shell scripts have a lot in common, and one of them is that they both use the same
command ( or function in c ) to terminate a program. It has the name exit in the shell and exit( ) in C.
The command is usually run with a numeric arguments:
• exit 0 #Used when everything went fine

• exit 1 #Used when something went wrong


The shell offers a variable $? and a command test that evaluates a command's exit status. 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.
Examples:
$ grep director emp.lst>/dev/null; echo $?
0 #Success
$ grep director emp.lst>/dev/null; echo $?
1 #Failure – in finding pattern
Department of CS&E, AJIET Page 33
UNIX PROGRAMMING 18CS56- Module 2

2.17 Logical operators for conditional execution Two


logical operators can be used for conditional execution: 1) && and 2) ||
1) && Operator
Syntax:
cmd1 && cmd2
• Here, cmd2 gets executed only when cmd1 succeeds.
2) || Operator
Syntax:
cmd1 || cmd2
• Here, cmd2 gets executed only when cmd1 fails.
Examples:
•$ date && echo “Date Command Executed Successfully!”
Sun Jan 13 15:40:13 IST 2013

Date Command Executed Successfully!

•$ grep 'director' emp.lst && echo “Pattern found in File!”


1234 | Henry Ford | director | Marketing | 12/12/12|25000
Pattern found in File!

•$ grep 'manager' emp.lst | | echo “Pattern not-found inFile!”


Pattern not-found in File!

Department of CS&E, AJIET Page 34


UNIX PROGRAMMING 18CS56- Module 2
2.18 The Test Command and Its Shortcut
When you use if to evaluate expressions, you need the test statement because the true or false values
returned by expression's can't be directly handled by if. test uses certain operators to evaluate the
condition on its right and returns either a true or false exit status, which is then used by if for making
decision.
test works in three ways:

• Compares two numbers

• Compares two strings or a single one for a null value.

• Checks a file's attributes


test doesn't display any output but simply sets the parameter $?.

Numeric Comparison
Numerical Comparison operators used by test:

Operator Meaning

-eq Equal to

-ne Not equal to

-gt Greater than

-ge Greater than or equal to

-lt Less than

-le Less than or equal to


The numerical comparison operators used by test always begins with a - (hyphen), followed by a
two-letter string, and enclosed on either side by whitespace.
Example : a shell script to find relationship between 2 numbers
#!/bin/usr
$ x=5, y=7
$ test $x -eq $y; echo $?
$ test $x -lt $y; echo $?
$ test $x -ne $y; echo $?
$ test $x -ge $y; echo $?

Department of CS&E, AJIET Page 35


UNIX PROGRAMMING 18CS56- Module 2

output
1 # x Not Equal to y
0 #True; 5<7
0 #true 5!=7
1 #false
SHORTHAND FOR TEST :-
A pair of rectangular brackets enclosing the expression can replace the term test. Hence, above
example may be re-written as
test $x -eq $y
or
[ $x -eq $y ] #Both are equivalent
Provide whitespace around the operators, operands and inside the [ and ].

String Comparison
Test can be used to compare strings with yet another set of operators. The below table shows string
tests used by test

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 a null string
stg String stg is assigned and not a null string
s1 == s2 String s1 = s2 (Korn and Bash only)
Example:-a shell script to check whether 2 strings are equal
#!/bin/sh
echo “Enter the first string: \c”
read str1
if [ -z “$str1” ] ; then
echo “You have not entered the string”; exit 1
echo “Enter the second string: \c”
read str2

Department of CS&E, AJIET Page 36


UNIX PROGRAMMING 18CS56- Module 2 if [ -z “$str2” ] ; then
echo “You have not entered the string”; exit 1
if[ $str1= $str2]
then
echo "Both strings are equal"
else
echo "Strings are unequal"
File Tests

• Test command can be used to check various file attributes such as file type (-, d or l) & file permission (r,
w, x).

Department of

CS&E, AJIET Page 37


UNIX PROGRAMMING 18CS56- Module 2

Output :-

2.19 The If
The if statement makes two-way decisions depending on the fulfilment of a certain condition. In the
shell, the statement uses the following forms
if command is successful then execute commands If command is successful then
execute commands else fi execute commands fi
If command is successful then execute commands elif command is execute commands else
successful then execute commands fi
Form 1 Form 2 Form 3

Example: A script to check whether an integer is positive or negative.


#!/bin/sh
echo “Enter any non zero integer: \n”
read num
if [$num -gt 0];
then
echo “Number is positive number”
else
echo “Number is negative number”
fi
Output:
Enter any non zero integer: 5
Number is positive number

Department of CS&E, AJIET Page 38


UNIX PROGRAMMING 18CS56- Module 2
2.20 THE case CONDITIONAL
The case statement is similar to switch statement in C. The statement matches an expression for
more than one alternative, and uses a compact construct to permit multi-way branching. case also
handles string tests The general syntax of the case statement is as follows:
case expression in
pattern1) command1 ;;
pattern2) command2 ;;
pattern3) command3 ;;
…........
esac
Here is how it works:
Firstly the expression is matched with pattern1.If the match succeeds, then statement1 will be
executed. If the match fails, then the expression is matched with pattern2 and this process continues.
• Each statement is terminated with a pair of semicolon (;;).
• This can match only strings but cannot handle numeric and file tests.
However, this can also handle numbers but treating them as strings.
Example:
A script to display appropriate message based on grades (A to D)
#!/bin/sh
echo “enter grade A to D \n”
read grade
case “$grade” in
A) echo “Excellent!” ;;
B) echo “Well done” ;;
C) echo “You passed” ;;
D) echo “Better try again” ;;
*) echo “Invalid grade” ;;
esac
echo “Your grade is $grade”
output

Department of CS&E, AJIET Page 39


UNIX PROGRAMMING 18CS56- Module 2

Matching Multiple Patterns


• case statement can also specify the same action for more than one
pattern. • Example: A script to test a user response for both y and Y (or n
and N). #!/bin/sh

echo “Do you wish to continue? [y/n]:”


read ans
case “$ans” in
Y | y ) ;;
N | n ) exit ;;
esac
Wild-Cards
• case statement has a superb string matching feature that uses wild-
cards. • case statement uses

→ filename matching meta-characters * and ?

→ string matching character.

• Example: A script to test a user response for YES, yes, Yes, yEs (or no, NO, No, nO).
#! /bin/sh
echo “Do you wish to continue? [y/n]:”
read ans
case “$ans” in
[Yy] [eE]* ) ;; # Matches YES, yes, Yes, yEs, etc
[Nn] [oO] ) exit ;; # Matches no, NO, No, nO
* ) echo “Invalid Response”
esac

Department of CS&E, AJIET Page 40


UNIX PROGRAMMING 18CS56- Module 2
2.21 expr: COMPUTATION AND STRING HANDLING
Shell does not have any computing features, but it rely on external expr command for that purpose.
This expr command combines two functions in one:

• Performs arithmetic operations on integers


Manipulates strings
Computation
expr can perform the four basic arithmetic operations as well as the modulus (remainder) function.
Examples:

Examples:
$ expr 3 + 5 //outputs 8
$ x=8 y=4
$ expr $x + $y //outputs 12
$ expr $x - $y //outputs 4
$ expr $x \* $y //32 Asterisk(*) has to be escaped to prevent from metacharacter $
expr $x / $y //outputs 2

$ expr $x % $y // outputs0
expr is often used with command substitution to assign a variable.
For example, you can set a variable z to the sum of two numbers:
$ x=6 y=2; z=expr `$x + $y`
$ echo $z
8
String Handling
For manipulating strings, expr uses two expressions separated by a colon.
The string to be worked upon is placed on the left of the colon (:) and regular expression is placed on
its right. Depending on the composition of the expression, expr can perform three important string
functions:
• Determine the length of the string

• Extract a substring
• Locate the position of a character in a string

Department of CS&E, AJIET Page 41


UNIX PROGRAMMING 18CS56- Module 2

i) Length of the String

The regular expression ".*" is used to print the number of characters matching the pattern.

Syntax: expr "string" : ".*"

• Example:

$ expr “vtunotesbysri” : ‘.*’ // outputs 13

ii) Extracting a Substring

expr command can be used to extract a string enclosed by the escape characters "\(" and"\)".

Syntax: expr "string" : "\( substring \)"

• Example:

$ expr “vtunotesbysri” : " \( sri \)" // outputs 'sri'

iii) Locating Position of a Character

expr command can be used to find the location of the first occurrence of a character inside a

string. Syntax: expr "string" : "[^ch]*ch" //ch → character

• Example:
$ expr “vtunotesbysri” : "[^u]*u" // outputs 3

2.22 while statement


The general syntax of this command is as follows: It repeatedly performs a set of instructions until
the control commands returns a true exit status.
Syntax :-
while condition is true
do
commands
done
The commands enclosed by do and done are executed repeatedly as long as condition remains

Department of CS&E, AJIET Page 42


UNIX PROGRAMMING 18CS56- Module 2
Example:
A script to display a message 3 times using while loop.
#!/bin/sh
num=1
while [$num -le 3]
do
echo " Welcome to Shell Programming "
expr $num = $num +1;
done

NOTE:

The shell also offers an until statement which operates with a reverse logic used in while. With until,
the loop body is executed as long as the condition remains false.

2.23 for: LOOPING WITH A LIST


Unlike while and until, for doesn't test a condition, but uses a list instead. The shell's for loop
differs in structure from the ones used in other programming language.
The general syntax of for loop is as follows
for variable in list
do
commands
done
• The loop body also uses the keywords do and done, but the additional parameters here are
variable and list.
• Each whitespace-separated word in list is assigned to variable in turn, and commands are
executed until list is exhausted.
Example :- A script to display elements of an array.
#! /bin/sh
print("Here are the numbers in the list: \n");
for var in 10 20 30 40 50 60;
do
echo “$var \t”
done
Department of CS&E, AJIET Page 43
UNIX PROGRAMMING 18CS56- Module 2

Possible sources of the list:-


1. List from variables:- a series of variables can be used in the command line. They are evaluated
by the shell before executing the loop
Example :-
for var in $PATH $HOME $MAIL ;
do echo”$var”;
done
2. List from command substitution:- command substitution can be used to create the list..the
following for command line picks up its list from the file clist
Example:- for file in `cat clist`
3. List from wild cards:- when the list consists of wild cards, the shell interprets them and used
for matching filenames
Example :-
for file in *.pdf
do
echo "Printing $file \n"
lp $file
done
4. List from positional parameters:- for is also used to process positional parameters that are
assigned from command line arguments
Example :-
for var in "$*"
do
echo "$var \t"
done

2.24The Set and Shift Commands And Handling Positional Parameters


set: Set the positional parameters set assigns its arguments to the positional parameters $1, $2 and
so on. This feature is especially useful for picking up individual fields from the output of a program.
$ set 98 23 62
Here, above line assigns , 98 to $1 , 23 to $2 ,62 to $3.
Example:
$ set `date` #Output of date command assigned to positional parameters $1, $2 & so on. $
echo $*

Sun Jan 13 15:40:13 IST 2013

Department of CS&E, AJIET Page 44


UNIX PROGRAMMING 18CS56- Module 2

$ echo “The date today is $2 $3 $6”


The date today is Jan 13 2013
shift: Shifting Arguments Left
shift transfers the contents of a positional parameters to its immediate lower numbered one.
This is done as many times as the statement is called. When called once, $2 becomes $1,$3
becomes $2 and so on
Example:
$ set `date`
$ echo $*
Sun Jan 13 15:40:13 IST 2013
$ echo $1 $2 $3
Sun Jan 13
$ shift #Shifts 1 place
$ echo $1 $2 $3
Jan 13 15:40:13
$ shift 2 #Shifts 2 places
$ echo $1 $2 $3
15:40:13 IST 2013
Set -- : Helps Command Substitution
• Problem with set command:
When set command is used with command substitution, the output of the command may begin with
a-(hypen). In this case, set command interprets -(hypen) as an option and does not work correctly.
For example:
$set 'ls -l student,lst'
-rwxr-xr--: bad option
• Solution: Use --(double hypen) immediately after set command.
$set -- 'ls -l student.lst'
-rwxr-xr-- 2 kumar group 163 Jul 13 21:36 student.lst

Department of CS&E, AJIET Page 45


UNIX PROGRAMMING 18CS56- Module 2

2.25 THE HERE(<< ) DOCUMENT


The << symbol can be used to read data from the same file containing the script. This file is called
here document. The term “here” signifies that data is here rather than in other file. Any command
using standard input can also take input from a here document.
Syntax:- command << delimiter
document
delimiter
example :-
$ mailx kumar<< MARK
Explore
Dream
Discover
MARK
The string MARK is delimiter. The shell treats every line delimited by MARK as input to the
command mailx.kumar at the other end will see 3 lines of message text with the date inserted by
command.The word MARK itself doesn’t show up.
Using Here Document with Interactive Programs:
• A shell script can be made to work non-interactively by supplying inputs through here
document. • For example:

$ wc -l << END
Decide
Commit
Succeed
END
3 //outputs number of lines = 3

2.26 TRAP COMMAND


Trap is a signal handler. Whenever the interrupt key is pressed, a signal SIGINT is sent to terminate
the shell script. Trap command can be used to perform clean up operations when a script receives a
terminate signal. This command is normally placed at the beginning of the shell script.
Syntax: trap command_list signal_list

Department of CS&E, AJIET Page 46


UNIX PROGRAMMING 18CS56- Module 2
Two common uses of trap:-

• Clean up temporaray files


The user can remove some files and then exit if someone tries to abort the script from the
terminal
$ trap ‘rm temp.txt ; exit’ SIGINT
Here, a file temp.txt will be automatically removed if a signal SIGINT is received by the
script.

• Ignore signals

A script can be made to ignore a specific signal by using a null command list
Ex:- trap ‘ ‘ SIGINT

Here, the script ignores a signal SIGINT when it is received.


..........................................................................................................................................................

SHELL SCRIPT EXAMPLES


1.Write a shell script to find the sum of n given number using for loop.
#!/bin/sh
sum=0
for i in $*
do
sum=`expr $sum + $i`
done
echo “The sum is $sum”

2. Shell script that accepts two file names as arguments, checks if the permissions for these
files are identical and if permissions are identical outputs the common permission,
otherwise outputs each file name followed by its permissions

#!/bin/bash
echo "enter the filenames:"
read f1 f2
file1=`ls -l $f1|cut -c 1-10`

Department of CS&E, AJIET Page 47


UNIX PROGRAMMING 18CS56- Module 2

file2=`ls -l $f2|cut -c 1-10`


if [ $file1 == $file2 ]
then
echo "common file permission: $file1"
else
echo " permission of $f1: $file1"
echo " permission of $f2: $file2"
fi
3.A shell script to print first 10 numbers (1 to 10)
#!/bin/sh

x=0
while [$x –le 10];
do
echo “$x \t”
x=`expr $x+1`
done
4.Shell script to check the regular files in a current directory
#!/bin/sh

for i in ~/*
do
if [ -f $i ]
then
echo " $i is a regular file "
else
echo " $i is not a regular file "
fi
done

Department of CS&E, AJIET Page 48


UNIX PROGRAMMING 18CS56- Module 2
5.A shell script (program5.sh) to accept any number of arguments and print them in a reverse
order. For example if A B C are entered then output is C B A.
#!/bin/bash
n=$#
if [ $n -lt 2 ];
then
echo "please enter 2 or more arguments" exit
else
echo "The command line arguments in reverse order:"
while [ $n -ne 0 ]
do
eval echo "\$$n" #display values in positional parameters $3 $2 $1
n= `expr $n - 1`
done
fi
6. A shell script to create a menu, which displays the list of files, process status, current date
and current users of the system.

Departm
ent of CS&E, AJIET Page 49
UNIX PROGRAMMING 18CS56- Module 2
7. A shell script to read a pattern and filename from the terminal. And search for the pattern
in the file.
#! /bin/sh
echo “Enter the pattern to be searched: \c”
read pname
echo “Enter the file to be used: \c”
read fname
echo “Searching for pattern $pname from the file $fname”
grep $pname $fname
echo “Selected records shown above”
8. A shell script to compute sum of numbers passed in command line
#!/bin/sh
sum=0
for I in “$@”
do
sum =`expr $sum + $I`
done
echo “sum is $sum”
Questions
1. Explain while and for with syntax.
2. Explain ls-l Command with its attributes.
3. With example explain set and shift commands and handling positional parameters.
4. Explain shells interpretive cycle.
5. Explain grep, egrep and list its option with significance
6. Explain three standard files supported by unix and Also give details about special file used for
redirection in unix
7. Explain here document and trap command
8. Explain how to change file permission
9. What do you mean by wild cards
10. Write a shell script to find the sum of n given number using for and while
loop 11. Write a shell script to check whether the given number is paliandrome or
not

Department of CS&E, AJIET Page 50

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy