0% found this document useful (0 votes)
27 views

2.1 Control Flow - For: Who - Grep $1

The document discusses UNIX file permissions and the chmod command for changing permissions to make a file executable. It then explains how shell scripts and programs can be used interchangeably when a file is made executable. The document also covers shell parameters like $# and $0, using positional parameters, and provides examples of using a for loop to loop through arguments and search a file.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

2.1 Control Flow - For: Who - Grep $1

The document discusses UNIX file permissions and the chmod command for changing permissions to make a file executable. It then explains how shell scripts and programs can be used interchangeably when a file is made executable. The document also covers shell parameters like $# and $0, using positional parameters, and provides examples of using a for loop to loop through arguments and search a file.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

who | grep $1

then
sh wg fred

is equivalent to
who | grep fred

UNIX files have three independent attributes, read, write and execute. The UNIX command chmod (1) may be used to make a file executable. For example,
chmod +x wg

will ensure that the file wg has execute status. Following this, the command
wg fred

is equivalent to
sh wg fred

This allows shell procedures and programs to be used interchangeably. In either case a new process is created to run the command.

As well as providing names for the positional parameters, the number of positional parameters in the call is available as $#. The name of the file being executed is available as $0. A special shell parameter $* is used to substitute for all positional parameters except $0. A typical use of this is to provide some default arguments, as in,
nroff -T450 -ms $*

which simply prepends some arguments to those already given.

2.1 Control flow - for


A frequent use of shell procedures is to loop through the arguments ($1, $2, ...) executing commands once for each argument. An example of such a procedure is tel that searches the file /usr/lib/telnos that contains lines of the form
... fred mh0123 bert mh0789 ...

The text of tel is


for i do grep $i /usr/lib/telnos; done

The command
tel fred

prints those lines in /usr/lib/telnos that contain the string fred.


tel fred bert

prints those lines containing fred followed by those for bert.

The for loop notation is recognized by the shell and has the general form
for name in w1 w2 ... do command-list done

A command-list is a sequence of one or more simple commands separated or terminated by a newline or semicolon. Furthermore, reserved words like do and done are only recognized following a newline or semicolon. name is a shell variable that is set to the words w1 w2 ... in turn each time the command-list following do is executed. If in w1 w2 ... is omitted then the loop is executed once for each positional parameter; that is, in $* is assumed.

Another example of the use of the for loop is the create command whose text is

You might also like