More Shell Programming Slide 2 Simple calculation using

  • Slides: 32
Download presentation
More Shell Programming

More Shell Programming

Slide 2 Simple calculation using expr (3) Example $ cat test 6 #!/bin/sh echo

Slide 2 Simple calculation using expr (3) Example $ cat test 6 #!/bin/sh echo "Enter height of rectangle: " read height echo "Enter width of rectangle: " read width area=`expr $height * $width` echo "The area of the rectangle is $area" $ test 6 Enter height of rectangle: 10 Enter width of rectangle: 5 The area of the ractangle is 50 $ test 6 Enter height of rectangle: 10. 1 Enter width of rectangle: 5. 1 expr: non-numeric argument Does not work for floats!

Slide 3 Backquotes – Command Substitution § A command or pipeline surrounded by backquotes

Slide 3 Backquotes – Command Substitution § A command or pipeline surrounded by backquotes causes the shell to: § § Run the command/pipeline Substitute the output of the command/pipeline for everything inside the quotes § You can use backquotes anywhere: $ whoami clinton $ cat test 7 #!/bin/sh user=`whoami` numusers=`who | wc -l` echo "Hi $user! There are $numusers logged on. " $ test 7 Hi clinton! There are 6 users logged on.

Slide 4 Backquote Example (1) $ cat big #!/bin/sh dir=`pwd` big=`ls -l | sort

Slide 4 Backquote Example (1) $ cat big #!/bin/sh dir=`pwd` big=`ls -l | sort +4 | tail -1 | cut -c 55 -70` size=`ls -l | sort +4 | tail -1 | cut -c 33 -40` echo "The biggest file in $dir is $big. " echo "$big is $size bytes. " $ big The biggest file in /homes/horner/111 is letter 1 is 155 bytes. § sort +4 sorts in increasing order based on the fourth field (fields start from zero, and are separated by whitespace). § cut -cx-y cuts characters from position x to position y (the first position is 1). field 4 field 0 1 2 3 4 5 6 123456789012345678901234567890123 -r--r--r-1 horner cs 155 Feb 12 16: 00 letter 1

Slide 5 Backquote Example (2) $ wc -w letter 1 7 letter 1 $

Slide 5 Backquote Example (2) $ wc -w letter 1 7 letter 1 $ cat big 1 #!/bin/sh dir=`pwd` big=`ls -l | sort +4 | tail -1 | cut -c 55 -70` nline=`wc -l $big | cut -c 6 -8` nword=`wc -w $big | cut -c 6 -8 ` nchar=`wc -c $big | cut -c 6 -8 ` echo "The biggest file in $dir is $big. " echo "$big has $nlines, $nwords, $ncharacters. " $ big 1 The biggest file in /homes/horner/111 is letter 1 has 7 lines, 29 words, 155 characters. § wc -w counts the number of words in the file (“words” are separated by whitespace). § wc -c counts the number of characters in the file.

Slide 6 Control Flow § The shell allows several control flow statements: § §

Slide 6 Control Flow § The shell allows several control flow statements: § § § if while for

Slide 7 if § The if statement works mostly as expected: $ whoami clinton

Slide 7 if § The if statement works mostly as expected: $ whoami clinton $ cat test 7 #!/bin/sh user=`whoami` if [ $user = "clinton" ] then echo "Hi Bill!" fi $ test 7 Hi Bill! § However, the spaces before and after the square brackets [ ] are required.

Slide 8 if … then … else § The if then else statement is

Slide 8 if … then … else § The if then else statement is similar: $ cat test 7 #!/bin/sh user=`whoami` if [ $user = "clinton" ] then echo "Hi Bill!" else echo "Hi $user!" fi $ test 7 Hi horner!

Slide 9 if … else § You can also handle a list of cases:

Slide 9 if … else § You can also handle a list of cases: $ cat test 8 #!/bin/sh users=`who | wc -l` if [ $users -ge 4 ] then echo "Heavy load" elif [ $users -gt 1 ] then echo "Medium load" else echo "Just me!" fi $ test 8 Heavy load!

Slide 10 Boolean Expressions § Relational operators: -eq, -ne, -gt, -ge, -lt, -le §

Slide 10 Boolean Expressions § Relational operators: -eq, -ne, -gt, -ge, -lt, -le § File operators: -f file -d file -s file § String operators: -z -n s 1 s 1 string = s 2 != s 2 True if file exists and is not a directory True if file exists and is a directory True if file exists and has a size > 0 True True if if if the length of s 1 and s 2 are s 1 is not the string is zero string is nonzero the same different null string

Slide 11 File Operator Example $ cat test 9 #!/bin/sh if [ -f letter

