Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Bourne Shell Programming l Web Sources l Bourne Shell Tutorials: l l

Similar presentations


Presentation on theme: "1 Bourne Shell Programming l Web Sources l Bourne Shell Tutorials: l l"— Presentation transcript:

1 1 Bourne Shell Programming l Web Sources l Bourne Shell Tutorials: l http://steve-parker.org/sh/sh.shtml http://steve-parker.org/sh/sh.shtml l http://www.geocities.com/~gregl/htm/bourne_shell_tutorial.ht m http://www.geocities.com/~gregl/htm/bourne_shell_tutorial.ht m

2 2 Bourne Shell Programming – Topics Covered l Shell variables l Using Quotes l Arithmetic On Shell l Passing Arguments l Testing conditions l Branching n if-else, if-elif, case l Looping n while, for, until n break and continue l Using options using getopts l Reading Data l Environment and subshells l Parameter substitution n Using set l I/O redirection l Shell archives l The eval, trap, wait commands

3 3 Part 1 Some Regular Expression Characters l. (dot) – any character l ^ - beginning of line l $ - end of line l * - zero or more occurences of previous regular expression l [chars] – any character in the set of chars l [^chars] – any character not in chars. l \{min,max\} – at least min occurences and at most max occurences of the previous regular expression.

4 4 2 Shell Scripts l Bourne Shell/Korn Shell l Invoking a shell script $shell_script_file or $sh -options shell_script_file n the script file must have execute-permission. l Shell Variables n mydir=/usr/jsmith/bin n count=#assign a null value to the variable n echo $mydir#display the contents of mvdir n x=* n echo $x#substitutes the names of the files in the directory #name of a command, options and arguments can be stored inside variables command=ls option=-l filename=namesFile $command $option $filename #shell performs the variable substitution before it # executes the command.

5 5 Quotes l The Single Quote n The white-space characters enclosed between the single quotes are preserved by the shell. n The special characters are ignored. n Example: filename=/usr/jsmith/bin/prog1 echo $filename echo ‘$filename’ echo ‘<> | ; () {} ` & “‘ l The Double Quote n The special characters, $, back quotes (`) and back slashes (\) are not ignored. n Example; n x=* n echo $x # filenames are substituted n echo ‘$x’ #$x is displayed n echo “$x” # * is displayed, variable substitution is done inside the double quotes, no file name substitution is done and * is passed to the shell.

