7/29/2019 Adv Shellprg
1/22
More Shell Programming
Learning Objectives:
1. To learn the usage of environment (shell)variables in shell programming
2. To understand the handling of command line
arguments
3. To illustrate more useful commands in shellprogramming using examples
7/29/2019 Adv Shellprg
2/22
COMP111Lecture 8 / Slide 2
More Shell Programming
Table of Content Keyword Shell Variables
Keyword Example
Read-only Shell Variable
Command Line Argument Command for handling arguments - shift
Command for handling arguments - set
Special Variable - $$
Looping using for
Looping using forMore Examples
Return Status Example
7/29/2019 Adv Shellprg
3/22
COMP111Lecture 8 / Slide 3
Keyword Shell Variables
The shell sets keyword shell variables.
You can use (and change) them.
HOME The path to your home directory
PATH Directories where the shell looks for executables
USER Your login name
SHELL The name of the shell you are running
PWD The current working directoryPRINTER Can be loaded with your default printer
7/29/2019 Adv Shellprg
4/22
COMP111Lecture 8 / Slide 4
Keyword Example
$ cat env#!/bin/shecho "Hi $USER!"echo "Your home is: $HOME"echo "Your path is: $PATH"
echo "Your current directory is: $PWD"echo "Your shell is: $SHELL"echo "Your printer is: $PRINTER"$ envHi horner!Your home is: /homes/horner
Your path is:/usr/bin:.:.:/homes/horner/Unix/bin:...Your current directory is: /homes/horner/111Your shell is: /bin/cshYour printer is: csl3
7/29/2019 Adv Shellprg
5/22
COMP111Lecture 8 / Slide 5
Readonly Shell Variables
$0 is the name the user typed to invoke the shell script:
$ cat print1
#!/bin/shecho "This script is called $0"$ print1This script is called print1$ ./print1This script is called ./print1
$ ~/111/print1This script is called /homes/horner/111/print1
7/29/2019 Adv Shellprg
6/22
COMP111Lecture 8 / Slide 6
Command Line Arguments (1)
The command line arguments that you call a script withare stored in variables $1, $2, ..., $9.
$ cat args1
#!/bin/shecho "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9"$ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10The args are a1 a2 a3 a4 a5 a6 a7 a8 a9
With more than 9 arguments, they are still stored, butthey have to be moved using the shift command
before they can be accessed.
7/29/2019 Adv Shellprg
7/22
COMP111Lecture 8 / Slide 7
Command Line Arguments (2)
Example: How to write a command to swap two files?
$ cat swap#!/bin/shmv $1 /tmp/$1mv $2 $1mv /tmp/$1 $2$ cat it1contents of file1$ cat it2
contents of file2$ swap it1 it2$ cat it1contents of file2$ cat it2contents of file1
$
7/29/2019 Adv Shellprg
8/22
COMP111Lecture 8 / Slide 8
Command Line Arguments (3)
$* lists all the command line args:$ cat args2#!/bin/shecho "The args are $*"$ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
$# contains the number of args:$ cat args3#!/bin/shecho "The number of args is $#"$ args3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10The number of args is 10
7/29/2019 Adv Shellprg
9/22
COMP111Lecture 8 / Slide 9
Command Handling Argument
- shift (1) The shift command promotes each command line
argument by one (e.g., the value in $2 moves to $1,$3 moves to $2, etc.)
$ cat shiftargs#!/bin/shecho "The args are 0 = $0, 1 = $1, 2 = $2"shiftecho "The args are 0 = $0, 1 = $1, 2 = $2"shiftecho "The args are 0 = $0, 1 = $1, 2 = $2"shift$ shiftargs arg1 arg2 arg3The args are 0 = shiftarg, 1 = arg1, 2 = arg2The args are 0 = shiftarg, 1 = arg2, 2 = arg3The args are 0 = shiftarg, 1 = arg3, 2 =
The previous $1 becomes inaccessible
COMP111
7/29/2019 Adv Shellprg
10/22
COMP111Lecture 8 / Slide 10
Example: How to write a general version of the
swap command for two or more files?swap f1 f2 f3 ... fn_1 fn
f1
7/29/2019 Adv Shellprg
11/22
COMP111Lecture 8 / Slide 11
shift Example (1)
$ cat swap#!/bin/shorig1=$1mv $1 /tmp/$1while [ $2 ]do
mv $2 $1shiftdonemv /tmp/$orig1 $1$ cat it1 it2 it3contents of file1
contents of file2contents of file3$ swap it1 it2 it3$ cat it1 it2 it3contents of file2contents of file3
contents of file1
1st Sample Run
COMP111
7/29/2019 Adv Shellprg
12/22
COMP111Lecture 8 / Slide 12
shift Example (2)
$ swap it1 it2 it3$ cat it1 it2 it3contents of file3contents of file1contents of file2
$ swap it1 it2 it3$ cat it1 it2 it3contents of file1contents of file2contents of file3$ swap it1 it2
$ cat it1 it2contents of file2contents of file1
2nd Sample Run
3rd
Sample Run
4th Sample run
COMP111
7/29/2019 Adv Shellprg
13/22
COMP111Lecture 8 / Slide 13
The set command sets the args:$ cat set1#!/bin/shset yat yih saamecho "One is $1, two is $2, three is $3"
$ set1One is yat, two is yih, three is saam
The set command is useful for moving the output ofcommand substitution into the args:
$ date
Thu Feb 25 17:06:27 HKT 1999$ cat day#!/bin/shset `date`echo "Today is $3 $2 $6"$ day
Today is 25 Feb 1999
Command Handling Argument - set
COMP111
7/29/2019 Adv Shellprg
14/22
COMP111Lecture 8 / Slide 14
$$ is the process ID (PID) of the current process (theshell script PID, or the shell PID if interactive).
$ cat pid#!/bin/sh
echo $$$ pid1154$ pid1156$ pid
1157$ echo $$
892
$ ps
PID TTY TIME CMD
892 pts/0 0:01 csh
Special variable - $$ (1)
COMP111
7/29/2019 Adv Shellprg
15/22
COMP111Lecture 8 / Slide 15
It can be used for temporary file names:
$ cat swap
#!/bin/sh
file=/tmp/tmp$$mv $1 $file
mv $2 $1
mv $file $1
$ cat it1 it2
contents of file1contents of file2$ swap it1 it2$ cat it1contents of file2contents of file1
$
Special variable - $$ (2)
COMP111
7/29/2019 Adv Shellprg
16/22
COMP111Lecture 8 / Slide 16
The for statement executes a loop once for each of alist of possibilities:
$ cat printall#!/bin/shfor file in *do
if [ -f $file ]then
echo "Print $file [y/n]? "read respif [ $resp = "y" ]then
lpr -Pcll3 $file
fifidone$ printallPrint letter1 [y/n]?yPrint names [y/n]?
n
Looping using for Example 1
COMP111
7/29/2019 Adv Shellprg
17/22
COMP111Lecture 8 / Slide 17
If the in ___ part is omitted, it defaults to $*:
$ cat p#!/bin/shfor filedo
if [ -f $file ]then
lpr -Pcll3 $filefi
done$ p letter1 names$
Looping using for Example 2
COMP111
7/29/2019 Adv Shellprg
18/22
COMP111Lecture 8 / Slide 18
The in clause of the for statement accepts asmany parameters as you wish in many forms:
$ for i in 1 2 3 ; do echo $i ; done
12
3
$ for pid in `ps -a | tail +2 | cut -c1-6 | sort`
> do
> kill -9 $pid> done
kill: permission denied
kill: permission denied
kill: permission denied
(you will then be logout!)
Looping using for - Example 3
COMP111
7/29/2019 Adv Shellprg
19/22
COMP111Lecture 8 / Slide 19
We can make use of the find command to generate alist of files in a sub-directory tree for processing:
#!/bin/sh
if [ $# = 0 ]then
echo "usage: treeproc []"; exit
fi
pattern="$1"; shift
if [ $1 ] ; then cmd=$*; else cmd=echo; fi
echo "Running \"$cmd\" for $pattern in dir: `pwd`"for file in `find . -name "$pattern" -print`
do
$cmd $file
done $ find . -name letter* -print./secret/letter1
./secret/letter2
Looping using for Example 4
COMP111
7/29/2019 Adv Shellprg
20/22
COMP111Lecture 8 / Slide 20
$ ls -R.:names letter0 secret/
./secret:
letter1 letter2
$ treeproc
usage: treeproc []$ treeproc "*1"
Running "echo" for *1 in dir: /home/kwchiu
./secret/letter1
$ treeproc "letter*" rm -i
Running "rm -i" for letter* in dir: /home/kwchiu
rm: remove ./letter0 (yes/no)?rm: remove ./secrete/letter1 (yes/no)?
rm: remove ./secrete/letter2 (yes/no)?
$ find . -name "letter*" -exec rm -i {} \;
(same as the above)
Looping using for Example 4 testing
{} substitutes each filename
\ denotes end of the exec command
COMP111
7/29/2019 Adv Shellprg
21/22
COMP111Lecture 8 / Slide 21
Return Status $?
In UNIX, after a command (or your own program)finishes execution, it returns an exit code to its parent.The shell can retrieve this code with $?.
$ lsname
$ cat testif
if cat $1 >/dev/null 2>&1
then
echo "OK"
else
echo "Problem: Code" $?
fi
$ testif name
OK
$ testif abc
Problem: Code 2
$? returns the previousexit code
The construct >file 2>&1 is useful
for logging. Both the stdout andstderr are sent to the same file.
/dev/null is a special device dumps everything input to it
COMP111
7/29/2019 Adv Shellprg
22/22
COMP111Lecture 8 / Slide 22
Here documents