Title: Advanced UNIX BASH
1Advanced UNIX (BASH)
- Objectives
- explain how to write Bourne and Bash Shell scripts
2Overview
- 1. Making a File Executable
- 2. Combining Commands
- 3. Redirecting Output
- 4. Executing Scripts
- 5. Variables
- 6. Control Flow
continued
3- 7. Functions
- 8. Other Commands
- 9. Here Documents
- 10. Debugging
- 11. More Information
41. Making a File Executable
- cat whosondateecho Users currently logged
onwho - Wrong
- whosonwhoson Permission denied
5Right
- ls -lg whoson-rw-r--r-- 1 ad pubs 42 Jun 17
1055 whoson chmod ux whoson ls -lg
whoson-rwxr--r-- 1 ad pubs 42 Jun 17 1055
whoson - whosonTue Nov 7 132134 ICT 2000Users
currently logged inad consol Nov 7
0826jenny tty02 Nov 7 1004
6Possible PATH Problem
- whosonwhoson Command not found
- Due to PATH shell variable (see later)
- Quick fixes ./whoson or sh whoson
72. Combining Commands
- Sequencing
- a b c
- same as
- a b c
8Process Creation ()
- a b c14271 (PID for a)14272 (PID for
b) - a b c 142901429114292
- a b c 14302 (PID for piped commands)
9Processes in Action
- cat aecho -n aaaaaaaaaaaaaaaaaaaaaecho -n
aaaaaaaaaaaaaaaaaaaaasleep 2echo -n
aaaaaaaaaaaaaaaaaaaaaecho -n
aaaaaaaaaaaaaaaaaaaaa - Similarly for b and c
- Try the following a few times
- a b c
On the lab machines there isn't much variation
unless the machine is loaded.
103. Redirecting Output
- 1gt redirect standard output (stdout)2gt redirect
standard error (stderr) - cat a b 1gt out 2gt err
Files
stdout
out
cat a b
err
stderr
11gt
- redirect one stream into another
- 2gt1 redirect stderr into stdout
- cat a b 1gt theLot 2gt1
theLot
stdout
cat a b
stderr
124. Executing Scripts
- Make sure that a script is executed by the Bourne
Shell - sh whoson (no need for chmod)
- or
- cat boss!/bin/shecho Definitely Bourne
Shell Script
continued
13- On Linux machines (e.g. calvin), the Bourne shell
has been replaced by Bash - sh means the Bash shell
145. Variables
- 5.1. User-defined Variables
- 5.2. Environment Variables
- 5.3. Readonly Shell Variables
155.1. User-defined Variables
No spaces around the
- personalex echo personperson echo
personalex - var returns the value stored in var
- called substitution
165.1.1. Switch off Substitution
- Swich off substitution with 'var' or \var
- echo 'person'person echo \personperson
175.1.2. Switch off Special Chars (")
- "" switches off the special meaning of characters
used in filename generation (e.g. , ?) - ls // directory contentsad.reportad.summary
- memoad echo "memo"ad echo
memoad.report ad.summary
means only
means any number of characters
185.1.3. Exporting Variables
- Normally a variable is local to the running
script (the process). - It is sometimes useful if running scripts
(processes) can access another scripts
variables. - e.g.
we want to use cheese in subtest
calls
extest
subtest
cheeseenglish
19No Exporting extest1 subtest
Using " is a good habit, see later
- cat extest1cheeseenglishecho "extest1 1
cheese"subtestecho "extest1 2 cheese"cat
subtestecho "subtest 1 cheese"cheeseswissech
o "subtest 2 cheese"
continued
20subtest does not see extest1's cheese value
- extest1extest1 1 englishsubtest 1 subtest
2 swissextest1 2 english
extest1 is not affected by subtest's setting
of cheese
21Exporting extest2 subtest
- cat extest2export cheesecheeseenglishecho
extest2 1 cheesesubtestecho extest2 2
cheese - extest2extest2 1 englishsubtest 1
englishsubtest 2 swissextest2 2 english
cheese value passed in
change not exported from subtest
225.1.4. Reading
read inputs everything up to the newline
- cat readlnecho -n Type read lnecho You
entered ln - readlnType The Wizard of OzYou entered The
Wizard of Oz
23No Quotes
- cat readlnnqecho -n Type read lnecho You
entered ln - lsad.report summary1 readlnnqType You
entered ad.report summary1
directory contents
245.1.5. Executing Commands
A very simple shell
- cat proc_cmdecho -n Enter a command read
commandcommandecho Thanks - proc_cmdEnter a command echo Display
thisDisplay thisThanks
255.1.6. Splitting Input
Text is split based on white space.
- cat split3echo -n Enter something read
word1 word2 word3echo Word 1 is word1echo
Word 2 is word2echo Word 3 is word3 - split3Enter something this is somethingWord1
is thisWord2 is isWord3 is something
26- split3Enter something this is something else,
xWord1 is thisWord2 is isWord3 is something
else, x
The last variable gets everything that is left in
the input.
275.1.7. Command Subsitution
Must use
- cat mydirthis_dirpwdecho Using the
this_dir directory.this_date(date)echo
"Today's date this_date" - mydirUsing /home/ad/teach/adv-unix/bourne
directoryToday's date Tue Nov 7 135246 ICT
2000
A Bash addition
285.2. Environment Variables
- Most environment variables get their values from
the shell at login. - The values of some of the variables can be set by
editing the .profile file in your home directory - Bash uses .bash_profile and .bashrc
295.2.1. Examples
- HOME pathname of your home directory
- pwd/home/ad/planning echo HOME/home/ad
cd pwd/home/ad
cd uses HOME to return to your home directory
continued
30- PATH
- directories where executable can be found
- represented as a string of pathnames separated by
s - echo PATH/usr/local/bin/usr/bin/bin
PATHSPATH"/home/ad/bin." echo
PATH/usr/local/bin/usr/bin/bin/home/ad/bin.
Extend the default PATH
31Note for SysAdmins
- If you are the system administrator (superuser,
root) for your machine, do not extend your path
with "." - it opens you to potential attack by hackers
- e.g. 'fake' UNIX utilities placed in the current
directory
325.2.2. Typical .profile
- cat .profileTERMvt100PATHPATH"/home/ad/bin
."PS1ad CDPATHHOMEexport TERM PATH PS1
CDPATHstty kill u - . .profile
export needed in the Bourne shell
335.2.3. Typical .bash_profile
- On calvin, .bash_profile simply invokes your
.bashrc (if it exists) - umask 002if -f /.bashrc -a PS1"\ "
then . /.bashrcfi
These shell commands will be explained later
continued
34Typical .bashrc
- PS1"\u_at_\h " PS1"\w\ "PATHPATH"."ali
as ls'/bin/ls -F'alias dir'ls -ba'alias
cls"clear" psgrep() ps aux grep 1
grep -v grep
No export needed
These features will be explained later.
355.2.4. set
- The current settings for the environment
variables can be listed with set - set moreBASH/bin/bash PATH/usr/local/b
in/usr/bin/bin. PS1'\u_at_\h '
365.3. Readonly Shell Variables
- These are environment variables that cannot have
their values changed. - Most of them relate to the arguments supplied to
a script when it is called.
375.3.1. Script Name (0)
- cat abcecho The name of this script is 0
- abcThe name of this script is abc
385.3.2. Script Arguments (1, 2,..., 9)
- cat display_5argsecho The first five command
lineecho arguments are 1 2 3 4 5 - display_5args jenny alex helenThe first five
command linearguments are jenny alex helen
If the variable has no value, then nothing is
printed.
395.3.3. All Arguments ()
- cat display_allecho
- display_all a b c de fg hi jk mno pqr stu
w x y za b c de fg hi jk mno pqr stu w x y z - _at_ is like but puts ... around each printed
argument
405.3.4. Number of Arguments ()
- cat num_argsecho This script has
arguments. - num_args helen alex jennyThis script has 3
arguments
415.3.5. The shift Commnd
- shift moves argument values on the command line
one ltnumbergt to the left. - Overcomes limit of 9 argument variables(1, 2,
..., 9)
continued
42- cat demo_shiftecho arg1 1 arg2 2 arg3
3shiftecho arg1 1 arg2 2 arg3
3shiftecho arg1 1 arg2 2 arg3
3shift - demo_shift alice helen jenny junearg1 alice
arg2 helen arg3 jennyarg1 helen arg2 jenny
arg3 june arg1 jenny arg2 june arg3
jenny "moves" to the left.
435.3.6. The set Command (Again)
- set cmd (must use )
- evaluates cmd and assigns its values to the
script command line arguments (1, 2, ..., 9,
) - the values in cmd output are separated by
whitespace
continued
44- dateFri Jun 17 230409 GMT7 1996 cat
datesetset dateecho echoecho Argument 1
1echo Argument 2 2echo Argument 3
3echo 2 3, 6
The date values are assigned to 1, 2, etc.
continued
45- datasetFri Jun 17 230413 GMT7
1996Argument 1 FriArgument 2 JunArgument 3
17Jun 17, 1996
466. Command Flow
- 6.1. Branching
- 6.2. Test Forms
- 6.3. Looping
- 6.4. break, continue,
- 6.5. trap
476.1. Branching
- cat same_wordecho -n word 1 read
word1echo -n word 2 read word2if test
"word1" "word2"then echo Matchfiecho End
of Program
Use " to stop filename expansion
continued
48- same_wordword1 peachword2 peachMatchEnd
of Program
496.1.1. Second Format
Redirect stdout to stderr
- cat chkargsif 0 then echo Usage
chkargs argument... 1gt2 exit 1fiecho Program
runningexit 0 - chkargsUsage chkargs argument... chkargs
abcProgram running
506.1.2. if-then-else
C programmers prefer this format
- cat showif 0 then echo Usage show
-v filenames 1gt2 exit 1fiif 1 ! -v
then cat _at_else shift more _at_fi
Use show -v f1.txt f2.txt
516.1.3. if-then-elif
For multiway branches
- cat same3echo -n word 1 read word1echo -n
word 2 read word2echo -n word 3 read
word3if word1 word2 -a \ word2
word3 then echo Match words 1, 2, and 3
-a means "and" \ means "continued on next line"
continued
52- elif word1 word2 then echo Match
words 1 and 2elif word1 word3 then
echo Match words 1 and 3elif word2
word3 then echo Match words 2 and
3else echo No matchfi
536.1.4. case
Better style specify the shell, and comment the
code.
- cat command_menu!/bin/sh menu interface to
simple commandsecho \n COMMAND MENU\necho
a. Current date and timeecho b. Users
currently logged inecho c. Name of working
directoryecho d. Contents of working
directoryecho -n Enter a, b, c, or d read
answerecho
continued
54- case answer in a) date b) who
c) pwd d) ls -C
) echo answer not legal
esacecho
is the default always include it at the end
55- command_menu COMMAND MENU a.
Current date and time b. Users currently logged
in c. Name of working directory d. Contents
of working directoryEnter a, b, c, or d aFri
Jun 17 141157 GMT 1996
56Other case Patterns
- ? matches a single character
- ... any character in the brackets. Use - to
specify a range (e.g. a-z) - or (e.g. aA)
- it can be used to match "any number of
characters"
57- cat timeDay!/bin/shecho Is it morning? Answer
yes or noread timeofdaycase "timeofday" in
"yes" "y" "Yes" "YES" ) echo "Good
Morning" nN ) echo "Good
Afternoon" ) echo "Uhh??" esac
586.2. test Forms
Used in the conditions of if's and loops.
- Format
- test expr
- expr
- e.g.
- test word1 word2
- 1 ! -v
- word2 word3
- word1 word2 -a \ word2
word3
596.2.1. String Expressions
- string1 string2
- string1 ! string2
- -n string (true if string is not )
- -z string (true if string is )
606.2.2. Numerical Expressions
- number1 -eq number2 (equality)
- number1 -ne number2 (inequality)
- number1 -lt number2 (lt)
- number1 -le number2 (lt)
- number1 -gt number2 (gt)
- number1 -ge number2 (gt)
616.2.4. File Test Expressions
- -f file (file exists)
- -d file (file exists but is a directory)
- -r file (file is readable)
- -w file (file is writable)
- -x file (file is executable)
626.2.5. Combining Expressions
- ! expr (not expr)
- expr1 -a expr2 (and)
- Bash allows as well
- expr1 -o expr2 (or)
- Bash allows as well
- ( expr )
636.3. Looping (for-in)
- Format
- for loop-index in argument-listdo
commandsdone
64- cat fruitfor fruit in apples oranges pearsdo
echo fruitdoneecho Task completed - fruitapplesorangespearsTask completed
65Looking for for in files
- First version of script
- cat file-fors1for f in lsdo echo fdone
66- cat file-fors2for f in lsdo grep -i for
f gt /dev/null if ? 0 then echo
f fidone
Ignore output
676.3.1. for
- cat whos Give user details from
/etc/passwdif 0 then echo Usage whos
id... 1gt2 exit 1fifor i read as for i in
_at_do awk -F print 1, 5 /etc/passwd
grep -i idone
awk variables with
686.3.2. while
- cat countnumber0while number -lt 10 do
echo -n "number" numberexpr number
1doneecho - count0123456789
69Watching for Mary to log in
- while sleep 60do who grep marydone
- Disadvantages
- if Mary is already logged in, then we must wait
60 secs to find out - we keep being told that Mary is logged in
706.3.3. until
- Format
- until commanddo loop body until the
command evaluates to truedone - Watching (v.2)
- until who grep marydo sleep 60done
loop until grep returns true (0)
716.3.4. Extending Watching
- Generalise so can watch for anyone
- watchfor ad
- Watch for everyone logging in/out
- watchwho
72watchfor
- !/bin/sh watchfor watch for person supplied
as argumentif 0 then echo Usage
watchfor person exit 1fiuntil who grep
1do sleep 60done
73watchwho
- Once a minute, run who and compare its output to
that from a minute ago. Report any differences. - Keep the who output in files in /tmp
- Give the files unique names by adding the shell
variable . - is the PID of the users shell
74- !/bin/sh watchwho watch who logs in and
outnew/tmp/wwho1.old/tmp/wwho2.gt old
create an empty filewhile truedo who gt
new diff old new mv new old sleep
60done awk /gt/ 1 in print /lt/
1 out print
An advanced Shell programming technique
75Use
initial users
- watchwhoin root tty1 Nov 6 0932in ad pts/3
Nov 8 0849 (myrrh.coe.psu.ac.th)in s4010441
pts/5 Nov 8 1011 (192.168.0.134)in ad pts/4
Nov 8 1012 (myrrh.coe.psu.ac.th)in s4010143
pts/17 Nov 7 2357 (192.168.0.204)out ad pts/4
Nov 8 1012 (myrrh.coe.psu.ac.th)in ad pts/4
Nov 8 1016 (myrrh.coe.psu.ac.th)out ad pts/4
Nov 8 1018 (myrrh.coe.psu.ac.th)
76Notes
- diff uses lt and gt to distinguish data from
old and new - diff old newlt adgt mary
old
new
johnad
johnmary
continued
77while loop
awk
- The output from the while loop is piped into awk
- only one call to awk is required
- the "pipe" programming style
- A calvin problem
- awk had to be called with the -W interactive
option so that its output was not buffered - otherwise nothing would appear
786.3.5. Checking mail
- Have a script watch your mailbox periodically,
and report whenever the mailbox changes by
printing You have mail. - Usage
- checkmail checks every 60 secs
- checkmail 120 checks every 120 secs
79checkmail
- !/bin/sh checkmail watch mailbox for
growthtime1-60oldls"ls -l MAIL"while
truedo newls"ls -l MAIL" echo oldls
newls oldls"newls" sleep timedone awk
5 lt 14 print You have mail
uses the pipe technique
80Notes
- MAIL is a builtin shell variable, with the value
/var/spool/mail/USER - t1-60 sets t to 1 or, if no argument is
provided, to 60 - General form var-thingreturns value of var
if defined otherwise thing
continued
81- Use awk to print a message only when the mailbox
gets bigger - awk 5 14 compares the size fields of the two ls
-l calls output at the end of each iteration - e.g. ls -l foo-rw-r--r-- 1 ad ad 34512 Nov
13 1996 foo ls -l foo-rw-r--r-- 1 ad ad 34512
Nov 13 1996 foo
5th value
14th value
826.4. break, continue,
- break and continue are used as in C
- break escapes from a loop
- for file in freddo if -d "file" then
deleted? break finish loop fi
do something done
continued
83- continue goes to the top of a loop
- for file in freddo if -d "file" then
deleted? continue go back to loop top
fi do something done
continued
84- The '' command is the same as true, but runs a
tiny bit faster - often used to simplify control logic
- if -f fred then is fred a file?
do nothingelse do something fi
856.5. trap
- Capture user interrupts or system call failures.
- Format (with )
- trap commands signal-numbers-or-name
- Some signal numbers (names)
- 2 (INT) press delete or control-C
- 3 (QUIT) press control- or control-\
- 15 (TERM) kill command signal
continued
86- A complete list of available signals is given by
typing trap -l at the command line. - To ignore a signal, set the trap command to be
empty () - To reset signal processing, set the command to -
continued
87- cat intertrap echo PROGRAM INTERRUPTED exit
1 INTwhile truedo echo Program running.
sleep 2done
887. Functions
- Bash allows shell scripts to use functions
- function_name() statements
- Functions must be defined textually before they
are used.
continued
89- Functions can return integer values
- Function parameters are passed by modifying ,
_at_, , 1 -- 9 while the function is
executing. - Local variables are defined with the local
keyword - the other variables in a script are global by
default
90my_name
- !/bin/shyes_or_no() a function echo
"Is your name ?" while true do echo -n
"Enter yes or no " read x case "x" in
y yes ) return 0 n no ) return
1 ) echo "Answer yes or no"
esac done end of yes_or_no()
continued
91- the main part of the scriptecho "Original
parameters are "if yes_or_no "1"then echo
"Hi 1, nice name"else echo "Never
mind"fiexit 0
92Use
- my_name andrew davisonOriginal parameters are
andrew davisonIs your name andrew ?Enter yes or
no yHi andrew, nice name
938. Useful Scripting Commands
- expr evaluates its argument as an expression
- ansexpr x 1
- The usually operators are available
- - / (integer divison) (modulo)
- gt gt lt lt
- (equality) !
- (or) (and)
continued
94printf
- Bash supports printf, as a more flexible echo
- printf "format string" parameter1 ...
- Very similar to printf() in C
- main restriction is no support for floats
- only integers are supported in the shell
- printf "s d\ts\n" "hi there" 15 studentshi
there 15 students
959. Here Documents
- A here document allows input to be passed into a
command from within a script - the command thinks the input is coming from a
file or input stream - A here document begins with ltltLABEL, and ends
with LABEL - LABEL can be anything
96- cat here1!/bin/shcat ltlt!FUNKY!hellothis
is a heredocument!FUNKY! - here1hellothis is a heredocument
the here document
9710. Debugging
- Various options can be set when invoking a shell
script to help with debugging. - There are two ways to set the options
- from the command line when calling the script
- from within the script by using set
continued
98- Command Line Set
- sh -n script set -n
- do not execute the script, only parse it
- sh -v script set -v
- echoes commands after executing them
- sh -x script set -u
- warns when an undefined variable is used
9911. More Information
- The Bourne Shell
- Ch. 10, Sobell
- Bourne/Bash
- Beginning Linux ProgrammingNeil Matthew and Rick
StonesChapter 2