6 6 Quotes l The Back Quote n purpose is to tell the shell to execute the enclosed command and to insert the standard output from the command at that point on the command line. n Example: echo The date and time is: `date` echo There are `who | wc -l` users logged on filelist=`ls` echo $filelist (#what is the output) mail `sort -u names` < memo #-u option removes the duplicate # entries from the file l The Back Slash n Is same as putting single quotes around a single character. n Quotes the single character that immediately follows it. n X=* n echo \$x # $x is displayed n Is interpreted inside the double quotes. n Use backslash inside the double quotes to remove the meaning of characters that otherwise would be interpreted. n Examples: echo “\$x” #$x is displayed echo “The value of x is \”$x\”” #The value of x is “5” is displayed

7 7 Arithmetic On Shell l A variable is just considered a string of characters. n Example: n x=1 n x=$x+1 n echo $x #will display 1+1 n A unix program expr evaluates an expression given to it on the command line. n Each operator and operand given to expr must be a separate argument. The operators, +, -, *, /, % are recognized. n Example: i=1 i=`expr $i + 1` n Evaluates only integer arithmetic expressions. n awk may be used for floating point calculations. expr 10 * 2 # what is the problem with this?

8 8 Passing Arguments l $#: Number of arguments passed to the program from the command line. l $* : references all the arguments Example: %cat showArgs echo $# arguments passed. echo they are :$*: %showArgs a b c d 1 #output - %showArgs “a b c d 1” #output - % showArgs `cat names` #output - % showArgs x* #output - %cat lookup grep $1 phonebook lookup “Mary Jones” What is the result?

9 9 Positional Parameters l Positional parameters n set shell script arguments. e.g. $my_script a b xy “1 2 3” n positional parameters have the values $0 -- my_script $1 -- a $2 -- b $3 -- xy $4 -- 1 2 3 n $* - references all the variables passed as arguments

10 10 The shift command l shift n left-shifts the positional parameters. n If more than 9 arguments are supplied, arguments 10 and up cannot be referenced. n use shift to access these arguments. n shift assigns value in $2 into $1, $3 into $2 etc. n The number of arguments ($#) gets decremented by one on each shift. %cat testshift echo $# $* shift echo $# $* shift echo $# $* % cat testshift 1 2 3 4 5 6 7 8 9 10 What is the output?

11 11 Testing Conditions l if statement: allows to test a condition and branch on the exit status of the condition tested. l An exit status of 0 indicates the program executed successfully. l An exit status of non-zero indicates a failure. l $?: contains the exit status of the last command executed. l Operators for integer comparisons n eq (equal), -ge (greater than or equal), -gt (greater than), le (less than or equal), -lt (less than) and –ne (not equal) l Operators for string comparisons n =, !=, -n (string is not null) and –z (string is null) l File operators n -d file file is a directory n -f file file is an ordinary file n -r file file is readable by the process n -s file is of non-zero length

12 12 Testing Conditions Examples user=$1 who | grep “^$user” > /dev/null - the exit status of the last command in the pipe line is returned.

13 13 The test command l The test command is used to test one or more conditions in an if statement. y=ABC test "$y" = ABC echo $? # displays 0 x= test -n x#checks if x is not null echo $?#displays 1 test -z x#checks if string is null echo $? x=ABC [ "$x" = ABC ]#[] same as using test [ ! "$x" = ABC ] x=5 # -a for logical and -o for logical or [ "$x" -ge 0 -a "$x" -lt 10 ] [ -f “$file1” -a -r “$file1” ]

14 14 Branching %cat isUserOn #checks if a user is logged on User=$1 if who | grep “$user”#what is the problem with matching a username in the output of who? then echo “$user is logged on” fi ======================================== if [ “$#” -ne 1 ] #checking for the correct number of arguments then echo “Incorrect number of args” exit 1#terminates the program with the exit status fi if [“$NAME” = “John Doe” -a “$BAL” -gt 5000] then echo “ok” else echo “not ok” fi

15 15 Using case l case: allows a comparison of a single character against other values and execute a command if a match is found. %cat ctype x=A case "$x“# The value in x is compared with each of the cases #until a match is found. When a match is found, the #commands up to the double colons are executed. in [0-9] ) echo digit;; [A-Z] ) echo uppercase;; [a-z ) echo lowercase;; * ) echo special character;; esac Exercise: Can you rewrite the script passing the value to be tested as an argument?

16 16 The && and || constructs l command1 && command2 n if the exit status of command 1 is 0 then command 2 is executed. n Example EDITOR=[ -z "$EDITOR" ] && EDITOR=/bin/ed echo "$EDITOR" l command1 || command2 n If the exit status of command 1 is not zero, then command 2 is executed. n Example: grep “$name” phonebook || echo “Couldn’t find name”

17 Debugging with a -x option l Trace the execution of any program by typing in sh -x followed by the name of the program and its arguments. l Starts up a new shell to execute the program with the debug option. l Commands are printed at the terminal as they are executed preceded by a + sign. sh -x ctype A + [ 1 -eq 1 ] + x=A + + echo A + wc -c num=2 + [ 2 -ne 1 ] + echo Enter a single character Enter a single character + exit 1

18 Looping l The for loop is executed for as many words as are included after in l for var in listofwords do commands done for i in 1 2 3 do echo $i done for file in * #substitutes all the files in the # directory do processCmd $file done

19 19 The for loop for var #uses all the arguments given to the program on the command line do command done for file in $*# Replaces $1, $2 as $1, $2 etc do x=`wc -l $file` echo There are `echo $x |cut -f1 -d’ ‘` lines in $file done for file in “$@” #Replaces $1, $2 as “$1”, “$2” etc. Should be included in double quotes do echo $file done

20 Looping while command do command1 command2 done l command1 and 2 are executed until command returns a nonzero exit status # Print command line arguments while [ “$#” -ne 0 ] do echo “$1” shift done until command do command1 command2 done l command1 and command2 are executed as long as command returns a non-zero exit status. until who | grep “^$user “ do sleep 60 done

21 Break and continue l break: to break out of a loop. l break n: to break out of n inner most loops for file do #variable error can be set to a value count=1 while [ “$count” -le 5 ] do #process file if [ -n “$error” ] #contains a value then break 2 fi count=`expr $count + 1` done l continue: the remaining commands in the loop are skipped. for file do if [ ! -f “$file” ] then echo “$file not found” continue fi cat $file done

22 11 The getopts command l The built-in command, getopts processes the command line arguments. l Format: getopts options variable l Used in designing programs which take options. l Example: n isLoggedOn mary n isLoggedOn -m mary n isLoggedOn -m -t 120 mary n isLoggedOn -t 120 -m mary l The getopts command is designed to be executed inside a loop.

23 An example using getopts

24 An example using getopts (cont)

25 25 Reading Data read variables – shell reads a line from stdin and assigns the first word to the first variable, second word to the second variable etc.If there are more words than variables, the excess words are assigned to the last variable. l Can redirect the input from a file. Examples: read x y#reads the input from the stdin read x y < names # reads the first line in names file and assigns the first word into x and the rest into y Example: while read val1 val2 do total=`expr $val1 + $val2` echo $total done

26 26 Copying a file using mycopy # check if two arguments are supplied. if not exit with an error message and usage fromFile="$1" toFile="$2" #check if the destination is a directory if [ -d "$toFile" ] then #extract the file name from the source file name and #concatenate it to the destination directory toFile=$toFile/`basename $fromFile` fi #check if a file with the destination file name exists if [ -f "$toFile" ] then echo "The file $toFile already exists. Do you want to overwrite it?" read $answer if [ "$answer" != "yes" ] then exit 0 fi cp $fromFile $toFile fi

27 27 SubShells $cat shelltest x=200 echo $x $shelltest#output is 200. The program shelltest executed in a # subshell. x=100 echo $x#output is 100 l A subshell runs in its own environment with its own local variables. l A subshell has no knowledge of the local variables in the parent shell, therefore cannot change the value of a variable in the parent shell. l The command export is used to make the variables in a shell known to all subshells executed from that point.

28 28 $cat one echo “x = $x y = $y “ $ one#x = y = are displayed since x and y have null values $ x=100 $ y=200 $ export y $ one#y=200 is displayed l Once a variable is exported, it remains exported to all the subshells that are subsequently executed. l Exported variables and their values are copied into a subshell’s environment, where they may be accessed. Any changes made to them have no effect on the variables in the parent shell. l A variable can be exported before or after it is assigned a value.

29 29 Subshells $cat one echo "x = $x" echo "y = $y" $cat two x=1 y=2 z=3 export z three $cat three echo "x = $x" echo "y = $y" echo "z = $z" $one#What is the output $y=100 $export y $one#What is the output $two# output # x = # y = #what is the output? # z = 3 why? $three #output # x = # y = 100 # z =Why?

30 30 Some Environment Variables l PS1- stores the command prompt l PS2 – stores the secondary command prompt l HOME – Stores the home directory l PATH – contains a list of directories which the shell searches whenever you type in the name of the program to be executed. l The PATH specifies the directories to be searched for programs to be executed and not for any other types of files. $echo $PATH – displays output which may look like this /bin:/usr/bin:: l CDPATH

31 31 The set command l Is used to set various shell options. l Is used to reassign the positional parameters. l Set with no arguments will give a list of all the variables that exist in your environment. l HOME, PATH, SHELL are some of the variables that are displayed. l Set can be used to reassign the positional parameters – can be used to parse the data from a file or the terminal. l Example: $cat testSet set x y z echo $1: $2: $3 echo $# echo $* $testSet x: y: z 3 x y z

32 32 Examples: $cat countWords read line #reads a line of input from the terminal set $line #each word in the input line is assigned # to a positional parameter echo $# #No. of words in the input line #A modified version of countWords read line #reads a line of input from the terminal set --$line #each word in the input line is assigned # to a positional parameter. With the – option, an input line like “-1 + 2 = “ will not cause problems. echo $#

33 33 The eval command l eval command-line – shell scans the command line twice. l Examples: $pipe=‘|’ $ls $pipe wc –l #will generate the errors - | not found wc not found –l not found The reason: Pipes and IO redirection is done before variable substitution Fix: $eval ls $pipe wc –l #will work as expected #The first time, the shell scans the line and substitutes | as the value of the pipe. Then the command line is rescanned at which point the pipe is recognized. Exercise: How do you print the last argument given to a program?

34 34 In-line Input Redirection Command << endingword #The shell will use the lines from the stdin as the input to the command until a line with endingword Example: $ wc -l <<TheEnd > Hi > This is a test > TheEnd 2

35 35 Shell Archives l One or more related shell programs can be put into a single file and shipped to a username with a mail command. l The shipped file can be “unpacked” by running the shell on it. l Example: n An archive file called shellProgs is created using the shell script, shellArchive The file shellProgs contains two shell scripts – lookup and countWords. n The file shellProgs is mailed to a username. n The user unpacks” the file shellProgs by running the shell on it – resulting in the files, lookup and countWords

36 36 $ cat shellProgs echo "Extracting script lookup" cat >lookup <<\END_OF_DATA name=$1 grep "$name" $PHONEBOOK if [ $? -ne 0 ] then echo "Name is not in the phone book" fi END_OF_DATA echo "Extracting script countWords" cat >countWords <<\END_OF_DATA read line set $line echo "NoOfWOrds = $#" END_OF_DATA

37 37 $cat shellArchive # Creates a shell archive from a set of files. The filenames are given as # command line arguments For file do echo echo “echo extracting script $file” echo “cat >$file <<\END_OF_DATA cat $file echo “END_OF_DATA” done

38 38 Grouping Commands l The semicolon allows users to string commands together on a single line as if one command was being issued. Each command within the semicolon separated list is executed by the shell in succession. l Example: n $ cd /user/jsmith; make l The parentheses, when grouped around a command, cause the command to be executed in a sub-shell. l Example: $ cd ; ls#change to the home directory and list the contents l $ cprogs shellprogs # contents of the home directory l $ (cd cprogs ; ls ); ls l a.c b.c x.c #contents of cprogs l cprogs shellprogs #contents of home directory

39 39 Grouping commands l Examples: $ line=hi $(line=bye)#execute it in a subshell $echo $line#hi $ { line=bye; }#execute it in the current shell $echo $line#bye

40 40 Parameter Substitution l Parameter substitution is a method of providing a default value for a variable in the event that it is currently null. l The construct uses a combination of braces delimiting a variable and its default. The variable and default value are separated by a keyword. l The keyword serves as a condition for when to assign the value to the variable. Supposing a script tries to de-reference a null variable, a good programmer can avoid catastrophic errors by using parameter substitution:

41 41 Examples: ${parameter} - Substitute the value of parameter. l file=prog1 l cp $file $filea # intention is to copy prog1 into prog1a.# Will not work since filea will be treated as a variable l cp $file ${file}a

42 42 ${parameter:-value} - Substitute the value of parameter if it is not null; otherwise, use value. $ result=1 $ echo "result is ${result:-2}" result is 1 $ result= $ echo "result is ${result:-2}“ #the value can be a backquoted command result is 2 $ echo "$result"

43 43 ${parameter:=value} Substitute the value of parameter if it is not null; otherwise, use value and also assign value to parameter. Example: $ result= $ echo "result is ${result:=2}" result is 2 $ echo "$result" 2

44 44 ${parameter:?value} Substitute the value of parameter if it is not null; otherwise, write value to standard error and exit. If value is omitted, then write "parameter: parameter null or not set" instead. Example: result= $ echo "result is ${result:?}" sh: result: Parameter null or not set. $ echo "result is ${result:? “result set now”}

45 45 ${parameter:+value} Substitute value if parameter is not null; otherwise, substitute nothing. Example: $ tracing=T $ echo "tracing is ${tracing:+on}" tracing is on

46 46 exec l The exec built-in command spawns its arguments into a new process and runs it in place of the currently running process. l Example if [ "${MYSCRIPT}" = "" ] then exit else if [ -x ${MYSCRIPT} ] then echo "Executing ${MYSCRIPT}" exec ${MYSCRIPT} fi

47 47 Synchronizing tasks l Since UNIX offers multitasking, commands can be sent to the background so that they are executed when the kernel finds time. l After a command is sent to the background, the shell frees itself to handle other commands. l To background a command, the user appends an ampersand, &, to it. l Example: $ find / -name junkfile.tar –print 2>/dev/null & [1] 10876#process id is printed by the shell $date#handle another command Wed Jun 21 08:46:58 PDT 2000

48 48 wait l Sometimes it may be necessary to synchronize a script with an asynchronous process, ie. a backgrounded job. l Wait is a built-in command for such synchronization. l Wait takes an optional process number as its argument and pauses the shell's execution until the specified process had terminated. l If only one process has been sent to the background, then wait can be issued without an argument. It will automatically wait for the job to complete. If more than one job is running in the background n Wait $! # waits for the last process that is sent to the background n process identifiers can be stored in variables and passed to wait to specify the job to be waited for.

49 49 wait Examples: $ find / -name junkfile.tar –print 2>/dev/null & [1] 10876#process id is printed by the shell $ Wait 10876 $processCmd1 & $pid1=$! $processCmd2 & $pid2=$! … wait $pid1 wait $pid2

50 50 trap l The pressing of DELETE key at the terminal, when a program is in execution, sends a signal to the executing program. l Using the trap command, the program can specify the action to be taken on receiving the signal. l Usage: trap commands signals, where commands are the actions to be taken on receiving the signals. l Some Commonly used signal numbers l 0Exit from Shell l 1 Hangup l 2Interrupt (eg: Delete key) l 15Software termination (sent by kill, for example)

51 51 Example Example: #!/bin/sh i=1 JUNK=junkfile trap ‘rm $JUNK$$;exit’ 2 while [ $i -le 100 ] Do # remove the file when interrupt is received echo $i >> $JUNK$$ i=`expr $i + 1` done

52 52 More On I/O l The shell permits the combination of the basic I/O constructs with a file descriptor. l A file descriptor (also known as a file handle) is a non-negative digit that points at a file. l The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. Any of these may be redirected to a file or to each other. In other words, it is quite possible to send the output from stdin and stderr to the same file. This is quite useful when a user would rather check a script's results after it has completed processing. Examples command 2> file – redirects the standard error from any command cd JUNK 2>>out #the directory JUNK does not exist cat out sh: JUNK: not found. $ cd JUNK >>out 2>&1#Change directory to JUNK, redirect stdout to the file out, and then redirect stderr to the same file that stdout uses." $ cat out sh: JUNK: not found.

53 53 Functions l The Bourne shell allows the grouping of commands into a reusable instruction set called a function. l Functions have two parts: a label and a body. The label names the function and is used to invoke the function. l Some rules in declaring a function: 1. The function label must be unique; it cannot be the same as any other variable or other function. 2. An empty set of parentheses must always follow the function label. They instruct the shell that a function is being defined. 3. Braces, {}, must be used to delimit the function's body. 4. A function must be defined before it can be used in a script.

54 54 Functions $cat fun_example1 #!/bin/sh setEnvironment () { ROOT=${PWD} LIB=${ROOT}/proglib BIN=${ROOT}/bin } echo "Trying to print environment..." echo ${ROOT} ${LIB} ${BIN} #invoking the function setEnvironment echo "Trying to print environment again..." echo ${ROOT} ${LIB} ${BIN}

55 55 $fun_example1 #output Trying to print environment... Trying to print environment again... /proglib /bin

56 56 Functions with arguments cat fun_example2 #!/bin/sh setEnvironment () { ROOT=${1} LIB=${ROOT}/proglib BIN=${ROOT}/bin } echo "Trying to print environment..." echo ${ROOT} ${LIB} ${BIN} #invoking setEnvironment with an argument setEnvironment./demos echo "Trying to print environment again..." echo ${ROOT} ${LIB} ${BIN}

57 57 $fun_example2 #output Trying to print environment... Trying to print environment again..../demos./demos/proglib./demos/bin

58 58 Returning a value l Functions return the exit status of the last command executed. l it uses the status of the last command issued. l A script controls its exit status by issuing an exit with a non- negative value. On the other hand, functions do not use exit because it is designed to terminate the shell. Instead, functions use return. l The return command stops execution of a function returning program control to the point in the script where the function was called. Script execution continues from where the function was invoked. The format of return follows return n where n is any non-negative integer. Providing a return value is optional just as providing a status code to exit is optional. If no code is given, return defaults to the value returned by the last command executed.

59 59 Functions – returning a value $cat fun_example3 #!/bin/sh isADir() { if [ ! -d ${1} ] then return 1 fi } isADir demos echo $? isADir fun2 echo $?

60 60 Functions $cat fun_example4 #!/bin/sh isADir() { if [ ! -d ${1} ] then return 1 fi } #Functions can be embedded in a test if [ "isADir ${HOME}" ] then echo "Yes $HOME is a directory" fi

61 61 Changes made in functions Changes made to variables persist beyond execution of the function. Cat fun_example5 #!/bin/sh change(){ ONE="Hi" TWO="NewVal"} ONE="Bye" echo "ONE = ${ONE}" echo "TWO = ${TWO}" change echo "ONE = ${ONE}" echo "TWO = ${TWO}"

62 62 $fun_example5 #output changesInfun ONE = Bye TWO = ONE = Hi TWO = NewVal

63 63 Including functions in multiple scripts % cat printData #!/bin/sh printArg () { echo ${1} echo } cat counterUser #!/bin/sh # execute the file printData in the # current shell. This will cause any functions # defined inside the file printData to be read in # and defined In the current shell.. printData x=0 while [ ${x} -le 10 ] do #calling function printData printArg $x x=`expr ${x} + 1` done echo "Done"

64 64 Functions l Functions are a very useful tool to allow scripters to organize actions into logical blocks. It is easier to think of a program a series of steps to perform and then to expand those steps into functions that perform the necessary actions. This is much better than trying to list every single command in order. l In addition, functions can improve a script's performance. Rather than employ functions, you might consider grouping logical blocks into subscripts which the main script uses. This technique will work just fine, but the program's execution time will take a hit. When a script calls a subscript, the shell must find the subscript on disk, open it, read it into memory, and then execute it. This process happens every time a subscript is called even though the subscript may have been used previously. l Functions are read once into memory as soon as they are declared. They have the advantage of one time read for multiple execution.

65 65 Some Environment Variables l PS1- stores the command prompt l PS2 – stores the secondary command prompt l HOME – Stores the home directory l PATH – contains a list of directories which the shell searches whenever you type in the name of the program to be executed. l The PATH specifies the directories to be searched for programs to be executed and not for any other types of files. $echo $PATH – displays output which may look like this /bin:/usr/bin:: l CDPATH

66 66 The.profile file l At the end of the login sequence, the login shell executes two special files - /etc/profile and.profile in your home directory. l /etc/profile – set up by the SysAdmin and does the tasks that the administrator wants to execute when you log in (eg: checking if you have mail) l.profile (you will get a default.profile when you get your account): gets automatically gets executed when you log in. You can include any commands that you want executed whenever you login. Example: PATH=/bin:/usr/bin:/$HOME/bin:: CDPATH=:$HOME:$HOME/progs PS1=“=>” PS2=“=  ” export PATH CDPATH PS1 PS2


Download ppt "1 Bourne Shell Programming l Web Sources l Bourne Shell Tutorials: l l"

Similar presentations


Ads by Google