You are on page 1of 6

Today in Class What Is the shell?

• Unix Shell Scripts • First UNIX “user interface”


• Text-based
– Bourne shell (/bin/sh)
• Provides access to all UNIX user-level programs
– Start programs
– Manage running programs (job control)
– Connect together programs (pipes)
– Manage I/O to and from programs (redirection)
• In actuality, the UNIX shell is just another
program
– One that is very specific to the operating system

1/4/2004 Copyright 2004 Jon Herlocker 1 1/4/2004 Copyright 2004 Jon Herlocker 2

What Is shell script? When to use shell scripts?


• High level programming language • Automating frequent UNIX tasks
– Variables, conditionals, loops, etc. • Complex shell commands
• Dynamic
• For small programs
– No compiling, no variable declaration, no memory
management – That you need to create quickly
• Powerful/efficient – That will change frequently
– Do lots with little code – That need to be very portable
– Can use other programs as subroutines! – That you want others to see and understand easily
• String oriented • As a glue to connect together other programs

1/4/2004 Copyright 2004 Jon Herlocker 3 1/4/2004 Copyright 2004 Jon Herlocker 4

simple shell script Using Shell Scripts


#!/bin/sh
# find_large.sh
• Invoke the shell explicitly
# This shell script locates all files
# larger than a megabyte in my home directory – /bin/sh find_large.sh
# and prints the top 5 largest in decreasing • Let the OS invoke the shell for you
# order. – Add “#!/bin/sh” as the first line
– chmod +x find_large.sh
find $HOME -size +1000000c -ls | sort -nrk 7 | head -5
– ./find_large.sh

1/4/2004 Copyright 2004 Jon Herlocker 6

Warning! Slides may change before


presentation. 1
Find_large.sh Output
Communicating between commands
: jasper{~/cs311}176; find_large.sh
2778522 6712 -rw------- 1 herlock faculty 6861050
Sep 28 08:39 /nfs/phantom/u10/herlock/mail/Sent • Command line parameters
1254431 4896 -rwx------ 1 herlock faculty 4997632
Sep 26 09:35
– Like “-size 1000000c”
/nfs/phantom/u10/herlock/thesis/thesis.doc – In shell script, accessible as
1254427 4856 -rwx------ 1 herlock faculty 4962816 • $1, $2, $3, etc.
Sep 17 16:18 • $# is the number of parameters
/nfs/phantom/u10/herlock/thesis/old/thesis.doc
• $*, $@ contain all parameters
2778511 1752 -rw------- 1 herlock faculty 1784763
Sep 18 13:47 /nfs/phantom/u10/herlock/5102.tar.gz • Environment variables
1022355 1752 -rw------- 1 herlock faculty 1784763 – HOME=/home/herlock
Sep 25 15:04 /nfs/phantom/u10/herlock/tmp/5102.tar.gz
– export HOME
1/4/2004 Copyright 2004 Jon Herlocker 8

Communicating between commands IF


• Pipes if command-list
– Standard in (stdin) – usually from keyboard then
– Standard out (stdout) – usually to terminal screen command-list
– Standard error (stderr) – usually to terminal screen too elif command-list
• Return values from programs then
– Exit value from the main function in C command-list
• exit(1) else
• return(1)
command-list
– 0 indicates success!
fi
1/4/2004 Copyright 2004 Jon Herlocker 9 1/4/2004 Copyright 2004 Jon Herlocker 10

WHILE Publish.sh
while command-list1 • Powerpoint saves a presentation as
– An root html file
do – A directory of support files (images)
command-list2 • Example (ch1.ppt):
done – ch1.htm
– ch1_files
• Publish them
– Copy them to web directory
– Set permissions appropriately

1/4/2004 Copyright 2004 Jon Herlocker 11 1/4/2004 Copyright 2004 Jon Herlocker 12

Warning! Slides may change before


presentation. 2
Publish.sh Part 1/3 Publish.sh part 2/3
#!/bin/sh
# publish.sh - copies a powerpoint
# presentation to the web directory.
file_basename=$1
usage="USAGE: publish.sh <presentation_root>" html_file=$file_basename.htm

# destination=/mywebdir
destination=/tmp if test ! -f $html_file
then
if test $# -ne 1
then echo "File \"$html_file\" does not exist"
echo "Wrong number of parameters" exit 1
echo $usage
exit 1 fi
fi

Publish.sh part 3/3 for