Slide 11 File Operator Example $ cat test 9 #!/bin/sh if [ -f letter 1 ] then echo "We have found the evidence!" cat letter 1 else echo "Keep looking!" fi $ test 9 We have found the evidence! Ms. Lewinski: It is getting late. Please order some pizza and stop by my office. We'll tidy up a few more things before calling it a night. Thanks! Bill

Slide 12 while (1) § The while statement loops indefinitely, while the condition is

Slide 12 while (1) § The while statement loops indefinitely, while the condition is true, such as a user-controlled condition: $ cat test 11 #!/bin/sh resp="no" while [ $resp != "yes" ] do echo "Wakeup [yes/no]? " read resp done $ test 11 Wakeup [yes/no]? no Wakeup [yes/no]? yes $

Slide 13 while (2) § while can also do normal incrementing loops: $ cat

Slide 13 while (2) § while can also do normal incrementing loops: $ cat fac #!/bin/sh echo "Enter number: " read n fac=1 i=1 while [ $i -le $n ] do fac=`expr $fac * $i` i=`expr $i + 1` done echo "The factorial of $n is $fac" $ fac Enter number: 5 The factorial of 5 is 120

Slide 14 break § The break command works like in C++, breaking out of

Slide 14 break § The break command works like in C++, breaking out of the innermost loop : $ cat test 12 #!/bin/sh while [ 1 ] do echo "Wakeup [yes/no]? " read resp if [ $resp = "yes" ] then break; fi done $ test 12 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yrs/no]? yes $

Slide 15 Keyword Shell Variables § The shell sets keyword shell variables. You can

Slide 15 Keyword Shell Variables § The shell sets keyword shell variables. You can use (and change) them. HOME PATH USER SHELL PWD PRINTER The path to your home directory Directories where the shell looks for executables Your login name The name of the shell you are running The current working directory Can be loaded with your default printer

Slide 16 Keyword Example $ cat env #!/bin/sh echo "Hi $USER!" echo "Your home

Slide 16 Keyword Example $ cat env #!/bin/sh echo "Hi $USER!" echo "Your home is: $HOME" echo "Your path is: $PATH" echo "Your current directory is: $PWD" echo "Your shell is: $SHELL" echo "Your printer is: $PRINTER" $ env Hi horner! Your home is: /homes/horner Your path is: /usr/bin: . : /homes/horner/Unix/bin: . . . Your current directory is: /homes/horner/111 Your shell is: /bin/csh Your printer is: csl 3

Slide 17 Readonly Shell Variables § $0 is the name the user typed to

Slide 17 Readonly Shell Variables § $0 is the name the user typed to invoke the shell script: $ cat print 1 #!/bin/sh echo "This script is called $0" $ print 1 This script is called print 1 $. /print 1 This script is called. /print 1 $ ~/111/print 1 This script is called /homes/horner/111/print 1

Slide 18 Command Line Arguments – (1) § The command line arguments that you

Slide 18 Command Line Arguments – (1) § The command line arguments that you call a script with are stored in variables $1, $2, . . . , $9. $ cat args 1 #!/bin/sh echo "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9" $ args 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 The args are a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 § With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

Slide 19 Command Line Arguments – (2) § Example: How to write a command

Slide 19 Command Line Arguments – (2) § Example: How to write a command to swap two files? $ cat swap #!/bin/sh mv $1 /tmp/$1 mv $2 $1 mv /tmp/$1 $2 $ cat it 1 contents of file 1 $ cat it 2 contents of file 2 $ swap it 1 it 2 $ cat it 1 contents of file 2 $ cat it 2 contents of file 1 $

Slide 20 Command Line Arguments – (3) § $* lists all the command line

Slide 20 Command Line Arguments – (3) § $* lists all the command line args: $ cat args 2 #!/bin/sh echo "The args are $*" $ args 2 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 The args are a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 § $# contains the number of args: $ cat args 3 #!/bin/sh echo "The number of args is $#" $ args 3 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 The number of args is 10

Slide 21 Command Handling Argument - shift (1) § The shift command promotes each

Slide 21 Command Handling Argument - shift (1) § The shift command promotes each command line argument by one (e. g. , the value in $2 moves to $1, $3 moves to $2, etc. ) $ cat shiftargs #!/bin/sh echo "The args are 0 = $0, 1 = $1, 2 = $2" shift $ shiftargs arg 1 The args are 0 = arg 2 arg 3 shiftarg, 1 = arg 1, 2 = arg 2 shiftarg, 1 = arg 2, 2 = arg 3 shiftarg, 1 = arg 3, 2 = § The previous $1 becomes inaccessible

