Data may be read using the read command. Read read one line from standard input and assigns the first word to the first variable, the second word to the second variable and so on.
The format of the read command is:
read var1 var2 var3
An example:
$ cat bm1 for i do echo "Do you wish to delete $i ? " read y case $y in Y | y | Yes | YES ) rm $i esac done $ $ bm1 b* Do you wish to delete bin ? n Do you wish to delete bm1 ? y $ $ bm1 bm1: not found $
0 - shell signal when shell exits. 1 - hangup 2 - interrupt. 3 - quit 15 - software termination
$ cat guess trap 'echo " I win!!"; exit' 2 echo "Welcome to the guessing game!" echo "I will chose a number between 0 and 59." echo "You try to guess the number." sec=`date +%S` guess="" numguess=0 until [ $sec -eq "$guess" ] do echo 'Enter a guess (0-59): ' read guess numguess=`expr $numguess + 1` if [ "$guess" -lt $sec ] then echo " Higher" elif [ "$guess" -gt $sec ] then echo " Lower" elif [ "$guess" -eq $sec ] then echo " Correct!! It took you $numguess guesses" fi done $
$ guess Welcome to the guessing game! I will chose a number between 0 and 59. You try to guess the number. Enter a guess (0-59): 30 Higher Enter a guess (0-59): 45 Lower Enter a guess (0-59): 37 Lower Enter a guess (0-59): 33 Higher Enter a guess (0-59): 35 Lower Enter a guess (0-59): 34 Correct!! It took you 6 guesses $
Welcome to the guessing game! I will chose a number between 0 and 59. You try to guess the number. Enter a guess (0-59): 45 Lower Enter a guess (0-59): ^C I win!! $
The Bourne Shell has an extended syntax for substituting the value of parameters.
If var is set and non-null use its value else substitute word for it. This does not change var.
$ cat bp1 a=value b= echo example 1 ${a:-hi} echo example 2 ${b:-hi} echo example 3 ${b:-$a} $ bp1 example 1 value example 2 hi example 3 value
If var is not set or is null set it to word, then use the value. This changes the value of the var. This may not be used to set positional parameters.
$ cat bp2 a=value b= echo example 1 ${a:=hi} echo example 2 ${b:=hi} echo example 3 ${b:=$a} $ $ bp2 example 1 value example 2 hi example 3 hi
If var is set and not null use it else print word and exit from the shell.
$ cat bp3 a=value b= echo example 1 ${a:?"Error message"} echo example 2 ${b:?"Error message"} $ $ bp3 example 1 value bp3: b: Error message
If var is set and not null, substitute word. Var is not changed.
a=value b= echo example 1 ${a:+hi} echo example 2 ${b:+hi} $ $ bp4 example 1 hi example 2
A primary use of parameter substitution is when checking for default values like environmental variables.
$ hostname cochise.austin.cc.tx.us $ cat sub #!/bin/sh a=${HOST:-`hostname | sed 's/\..*$//'`} echo The host name is $a $ sub The host name is cochise $ HOST=junk $ export HOST $ sub The host name is junk
The eval command causes the shell to evaluate the line again.
$ cat bp5 a='$PATH' echo example 1 $a eval echo example 2 $a $ $ bp5 example 1 $PATH example 2 /local/bin:./bin:/usr/local:/usr/ucb:/usr/bin:/bin:.: /home/kochis:/home/kochis/bin:/etc:/local/lib:/local/staff: /local/uns:/local/bin/X11:/local/uns/X11R4:/appl/ASQL:/appl/ASQL/bin $
Eval can be used to simulate an array like:
$ cat array #!/bin/sh a01=January a02=February a03=March a04=April a05=May a06=June a07=July a08=August a09=September a10=October a11=November a12=December value=`date "+%m"` echo $value eval echo \$a$value $ ./array 03 March
The set command will display set variables, change shell options or set positional parameters.
The set command without arguments will display variables that are set:
$ set AUTHSTATE=compat BACKUP_DEVICE=/dev/rmt0 BACKUP_DIRECTORIES=/home CDPATH=/u/ask DOMAIN=tea.state.tx.us EDITOR=vi ENV=.kshrc HOME=/u/ask LANG=C LOGIN=ask LOGNAME=ask MAIL=/usr/spool/mail/ask MAILCHECK=600 OPTIND=1 PATH=/usr/bin:/usr/local/bin:/etc:/usr/sbin:/usr/ucb:/u/ask/bin:/usr/bin/X11 PS2='> ' PWD=/u/ask/Outline/course SHELL=/bin/ksh TERM=dtterm TZ=CST6CDT USER=ask
set may used to to turn off or on shell options. The most common option is the x option for debugging.
$ cat bp6 set -x a='$PATH' echo example 1 $a eval echo example 2 $a $ $ bp6 a=$PATH + echo example 1 $PATH example 1 $PATH + eval echo example 2 $PATH + echo example 2 /local/bin:./bin:/usr/local:/usr/ucb:/usr/bin:/bin:.:/home/xxam 225:/home/kochis/bin:/etc:/local/lib:/local/staff:/local/uns:/local/bin/X11:/lo cal/uns/X11R4:/appl/ASQL:/appl/ASQL/bin example 2 /local/bin:./bin:/usr/local:/usr/ucb:/usr/bin:/bin:.:/home/kochis:/ho me/kochis/bin:/etc:/local/lib:/local/staff:/local/uns:/local/bin/X11:/local/uns /X11R4:/appl/ASQL:/appl/ASQL/bin $
The set option may be used to set the positional parameters.
$ cat bp7 set `date` for i do echo $i done $ $ bp7 Tue Jul 2 10:42:35 CDT 1999
The IFS (input field separator) may be used to change the field separator before using the set command.
$ cat bp8 grep Kochis /etc/passwd a=`grep Kochis /etc/passwd` IFS=":" set $a for i do echo $i done $ $ bp kochis:guVC19th4z.6M:1123:50:Allan Kochis:/home/kochis:/bin/csh kochis guVC19th4z.6M 1123 50 Allan Kochis /home/kochis /bin/csh
I could use either methor to select the day from the date command
$ cat extract date day=`date | tr -s ' ' ':' | cut -d: -f3` echo $day set `date` day=$3 echo $day $./extract Wed Oct 17 02:04:40 CST 2001 17 17
To unset (delete a variable) use the unset command.
$ cat bp9 a=junk echo example 1 $a unset a echo example 2 $a $ $ bp9 example 1 junk example 2
Functions may be defined for the Bourne shell like:
name() { command ... }Functions allow the user to extend or customize the command set.
For example:
$ dir() > { > ls -al > } $ $ dir total 284 drwxrwxr-x 33 kochis staff 1536 Jul 2 10:52 . drwxr-xr-x 4 root system 512 Apr 23 16:38 .. -rw-r--r-- 1 kochis staff 581 Oct 4 1990 .Xdefaults -rw-r--r-- 1 kochis staff 2466 Jun 5 13:31 .Xresources -rw-r--r-- 1 kochis staff 328 Jun 7 14:23 .ask drwxr-xr-x 2 kochis staff 512 Jul 2 09:44 .bitmaps -rw-r--r-- 1 kochis staff 1210 Jun 20 09:49 .cshrc -rw------- 1 kochis staff 215 Nov 29 1989 .cshrc.dist drwx------ 2 kochis staff 512 Jul 2 11:32 .elm -rw------- 1 kochis staff 44 Nov 29 1989 .exrc.dist -rw-r--r-- 1 kochis staff 24 Jun 20 13:46 .forward
Functions may not be recursive. They cannot call themselves. In order to use the same name as an existing command, use the full name of the command. For example to refine the ls command.
$ ls() > { > /bin/ls -FC > } $ ls Cancelled.mail bp8* ftp/ pascal/ Mail/ bp9* hli/ problems/ News/ c/ infosys/ sql/ accounting/ cal/ misc/ tcp/ awk/ cerb@ nu_tpu.def tdb/ bin/ clips/ othello/ text/ bp6* cpr/ outline/ typescript bp7* fortran/ pas/ users $
Functions that you use all of the time can be placed .profile which runs at login time. Existing functions may be seen using the set command.
The environment defines several default variables. Shell scripts may use any of the environment variables. By convention most environment variables are upper case. When you log on first /etc/profile is run, then .profile in your home directory is run. Among the environment variables are:
To set an environment variable.
var=value export var
export var=value
setenv var=value
To use options in a shell script use getopts. getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options. Builtin variables OPTIND, OPTARG and USAGE.
$ cat opts #!/bin/sh while getopts ah: c do case $c in a ) flag=$c;; h ) flag=$c; count=$OPTARG;; \?) echo $USAGE exit;; esac done shift `expr $OPTIND - 1` filename=$1 if [ "$flag" = "a" ] then cat $filename elif [ "$flag" = "h" ] then head -$count $filename fi $ opts -d datafile ./opts: Not a recognized flag: d $ opts -a datafile line1 line2 line3 line4 line5 $ opts -h 3 datafile line1 line2 line3
© Allan Kochis | Last revision 10/17/2001 |