html_directory=${file_basename}_files
• Doesn’t use exit status
cp -a $html_file $html_directory for i in a b c d
$destination do
printf "<%s>" $i
chmod o+r $destination/$html_file
done
chmod -R o+rx
$destination/$html_directory
chmod -R o+r • Output
$destination/$html_directory <a><b><c><d>
1/4/2004 Copyright 2004 Jon Herlocker 16

Quoting Quoting Examples


• Quoting printf '%s\n' I have lots of $$$$$s
– Protect metacharacters printf '%s\n' "I have lots of $$$$s"
– Groups into a single parameter printf '%s\n' "I have lots of \$\$\$\$s"
• Single quotes(' ') printf '%s\n' 'I have lots of $$$$s'
– Protects all metacharacters – no variable expansion
• Double quotes
– Protects spaces
– Variables are expanded
• Backslash
– Protects any single metacharacter
1/4/2004 Copyright 2004 Jon Herlocker 17 1/4/2004 Copyright 2004 Jon Herlocker 18

Warning! Slides may change before


presentation. 3
Efficient code creation Error handling
• Borrow and learn from other code • Correct syntax, but command fails
• Places to look: – Shell will keep executing
– /bin, /usr/bin, /sbin, lots of other places • If you want the shell to exit if any
– cd /bin commands have a problem
grep ‘/bin/sh’ * – -e flag to /bin/sh
• Most signals will kill script immediately
– EX: control-C

1/4/2004 Copyright 2004 Jon Herlocker 19 1/4/2004 Copyright 2004 Jon Herlocker 20

EX: Disastrous results One fix


#!/bin/sh -e
#!bin/sh

cp thesis.doc
cp thesis.doc thesis_current.doc
thesis_current.doc
rm thesis.doc
rm thesis.doc

Another fix Another fix


#!/bin/sh #!/bin/sh

cp thesis.doc thesis_current.doc if cp thesis.doc thesis_current.doc


if test $? -ne 0 then
then rm thesis.doc
echo "copy failed” 1>&2 else
exit 1 echo "copy failed” 1>&2
fi exit 1
rm thesis.doc fi

Warning! Slides may change before


presentation. 4
Trap command Trap example
#!/bin/sh

• Use the trap command to catch signals and TMP=ps$$


clean up
• Usage trap "rm –f $TMP; exit 1" INT HUP
trap <code to execute> list of signals TERM

ps –ef | grep herlock > $TMP


lpr $TMP
rm $TMP
1/4/2004 Copyright 2004 Jon Herlocker 25

Reading a Line from Input Standard in/out/err in sh


• Use the read command • Standard error redirected to
read line – A file
– Standard in
• Now the next line of stdin is stored in • Standard in redirected to
variable $line – A file
– Standard error
• Both redirected
– To a file
1/4/2004 Copyright 2004 Jon Herlocker 27 1/4/2004 Copyright 2004 Jon Herlocker 28

Here documents Here documents


• Used in situations where cat <<EOF
Welcome to the world of CS311. In this
– You have a small amount of input to provide class you will learn how to use all the
• but more than one line cool features of the UNIX operating
– You want to do shell variable expansion system. You will become an expert in
within the input the ways of UNIX, which will in turn
make you more productive.
– You don’t want to use a temporary file Your first class is scheduled on $DATE.
Class begins at $TIME
EOF

1/4/2004 Copyright 2004 Jon Herlocker 29

Warning! Slides may change before


presentation. 5
Shell File Expansion Examples – file expansion
• Certain metacharacters are expanded and • List all files in the current directory whose names
replaced with all files with matching names start with "cs311"
– ls cs311*
• * - matches anything
• List all files in the current directory that have
• ? – matches any one character "cs311" somewhere in their name
– ls *cs311*
• List all files in subdirectories that contain "cs311"
– ls */*cs311*

1/4/2004 Copyright 2004 Jon Herlocker 31 1/4/2004 Copyright 2004 Jon Herlocker 32

Tricks of read: Tricks of read:


Input & subshells Input & subshells solution
• Beware of this construct: • The following code will achieve the
– read line < input_file
expected result
• The following code just reads the first line of the
file repeatedly for ever – We redirect the I/O of the entire “while” block
– while read line < input_file
do
# Do something with $line
while read line
done do
• Reason: Every time the shell sees ‘< input_file‘, it # Do something with $line
redirects the input to the beginning of input_file
done < input_file
1/4/2004 Copyright 2004 Jon Herlocker 33 1/4/2004 Copyright 2004 Jon Herlocker 34

Warning! Slides may change before


presentation. 6