Bourne Shell Programming
Bourne Shell Programming
Background
Early Unix shell that was written by Steve Bourne of AT&T Bell Lab. Basic shell provided with many commercial versions of UNIX Many system shell scripts are written to run under Bourne Shell A long and successful history
Bourne Shell
1-1
Bourne Shell
1-2
if then
Structure
if test-command then
commands
test
Command test is a built-in command
Syntax
The test command evaluate an expression Returns a condition code indicating that the expression is either true (0) or false (not 0)
Logical AND operator to separate two criteria: -a Logical OR operator to separate two criteria: -o Negate any criterion: ! Group criteria with parentheses
Bourne Shell
1-4
Test Criteria
Test Operator for integers: int1 relop int2
Relop -gt -ge -eq -ne -le -lt Description Greater than Greater than or equal to Equal to Not euqal to Less than or equal to Less than
Bourne Shell
1-5
Exercise
Create a shell script to check there is at
if test $# -eq 0 then echo you must supply at least one arguments exit 1 fi
Bourne Shell
1-6
Test Criteria
-u filename
-w filename -x filename
Bourne Shell
1-7
Exercise
Check weather or not the parameter is a
Bourne Shell
1-8
Test Criteria
String testing
Criteria String -n string -z string String1 = string2 String1 != string2 meaning True if string is not the null string True if string has a length greater than zero True if string has a length of zero True if string1 is equal to string2 True if string1 is not equal to string2
Bourne Shell
1-9
Exercise
Check users confirmation
Frist,
What will happen if no around $user_input and user just typed return?
Bourne Shell 1-10
ifthenelse
Structure
if test-command then
else
commands commands
fi
You can use semicolon (;) ends a command the same way a NEWLINE does.
if [ ]; then fi if [ 5 = 5 ]; then echo "equal"; fi
Bourne Shell 1-11
ifthenelif
Structure
if test-command then
. . .
fi
commands
else
commands
Bourne Shell
1-12
Distinguish the output of trace from any output that the script produces
for in
Structure
for loop-index in argument_list do
done
commands
for
Structure
for loop-index do
done
commands
Automatically takes on the value of each of command line arguments, one at a time. Which implies
for arg in $@
Bourne Shell
1-15
while
Structure
while test_command do
commands
until
Structure
until test_command do
done
commands
Example: secretname=jenny name=noname until [ $name = $secretname ] do echo Your guess: \c read name done
Bourne Shell
1-17
The break statement transfer control to the statement AFTER the done statement terminate execution of the loop
The continue statement Transfer control to the statement TO the done statement Skip the test statements for the current iteration Continues execution of the loop
Bourne Shell 1-18
Example:
for index in 1 2 3 4 5 6 7 8 9 10 do if [ $index le 3 ]; then echo continue continue fi echo $index if [ $index ge 8 ]; then echo break break fi done
Bourne Shell 1-19
case
Structure
case test_string in pattern-1 )
commands_1
;;
pattern-2 ) commands_2
esac ;;
*)
Bourne Shell
1-20
case
Special characters used in patterns
Pattern * ? [] | Matches Matches any string of characters. Matches any single character. Defines a character class. A hyphen specifies a range of characters Separates alternative choices that satisfy a particular branch of the case structure
Bourne Shell
1-21
Example
#!/bin/sh echo \n Command MENU\n echo a. Current data and time echo b. Users currently logged in echo c. Name of the working directory\n echo Enter a,b, or c: \c read answer echo case $answer in a) date ;; b) who ;; c) pwd ;; *) echo There is no selection: $answer ;; esac
Bourne Shell
1-22
Read one line of standard input Assign each word to the corresponding variable, with the leftover words assigned to last variables If only one variable is specified, the entire line will be assigned to that variable.
Bourne Shell 1-23
Example: bundle
#!/bin/sh #bundle: group files into distribution package echo "# To Uble, sh this file" for i do echo "echo $i" echo "cat >$i <<'END of $i' " cat $i echo "END of $i" done
Will this program work for sure?
Bourne Shell
1-24
Built-in: exec
Execute a command:
Syntax:
Built-in: exec
Redirect standard output, input or error of
Example:
sh-2.05b$ more redirect.sh exec > /dev/tty echo "this is a test of redirection" sh-2.05b$ ./redirect.sh 1 > /dev/null 2 >& 1 this is a test of redirection
Bourne Shell
1-26
Example
[ruihong@dafinn ~/cs3451]$ more inter #!/bin/sh trap 'echo PROGRAM INTERRUPTED' 2 while true do echo "programming running." sleep 1 done
Bourne Shell
1-28
job control change the loop working directory display/read scan and evaluate the command execute a program exit from current shell export/ remove a val or fun compare arguments
Bourne Shell
1-29
set
shift times trap type umask wait ulimit
sends a signal to a process or job sets flag or argument promotes each command line argument displays total times for the current shell and traps a signal show whether unix command, build-in, function file creation mask waits for a process to terminate. print the value of one or more resource limits
Bourne Shell
1-30
functions
A shell function is similar to a shell script It stores a series of commands for execution at a later time. The shell stores functions in the memory Shell executes a shell function in the same shell that called it. Where to define In .profile In your script Or in command line Remove a function Use unset built-in
Bourne Shell 1-31
functions
Syntax
sh-2.05b$ whoson() >{ > date > echo "users currently logged on" > who >} sh-2.05b$ whoson Tue Feb 1 23:28:44 EST 2005 users currently logged on ruihong :0 Jan 31 08:46 ruihong pts/1 Jan 31 08:54 (:0.0) ruihong pts/2 Jan 31 09:02 (:0.0)
Bourne Shell
1-32
Example
sh-2.05b$ more .profile setenv() { if [ $# -eq 2 ] then eval $1=$2 export $1 else echo "usage: setenv NAME VALUE" 1>&2 fi } sh-2.05b$. .profile sh-2.05b$ setenv T_LIBRARY /usr/local/t sh-2.05b$ echo $T_LIBRARY /usr/local/t
Bourne Shell 1-33
Exercise
Lets look at some system scripts
/etc/init.d/syslog
/etc/init.d/crond
Bourne Shell
1-34
Summary
Shell is a programming language
Bourne Shell
1-35