Slide 22 Command Handling Argument - shift (2) § Example: How to write a

Slide 22 Command Handling Argument - shift (2) § Example: How to write a general version of the swap command for two or more files? swap f 1 f 2 f 3. . . fn_1 fn <--- f 2 <--- f 3 <--- f 4 <--- fn <--- f 1

Slide 23 shift Example (1) $ cat swap #!/bin/sh orig 1=$1 mv $1 /tmp/$1

Slide 23 shift Example (1) $ cat swap #!/bin/sh orig 1=$1 mv $1 /tmp/$1 while [ $2 ] do mv $2 $1 shift done mv /tmp/$orig 1 $1 $ cat it 1 it 2 it 3 contents of file 1 contents of file 2 contents of file 3 $ swap it 1 it 2 it 3 $ cat it 1 it 2 it 3 contents of file 2 contents of file 3 contents of file 1 1 st Sample Run

Slide 24 shift Example (2) $ swap it 1 it 2 it 3 $

Slide 24 shift Example (2) $ swap it 1 it 2 it 3 $ cat it 1 it 2 it 3 contents of file 1 contents of file 2 contents of file 3 $ swap it 1 it 2 $ cat it 1 it 2 contents of file 1 2 nd Sample Run 3 rd Sample Run 4 th Sample run

Slide 25 Command Handling Argument - set § The set command sets the args:

Slide 25 Command Handling Argument - set § The set command sets the args: $ cat set 1 #!/bin/sh set yat yih saam echo "One is $1, two is $2, three is $3" $ set 1 One is yat, two is yih, three is saam § The set command is useful for moving the output of command substitution into the args: $ date Thu Feb 25 17: 06: 27 HKT 1999 $ cat day #!/bin/sh set `date` echo "Today is $3 $2 $6" $ day Today is 25 Feb 1999

Slide 26 Special variable - $$ (1) § $$ is the process ID (PID)

Slide 26 Special variable - $$ (1) § $$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive). $ cat pid #!/bin/sh echo $$ $ pid 1154 $ pid 1156 $ pid 1157 $ echo $$ 892 $ ps PID TTY 892 pts/0 TIME CMD 0: 01 csh

Slide 27 Special variable - $$ (2) § It can be used for temporary

Slide 27 Special variable - $$ (2) § It can be used for temporary file names: $ cat swap #!/bin/sh file=/tmp$$ mv $1 $file mv $2 $1 mv $file $1 $ cat it 1 it 2 contents of file 1 contents of file 2 $ swap it 1 it 2 $ cat it 1 contents of file 2 contents of file 1 $

Slide 28 for § The for statement executes a loop once for each of

Slide 28 for § The for statement executes a loop once for each of a list of possibilities: $ cat printall #!/bin/sh for file in * do if [ -f $file ] then echo "Print $file [y/n]? " read resp if [ $resp = "y" ] then lpr –Pcll 2 a $file fi fi done $ printall Print it 1 [y/n]? y Print it 2 [y/n]? n

Slide 29 Looping using for § The in clause of the for statement accepts

Slide 29 Looping using for § The in clause of the for statement accepts as many parameters as you wish in many forms: $ for i in 1 2 3 ; do echo $i ; done 1 2 3 $ for pid in `ps > do > kill -9 $pid > done kill: permission -a | tail +2 | cut -c 1 -6 | sort` denied (you will then be logout!)

Slide 30 For example (2) for index in 1 2 3 4 5 6

Slide 30 For example (2) for index in 1 2 3 4 5 6 7 8 9 10 do if [ $index –le 3 ]; then echo continue fi echo $index if [ $index –ge 8 ]; then echo “break” break fi done

Slide 31 Catch a signal: builtin trap § Built-in trap Syntax: trap ‘commands’ signal-numbers

Slide 31 Catch a signal: builtin trap § Built-in trap Syntax: trap ‘commands’ signal-numbers § Shell executes the commands when it catches one of the signals § Then resumes executing the script where it left off. § – Just capture the signal, not doing anything with it trap ‘ ‘ signal_number Often used to clean up temp files § Signals § – – – SIGHUP SIGINT SIGKILL SIGTERM SIGSTP … 1 2 9 15 24 disconnect line control-c kill with -9 default kill control-z

Slide 32 Example [ruihong@dafinn ~/cs 3451]$ cat inter #!/bin/sh trap 'echo PROGRAM INTERRUPTED' 2

Slide 32 Example [ruihong@dafinn ~/cs 3451]$ cat inter #!/bin/sh trap 'echo PROGRAM INTERRUPTED' 2 while true do echo "programming running. " sleep 1 done