16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Unix</strong> Shells<br />

! CASE Statement<br />

case test-string in<br />

pattern-1)<br />

commands<br />

; ;<br />

pattern-2)<br />

commands<br />

; ;<br />

:<br />

esac<br />

TRAP Command<br />

trap commands signal-numbers<br />

Signal Number Conditions<br />

hang up 1 disconnect phone line<br />

terminal interrupt 2 pressing interrupt key<br />

quit 3 pressing ^ \<br />

kill 9 kill command (not -9)<br />

ware terminate 15 default of kill command<br />

Example:<br />

trap '' 15 # prevent script exiting<br />

trap 'echo INTERRUPT; exit l' 2<br />

# remove temporary file on exit command<br />

trap 'rm /tmp/$$.$script 2> /dev/null ' 0<br />

Functions<br />

Functions are ei<strong>the</strong>r in ".profile" or in scripts that require <strong>the</strong>m.<br />

function-name()<br />

{<br />

commands<br />

}<br />

Example:<br />

wd()<br />

{<br />

cd $1<br />

PS1=" ['pwd'] "<br />

}<br />

Shell Programs<br />

A shell program is simply an ordinary text file containing shell commands. Shell programs are interpreted and<br />

<strong>the</strong>refore need no compiler. With one minor exception, <strong>the</strong>re is no difference between what can be typed on <strong>the</strong><br />

keyboard and what can be written into a shell program.<br />

The exception is that shell programs should start <strong>with</strong> a comment line: #!/bin/sh<br />

To ensure that if <strong>the</strong>y are executed under a different shell program such as Korn or C Shell, <strong>the</strong>y will be interpreted<br />

by <strong>the</strong> Bourne Shell.<br />

Shell program files must be made executable: chmod u+x myshellprog<br />

and can be treated like any o<strong>the</strong>r unix command.<br />

To trace a shell program, run it using -x option of sh(l}. sh -x ./myshellprog<br />

For more info on shell programming type:<br />

"man sh".<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!