03 bash intro
TRANSCRIPT
-
8/8/2019 03 Bash Intro
1/99
Advanced UNIX (BASH)
Objectivesexplain how to write Bourne and BashShell scripts
-
8/8/2019 03 Bash Intro
2/99
Overview
1. Making a File Executable2 . Combining Commands3 . Redirecting Output4 . Executing Scripts5 . Variables
6 . Control Flow
continued
-
8/8/2019 03 Bash Intro
3/99
7 . Functions8 . Other Commands9 . H ere Documents10. Debugging11. More Information
-
8/8/2019 03 Bash Intro
4/99
1.Making a File Executable
$ cat whosondateecho Users currently logged on
who
Wrong:$ whoson
whoson: Permission denied
-
8/8/2019 03 Bash Intro
5/99
Right:$ ls -lg whoson
-rw-r--r-- 1 ad pubs 42 Jun 1 7 10 : 55 whoson$ chmod u+x whoson$ ls -lg whoson
-rwxr--r-- 1 ad pubs 42 Jun 1 7 10 : 55 whoson
$ whosonTue Nov 7 1 3: 21 : 34 ICT 2000Users currently logged inad consol Nov 7 08: 26jenny tty 0 2 Nov 7 10 :0 4
-
8/8/2019 03 Bash Intro
6/99
Possible
PATH
P
r o
blem$ whosonwhoson: Command not found
Due to PATH shell variable (see later)
Quick fixes:
$ ./whosonor $ sh whoson
-
8/8/2019 03 Bash Intro
7/99
2.C
ombining C
ommand
s
Sequencing:$ a ; b ; c
same as:$ a$ b$ c
-
8/8/2019 03 Bash Intro
8/99
P r o cess Creatio n (&)$ a & b & c1 4271 (PID for a)1 4272 (PID for b)
$ a & b & c &1 429 01 429 11 4292$
$ a | b | c &1 43 0 2 (PID for piped commands)
-
8/8/2019 03 Bash Intro
9/99
Pr o
cess
es
in Actio
n$ 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 thereisn't muchvariation unlessthe machine isloaded.
-
8/8/2019 03 Bash Intro
10/99
3 . Redirecting Output
1> redirect standard output (stdout)2> redirect standard error (stderr)
$ cat a b 1 > out 2> err
cat a b
out
err
stdout
stderr
Files
-
8/8/2019 03 Bash Intro
11/99
>&redirect one stream into another:
2>&1 redirect stderr intostdout
$ cat a b 1 > theLot 2>&1
cat a b
theLot
stderr
stdout
-
8/8/2019 03 Bash Intro
12/99
4.Executing Script
s
Make sure that a script is executed by theBourne Shell:
$ sh whoson (no need for chmod )
or:$ cat boss#!/bin/shecho Definitely Bourne Shell Script
continued
-
8/8/2019 03 Bash Intro
13/99
On Linux machines (e .g . calvin ), the Bourneshell has been replaced by Bash
sh means the Bash shell
-
8/8/2019 03 Bash Intro
14/99
5.Variable
s
5 .1. User-defined Variables
5 .2 . Environment Variables
5 .3 . Readonly Shell Variables
-
8/8/2019 03 Bash Intro
15/99
5.1
.U
ser-defined Variable
s
$ person=alex$ echo personperson$ echo $personalex
$var returns the value stored in var
called substitution
No spacesaround the =
-
8/8/2019 03 Bash Intro
16/99
5 .1 .1 . Switch o ff Subs titutio n
Swich off substitution with 'var ' or \var
$ echo '$person'$person$ echo \$person$person
-
8/8/2019 03 Bash Intro
17/99
5 .1 .2 . Switch o ff Special Char s (")"" switches off the special meaning of charactersused in filename generation (e .g. *, ?)
$ ls // directory contentsad.reportad.summary$ memo=ad*$ echo "$memo"ad*$ echo $memoad.report ad.summary
* means only *
* means any number of characters
-
8/8/2019 03 Bash Intro
18/99
5 .1 .3 . Expo rting Variable s
Normally a variable is local to the runningscript (the process) .
It is sometimes useful if running scripts(processes) can access another scriptsvariables .
e .g.extest subtest
cheese=english
callswe want touse cheesein subtest
-
8/8/2019 03 Bash Intro
19/99
No Expo rting: extest 1 &subtest
$ cat extest 1cheese=englishecho "extest 1 1 : $cheese"
subtestecho "extest 1 2: $cheese"
$cat subtestecho "subtest 1 : $cheese"cheese=swissecho "subtest 2: $cheese"
continued
Using " isa good habit,see later
-
8/8/2019 03 Bash Intro
20/99
$ extest 1extest 1 1 : englishsubtest 1 :
subtest 2: swissextest 1 2: english
subtest does notsee extest 1 'scheese value
extest 1 is not affected
by subtest 's setting of cheese
-
8/8/2019 03 Bash Intro
21/99
-
8/8/2019 03 Bash Intro
22/99
5 .1 .4 . Reading$ cat readlnecho -n Type: read ln
echo You entered: $ln
$ readlnType: The Wizard of Oz You entered: The Wizard of Oz
read inputs everythingup to the newline
-
8/8/2019 03 Bash Intro
23/99
No Quo tes
$ cat readlnnqecho -n Type: read ln
echo You entered: $ln
$ lsad.report summary 1$ readlnnq
Type:*
You entered: ad.report summary 1
directorycontents
-
8/8/2019 03 Bash Intro
24/99
5 .1 .5 . Executing Co mmand s
$ cat proc_cmdecho -n Enter a command: read command
$c ommand echo Thanks
$ proc_cmdEnter a command: echo Display this
Display thisThanks
A very simple shell
-
8/8/2019 03 Bash Intro
25/99
5 .1 .6 . Splitting Input$ cat split 3echo -n Enter something: read word 1 word 2 word 3echo Word 1 is: $word 1 echo Word 2 is: $word 2echo Word 3 is: $word 3
$ split 3Enter something: this is something
Word 1 is: thisWord 2 is: isWord 3 is: something
Text is split based on whitespace.
-
8/8/2019 03 Bash Intro
26/99
$ split 3Enter something: this is something else, x Word 1 is: thisWord 2 is: isWord 3 is: something else, x
The last variable getseverything that is leftin the input.
-
8/8/2019 03 Bash Intro
27/99
5 .1 .7 . Co mmandSubs itutio n
$ cat mydirthis_dir=pwdecho 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 1 3: 52 : 46 ICT 2000$
A Bashaddition
Must use
-
8/8/2019 03 Bash Intro
28/99
5.2
.Envir
onment Variable
s
Most environment variables get their values fromthe shell at login .
The values of some of the variables can be setby editing the .profile file in your home directory
Bash uses .bash_profile and .bashrc
-
8/8/2019 03 Bash Intro
29/99
5 .2 .1 . Examples
HOME pathname of your home directory
$ pwd/home/ad/planning$ echo $HOME/home/ad$ cd$ pwd/home/ad
continued
cd uses HOME
to return to your home directory
-
8/8/2019 03 Bash Intro
30/99
PATH
directories where executable can be foundrepresented as a string of pathnames separated by:s
$ echo $PATH/usr/local/bin:/usr/bin:/bin:$ PATH=SPATH":/home/ad/bin:."$ echo $PATH/usr/local/bin:/usr/bin:/bin:/home/ad/bin:.
Extend thedefault PATH
-
8/8/2019 03 Bash Intro
31/99
No te f o r Sys Admins
If you are the system administrator (superuser, root) for your machine,
do not extend your path with "."it opens you to potential attack byhackerse .g . 'fake' U NIX utilities placed inthe current directory
-
8/8/2019 03 Bash Intro
32/99
5 .2 .2 . Typical .pr o file$ cat .profileTERM=vt 100PATH=$PATH":/home/ad/bin:."PS 1 =ad: CDPATH=:$HOMEexport TERM PATH PS 1 CDPATH
stty kill ^u
$ . .profile export neededin the Bourneshell
-
8/8/2019 03 Bash Intro
33/99
-
8/8/2019 03 Bash Intro
34/99
Typical .bas hrcPS 1 ="\u@\h$ "# PS 1 ="\w[\#]$ "PATH=$PATH":."
alias ls='/bin/ls -F'alias dir='ls -ba'alias cls="clear"
::
psgrep(){
ps aux | grep $1 | grep -v grep}
These featureswill be explained
later.
No export needed
-
8/8/2019 03 Bash Intro
35/99
5 .2 .4 . s etThe current settings for the environmentvariables can be listed with set :
$ set | moreBASH=/bin/bash
:PATH=/usr/local/bin:/usr/bin:/bin:.
:
PS1
='\u@\h$ ':
-
8/8/2019 03 Bash Intro
36/99
5.3
. R
eado
nly Shell Variables
These are environmentvariables that ca nnot have their values changed .
Most of them relate to the
arguments supplied to a scriptwhen it is called .
-
8/8/2019 03 Bash Intro
37/99
5.3
.1
.Script Name ($0 )
$ cat abcecho The name of this script is $0
$ abcThe name of this script is ab c
-
8/8/2019 03 Bash Intro
38/99
5 .3 .2 . Script Argument s ($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 j enny alex helen
If the variable has no value,then nothing is printed.
-
8/8/2019 03 Bash Intro
39/99
5 .3 .3 . All Arguments ($*)
$ cat display_allecho $*
$ display_all a b c de fg hi jk mno pqrstu w x y z
a b c de fg hi jk mno pqr stu w x y z
$@ is like $* but puts ... around each printedargument
-
8/8/2019 03 Bash Intro
40/99
5 .3 .4 . Number o f Arguments
($#)$ cat num_argsecho This script has $# arguments.
num_args helen alex jennyThis script has 3 arguments
-
8/8/2019 03 Bash Intro
41/99
5 .3 .5 . The shift Co mmnd
shift moves argument values onthe command line one $to the left .
Overcomes limit of 9 argument
variables($1 , $2, ... , $9 )
continued
-
8/8/2019 03 Bash Intro
42/99
$ cat demo_shift
echo arg 1 = $ 1 arg 2= $ 2 arg 3= $ 3shiftecho arg 1 = $ 1 arg 2= $ 2 arg 3= $ 3shiftecho arg 1 = $ 1 arg 2= $ 2 arg 3= $ 3shift
$ demo_shift alice helen jenny junearg 1 = alice arg 2= helen arg 3= jennyarg 1 = helen arg 2= jenny arg 3= junearg 1 = jenny arg 2= june arg 3=
jenny "moves"to the left.
-
8/8/2019 03 Bash Intro
43/99
5 .3 .6 . The set Co mmand
(Again)set cmd (mustuse )
evaluates cmd and assigns itsvalues to the script command linearguments ($ 1 , $2, ... , $9, $*)the values in cmd output areseparated by whitespace
continued
-
8/8/2019 03 Bash Intro
44/99
$ dateFri Jun 1 7 23 : 0 4: 0 9 GMT+7 1 996$ cat datesetset dateecho $*
echoecho Argument 1 : $ 1 echo Argument 2: $ 2echo Argument 3: $ 3echo $ 2 $3, $6
continued
The date valuesare assigned to $ 1 ,$2, etc.
-
8/8/2019 03 Bash Intro
45/99
$ datasetFri Jun 1 7 23 : 0 4: 1 3 GMT+7 1996
Argument1
: FriArgument 2: JunArgument 3: 1 7Jun 1 7 , 1 996
-
8/8/2019 03 Bash Intro
46/99
6 . Co mmand Flo w
6 .1. Branching6 .2 . Test Forms
6 .3 . Looping6 .4 . break , continue , :6 .5 . trap
-
8/8/2019 03 Bash Intro
47/99
6 .1 . Branching
$ cat same_wordecho -n word 1 : read word 1
echo -n word 2: read word 2if test " $ word 1" = " $ word 2"then
e c ho Mat c hfiecho End of Program
Use " to stopfilename
expansion
continued
-
8/8/2019 03 Bash Intro
48/99
$ same_wordword 1 : peachword 2: peach
MatchEnd of Program
-
8/8/2019 03 Bash Intro
49/99
$ cat chkargsif [ $# = 0 ]then
e c ho Usage: c hkargs argument... 1>&2exit 1
fiecho Program runningexit 0
$ chkargs
Usage: chkargs argument...$ chkargs abcProgram running
R edirectstdout to stderr
6 .1 .1 . Seco nd Fo rmat
-
8/8/2019 03 Bash Intro
50/99
6 .1 .2 . if-then-els e$ cat showif [ $# = 0 ]; then
echo Usage: show [-v] filenames 1 >&2exit 1
fiif [ $ 1 != -v ]then
cat $@else
shiftmore $@
fi
C programmers prefer this format
Use:$ show -v f 1 .txt f 2.txt
-
8/8/2019 03 Bash Intro
51/99
6 .1 .3 . if-then-elif
$ cat same 3echo -n word 1 : read word 1echo -n word 2:
read word 2echo -n word 3: read word 3if [ $word 1 = $word 2 - a \
$word 2 = $word 3 ]then
echo Match: words 1 , 2, and 3
continued
For multiway branches
-a means "and"
\ means "continuedon next line"
-
8/8/2019 03 Bash Intro
52/99
elif [ $word 1 = $word 2 ]then
echo Match: words 1 and 2elif [ $word 1 = $word 3 ]then
echo Match: words 1 and 3
elif [ $word 2 = $word 3 ]thenecho Match: words 2 and 3
elseecho No match
fi
-
8/8/2019 03 Bash Intro
53/99
6 .1 .4 . cas e$cat command_menu#!/bin/sh# menu interface to simple commands
echo \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
Better style: specifythe shell, and commentthe code.
-
8/8/2019 03 Bash Intro
54/99
c ase $answer ina) date
;;b) who
;;c) pwd
;;d) ls -C;;
*) echo $answer not legal;;
esa c
echo
* is the default;always include itat the end
-
8/8/2019 03 Bash Intro
55/99
$ command_menuCOMMAND MENU
a. Current date and timeb. Users currently logged inc. Name of working directoryd. Contents of working directory
Enter a, b, c, or d: a
Fri Jun 1 7 1 4: 11 : 57 GMT 1 996
-
8/8/2019 03 Bash Intro
56/99
O ther ca s e P attern s? matches a single character
[... ] any character in the brackets .Use - to specify a range (e .g . a-z )
| or (e .g . a|A )
* it can be used to match "anynumber of characters "
-
8/8/2019 03 Bash Intro
57/99
$cat timeDay
#!/bin/shecho Is it morning? Answer yes or noread timeofday
case "$timeofday" in"yes" | "y" | "Yes" | "YES" )
echo "Good Morning";;
[nN]* )echo "Good Afternoon";;
*) echo "Uhh??";;esac
-
8/8/2019 03 Bash Intro
58/99
6 .2 . tes t Fo rms
Format:test expr
[ expr ]e .g .
test $word 1 = $word 2[ $ 1 != -v ]
[ $word 2 = $word 3 ][ $word 1 = $word 2 -a \
$word 2 = $word 3]
Used in the conditions of if's and loops.
-
8/8/2019 03 Bash Intro
59/99
6 .2 .1 . String
Express io nsstring 1 = string 2
string 1 != string 2
-n string (true if string is not )
-z string (true if string is )
-
8/8/2019 03 Bash Intro
60/99
6 .2 .2 . Numerical
Express io nsnumber 1 -eq number 2 (equality)number 1 -ne number 2
(inequality)
number 1 -lt number 2 ( =)
-
8/8/2019 03 Bash Intro
61/99
6 .2 .4 . File Tes tExpress io ns
-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)
-
8/8/2019 03 Bash Intro
62/99
6 .2 .5 . Co mbiningExpress io ns
! expr (not expr)
expr 1 -a expr 2 (and)Bash allows && as well
expr 1 -o expr 2 (or)Bash allows || as well
( expr )
-
8/8/2019 03 Bash Intro
63/99
-
8/8/2019 03 Bash Intro
64/99
$ cat fruitfor fruit in apples oranges pearsdo
echo $fruitdone
echo Task completed
$ fruitapplesorangespearsTask completed
-
8/8/2019 03 Bash Intro
65/99
Loo king f o r f o r in files
First version of script:
$ cat file-fors 1for f in lsdo
echo $fdone
-
8/8/2019 03 Bash Intro
66/99
$ cat file-fors 2for f in lsdo
grep -i for $f > /dev/nullif [ $? == 0 ]then
echo $ffi
done
Ignore output
-
8/8/2019 03 Bash Intro
67/99
6 .3 .1 . f o r $ cat whos# Give user details from /etc/passwdif [ $# = 0 ]then
echo Usage: whos id... 1 >&2exit 1
fi
for i # read as for i in $@do
awk -F: {print $ 1 , $5 } /etc/passwd |grep -i $i
done awk variableswith
-
8/8/2019 03 Bash Intro
68/99
6 .3 .2 . while$ cat countnumber= 0while [ $number -lt 10 ]do
echo -n "$number"number=expr $number + 1 doneecho
$ count01 23456 789$
-
8/8/2019 03 Bash Intro
69/99
Watching f o r Mary to lo g in
while sleep 60do
who | grep mary
done
Disadvantages:if Mary is already logged in, then
we mustwait 6 0 secs to find outwe keep being told that Mary islogged in
-
8/8/2019 03 Bash Intro
70/99
-
8/8/2019 03 Bash Intro
71/99
6 .3 .4 . ExtendingWatching
Generalise so can watch for anyone:
$ watchfor ad
Watch for everyone logging in/out:$ watchwho
-
8/8/2019 03 Bash Intro
72/99
watchf o r #!/bin/sh# watchfor# watch for person supplied as argument
if [ $# = 0 ]
thenecho Usage: watchfor personexit 1
fiuntil who | grep $ 1 do
sleep 60done
-
8/8/2019 03 Bash Intro
73/99
watchwhoOnce a minute, run who and compare itsoutput to that from a minute ago . Report anydifferences .
Keep the who output in files in /tmp
G ive the files unique names by adding theshell variable $$ .$$ is the PID of the users shell
-
8/8/2019 03 Bash Intro
74/99
#!/bin/sh# watchwho: watch who logs in and out
new=/tmp/wwho 1 .$$old=/tmp/wwho 2.$$> $old # create an empty file
while truedo
who > $newdiff $old $newmv $new $old
sleep 60done | awk />/ {$ 1 = in: ; print}/
-
8/8/2019 03 Bash Intro
75/99
Us e$ watchwhoin: root tty 1 Nov 6 09: 32in: ad pts/ 3 Nov 8 0 8: 49 (myrrh.coe.psu.ac.th)in: s 4010 441 pts/ 5 Nov 8 10: 11 ( 1 92 . 1 68 . 0 . 1 34 )in: ad pts/ 4 Nov 8 10 : 1 2 (myrrh.coe.psu.ac.th)in: s 40101 43 pts/ 1 7 Nov 7 23: 57 ( 1 92 . 1 68 . 0 . 20 4)out: ad pts/ 4 Nov 8 10 : 1 2 (myrrh.coe.psu.ac.th)in: ad pts/ 4 Nov 8 10 : 1 6 (myrrh.coe.psu.ac.th)out: ad pts/ 4 Nov 8 10 : 1 8 (myrrh.coe.psu.ac.th)
:
initialusers
-
8/8/2019 03 Bash Intro
76/99
No tesdiff uses to distinguish data from$old and $new
$ diff old new< ad> mary
john
ad
old john
mary
new
continued
-
8/8/2019 03 Bash Intro
77/99
The output from the while loop is piped intoawk
only one call to awk is requiredthe "pipe " programming style
A calvin problem:
awk had to be called with the -W interactiveoption so that its output was not bufferedotherwise nothing would appear
whileloop
awk
-
8/8/2019 03 Bash Intro
78/99
6 .3 .5 . Checking mail
H ave a script watch your mailboxperiodically, and report whenever
the mailbox changes by printingYou have mail .
Usage:$ checkmail # checks every 6 0 secs$ checkmail 1 20 # checksevery 1 20 secs
-
8/8/2019 03 Bash Intro
79/99
checkmail#!/bin/sh# checkmail: watch mailbox for growth
time=${ 1 - 60 }
oldls="ls -l $MAIL"while truedo
newls="ls -l $MAIL"echo $oldls $newlsoldls="$newls"sleep $time
done | awk $ 5 < $ 1 4 {print You have mail}
uses the pipe technique
-
8/8/2019 03 Bash Intro
80/99
No tes
$MAIL is a builtin shell variable, withthe value /var/spool/mail/$USER
t=${ 1 -60 } sets t to $1 or, if noargument is provided, to 60
General form: ${var-thing}
returns value of var if defined;otherwise thing
continued
-
8/8/2019 03 Bash Intro
81/99
Use awk to print a message only when themailbox gets bigger:
awk $ 5 $ 1 4 compares the size fields of the two ls -lcalls output at the end of each iteration
e .g. $ ls -l foo-rw-r--r-- 1 ad ad 345 1 2 Nov 1 3 1 996 foo$ ls -l foo
-rw-r--r-- 1 ad ad 345 1 2 Nov 1 3 1 996 foo
5th value
1 4th value
-
8/8/2019 03 Bash Intro
82/99
6 .4 . break, c o ntinue, :
break and continue are used as in C:break escapes from a loop
for file in fred*do
if [ -d "$file" ]; then # deleted?break # finish loop
fi# do something#
done
continued
-
8/8/2019 03 Bash Intro
83/99
continue goes to the top of a loop:
for file in fred*doif [ -d "$file" ]; then # deleted?
continue # go back to loop topfi
# do something#
done
continued
-
8/8/2019 03 Bash Intro
84/99
The ':' command is the same astrue , but runs a tiny bit faster
often used to simplify control logicif [ -f fred ]; then # is freda file?
: # do nothingelse
# do something#
fi
-
8/8/2019 03 Bash Intro
85/99
6 .5 . trapCapture user interrupts or system call failures .
Format (with ):trap commands signal-numbers-or-name
Some signal numbers (names):2 ( INT ) press delete or control-C3 (QUIT ) press control- | or control- \1 5 (TERM) kill command signal
continued
-
8/8/2019 03 Bash Intro
86/99
A complete l ist of ava i l a b l e sign a l s is givenby typing trap -l at the command line .
To ignore a signal, set the trap commandto be empty ( )
To reset signal processing, set thecommand to -
continued
-
8/8/2019 03 Bash Intro
87/99
$ cat intertrap echo PROGRAM INTERRUPTED; exit 1 INTwhile truedo
echo Program running.sleep 2
done
-
8/8/2019 03 Bash Intro
88/99
7 . Functio ns
Bash allows shell scripts to usefunctions:
function_name() {statements}
Functions must be definedtextually before they are used .
continued
-
8/8/2019 03 Bash Intro
89/99
Functions can return integer values
Function parameters are passed by modifying$*, $@ , $#, $1 -- $9 while the function isexecuting .
Local variables are defined with the localkeyword
the other variables in a script are global bydefault
-
8/8/2019 03 Bash Intro
90/99
my_name#!/bin/sh
yes_or_no() { # a functionecho "Is your name $* ?"while truedo
echo -n "Enter yes or no: "read xcase "$x" in
y | yes ) return 0;;n | no ) return 1 ;;* ) echo "Answer yes or no"
esacdone
} # end of yes_or_no()
continued
-
8/8/2019 03 Bash Intro
91/99
# the main part of the script
echo "Original parameters are $*"
if yes_or_no " $1"then
echo "Hi $1 , nice name"else
echo "Never mind"fi
exit 0
-
8/8/2019 03 Bash Intro
92/99
Us e$ my_name andrew davisonOriginal parameters are andrew davisonIs your name andrew ?Enter yes or no: y
Hi andrew, nice name$
-
8/8/2019 03 Bash Intro
93/99
8 . Us eful Scripting
Co
mmands
expr evaluates its argument as anexpression:
ans=expr $x + 1
The usually operators are available:+ - * / (integer divison) % (modulo)
> > = <
-
8/8/2019 03 Bash Intro
94/99
Bash supports printf , as a more flexible echoprintf "format string" parameter 1 ...
Very similar to printf() in Cmain restriction is no support for floatsonly integers are supported in the shell
$ printf "%s %d\t%s\n" "hi there" 1 5 studentshi there 1 5 students$
printf
-
8/8/2019 03 Bash Intro
95/99
9 . Here Do cument s
A here document allows input to bepassed into a command from
within a scriptthe command thinks the input iscoming from a file or input stream
A here document begins with
-
8/8/2019 03 Bash Intro
96/99
$ cat here 1#!/bin/sh
cat $ here 1hello
this is a heredocument$
the heredocument
-
8/8/2019 03 Bash Intro
97/99
10 . Debugging
Various options can be set wheninvoking a shell script to help with
debugging.
There are two ways to set theoptions:
from the c omm a nd l ine whencalling the scriptfrom within the script by using set
continued
-
8/8/2019 03 Bash Intro
98/99
Command Line Setsh -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
-
8/8/2019 03 Bash Intro
99/99
11 . Mo re Inf o rmatio n
The Bourne Shell:Ch . 10 , Sobell
Bourne/Bash:B eginning Linux Progr a mming Neil Matthew and Rick StonesChapter 2