Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

12-Bash Programming - part 3

VIEWS: 47 PAGES: 22

									CSCI 330 The UNIX System
Unit XII:
Bash Programming, part 2

Bash shell control structures
• Sequence • Decision:
– If-then-else – Case

• Repetition
– do While, repeat Until – For – Select

• Functions CSCI 330 - The Unix System • Traps

2

Shell Functions
• A shell function is similar to a shell script
– stores a series of commands for execution later – 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 CSCI 330 - The Unix System – Or in command line

3

Shell Functions
• must be defined before they can be referenced • usually placed at the beginning of the script • Syntax
function function-name () { statements } CSCI 330 - The Unix System

4

Example: function
#!/bin/bash
funky () { # This is about as simple as functions get. echo "This is a funky function." echo "Now exiting funky function." } # Function declaration must precede call. funky
CSCI 330 - The Unix System
5

Example: function
#!/bin/bash fun () { # A somewhat more complex function. JUST_A_SECOND=1 let i=0 REPEATS=30 echo "And now the fun really begins." while [ $i -lt $REPEATS ] do echo "-------FUNCTIONS are fun------->" sleep $JUST_A_SECOND 6 CSCI 330 - The Unix System let i+=1

Function parameters
• Need not be declared • Arguments provided via function call are accessible inside function as $1, $2, $3, … • $# reflects number of parameters • $0 still contains name of script (not name of function)

CSCI 330 - The Unix System

7

Example: function with parameter
#! /bin/sh testfile() { if [ $# -gt 0 ]; then if [[ -f $1 && -r $1 ]]; then echo $1 is a readable file else echo $1 is not a readable file fi fi }
CSCI 330 - The Unix System
8

Example: function with parameters checkfile() {
for file do if [ -f "$file" ]; then echo "$file is a file" else if [ -d "$file" ]; then echo "$file is a directory" fi fi done

}

CSCI 330 - The Unix System

9

Local Variables in Functions
• Variables defined within functions are global, i.e. their values are known throughout the entire shell program • keyword “local” inside a function definition makes referenced variables “local” to that function
CSCI 330 - The Unix System
10

Example: function
#! /bin/bash global="pretty good variable" foo () { local inside="not so good variable" echo $global echo $inside global="better variable" }

echo $global foo

CSCI 330 - The Unix System

11

Handling signals
• Unix allows to send signal to any process
– kill -HUP 1234 – kill -KILL 1235 – kill -9 1236

• active jobs listing via
– jobs – kill -HUP %1 – kill -KILL %3 – kill -9 %2 330 - The Unix System CSCI

12

Handling signals
• Default action for most signals is to end process term: signal handler • Bash allows to install custom signal handler • Syntax:
– trap „handler commands‟ signals

• Example:
– trap „echo do not hangup‟ 1 2
CSCI 330 - The Unix System
13

Signals on Linux
lx% kill -l 1) SIGHUP 5) SIGTRAP 9) SIGKILL 13) SIGPIPE 17) SIGCHLD 21) SIGTTIN 25) SIGXFSZ 29) SIGIO 35) SIGRTMIN+1 39) SIGRTMIN+5 43) SIGRTMIN+9 SIGRTMIN+12 47) SIGRTMIN+13 14 51) SIGRTMAX-13 10 55) SIGRTMAX-9 59) SIGRTMAX-5 63) SIGRTMAX-1 2) 6) 10) 14) 18) 22) 26) 30) 36) 40) 44) SIGINT SIGABRT SIGUSR1 SIGALRM SIGCONT SIGTTOU SIGVTALRM SIGPWR SIGRTMIN+2 SIGRTMIN+6 SIGRTMIN+10 3) 7) 11) 15) 19) 23) 27) 31) 37) 41) 45) SIGQUIT SIGBUS SIGSEGV SIGTERM SIGSTOP SIGURG SIGPROF SIGSYS SIGRTMIN+3 SIGRTMIN+7 SIGRTMIN+11 4) 8) 12) 16) 20) 24) 28) 34) 38) 42) 46) SIGILL SIGFPE SIGUSR2 SIGSTKFLT SIGTSTP SIGXCPU SIGWINCH SIGRTMIN SIGRTMIN+4 SIGRTMIN+8

48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX56) SIGRTMAX-8 57) SIGRTMAX-7 60) SIGRTMAX-4 61) SIGRTMAX-3 CSCI 330 64) SIGRTMAXThe Unix System 58) SIGRTMAX-6 62) SIGRTMAX-2
14

Example: trap hangup
#! /bin/bash trap 'echo dont hangup' 1 while [ true ] do echo "try to hangup" sleep 1 done
CSCI 330 - The Unix System
15

Example: trap multiple signals
#! /bin/sh trap 'echo trap 'echo trap 'echo trap 'echo
1' 2' 3' 9' 1 2 3 9

while [ true ]; do echo -n . sleep 1 330 - The Unix System CSCI

16

Example: removing temp files
#! /bin/bash trap 'cleanup; exit' 2 3
cleanup () { /bin/rm -f /tmp/tempfile.$$.? }

for i in 1 2 3 4 5 6 7 8 do echo "$i.iteration" touch /tmp/tempfile.$$.$i CSCI 330 - The Unix System

17

Debug Shell Programs
• Debugging is troubleshooting errors that may occur during the execution of a program/script. • The following two commands can help you debug a bash shell script:
– echo – set

CSCI 330 - The Unix System

18

Debugging using “echo”
• The simplest method for debugging • Use the “echo” statement to display the contents of a variable at different points in the script. • Disadvantage: it cannot easily show you the flow in a script

CSCI 330 - The Unix System

19

• The “set” command is a shell built-in command • has options to allow flow of execution
–v option prints each line as it is read. –x option displays the command and its arguments. –n checks for syntax errors

Debugging using “set”

• Note: remove set options before release
– To turn on the option: set -xv – To turn off the options: set +xv
CSCI 330 - The Unix System
20

Example: Debugging using “set”
$ cat ZipCode #!/bin/bash set –xv read -p "Enter Zip code " Zip echo "Zip Code is: " $Zip readonly Zip read -p "Attempting to change Zip Code: " Zip echo "Zip Code is: " $Zip set +xv
$ chmod u+x ZipCode $ ZipCode read -p "Enter Zip code " Zip + read -p 'Enter Zip code ' Zip Enter Zip code 60563 echo "Zip Code is: " $Zip + echo 'Zip Code is: ' 60563 Zip Code is: 60563 readonly Zip + readonly Zip read -p "Attempting to change Zip Code: " Zip + read -p 'Attempting to change Zip Code: ' Zip Attempting to change Zip Code: 60115 ./ZipCode: Zip: readonly variable echo "Zip Code is: " $Zip + echo 'Zip Code is: ' 60563 Zip Code is: 60563
set +xv + set +xv
21

The plus sign indicates that the command is being executed

CSCI 330 - The Unix System

Summary
• Bash shell programming • Next: awk

CSCI 330 - The Unix System

22


								
To top