Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.

Similar presentations


Presentation on theme: "1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs."— Presentation transcript:

1 1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs

2 2 Shell Scripts Parameters/arguments - what’s on the input line Control structures –for- repetition –if … then … else- conditional execution –while/until- conditional repetition –case- do one of several alternatives test command- useful for conditions read command - set local variables from file sleep- pause for a while echo,shift, tee, exit and other utilities

3 3 Shell Scripts Positional Parameters (arguments to shell program)  $0 - name of program  $1, $2, $3…  $# - how many  $@ - all of them at once $$ - PID of process (Process Identification) $? - Return value from a command 3

4 4 variables.ex  echo \$0 is \"$0\"  echo \$1 is \"$1\"  echo \$\@ is \"$@\"  echo \$* is \"$*\"  echo number of args is $#  echo Process ID is $$  echo Exit value of the previous echo command is -$?-  ls abcxyz  echo A bad exit value might be \"$?\"  echo Done 4

5 5 More on variables ${var:-default}  If var not set, use default ${var :=default}  If var not set, set it to default and use it ${var :?error message}  If var not set, print error message (to STDERR) ${var#start}, ${var##longstart}  If left end matches start, delete it  # for shortest match, ## for longest match  ${path##*/} will remove all but file name (everything up to last slash) ${var%end}, ${var%longend}  If right end matches end, delete it  % for shortest match, % for longest match  ${filename%.*} will get rid of last file extension (last dot and everying after it) 5

6 6 Arithmetic (( … )), $(( … )), let …, $[ … ] All of these do arithmetic  $[ … ] (in textbook) is denigrated  i=1  i=$(($i+1)) $i is now 2  ((i = i + 1 )) $i is now 3  let “i = i + 1 “ $i is now 4 6

7 7 Command substitution $( … )  Can contain any pipeline  The result of the commands will replace it  echo $( wc myfile ) would be the same as just executing “wc myfile”  dt=$( date ) would save the date in $dt  list=$( cat filename | tail -10 ) $list would contain the last 10 lines of the file 7

8 8 Shell Script example cat > reverse –file name or name of program or name of shell script or command name echo $3, $2, $1 –that’s the program! ^D (to end the program) reverse one two three –prints: “three, two, one” 8

9 9 read command reads a line at a time from standard input, filling in variables with “words” (tokens separated by spaces) read num1 num2 num3 –echo :$num1:$num2:$num3: echo “prompt string” read var1 var2 –will print “prompt string” before reading variables 9

10 10 read example  echo Please type in 3 numbers.  read num1 num2 num3  echo :$num1:$num2:$num3: 10

11 11 echo Just “prints” to standard output echo hello echo “hello” echo \”hello\” echo $hello –blank hello=“Hi, there” echo $hello echo -n This line doesn’t have a newline 11

12 12 Summary: script input parameters –script parm1 parm2 parm2 interactive stdin –using echo/read combination file stdin –expects file input; uses read Flags? (script -a -fname) –just parameters, you need to check for them –try getopts command 12

13 13 Script layout and form #! path to shell or program # Description of what the script does, and any other information for users/maintainers # usage information usage=“name [-f] …” #Definitions, defaults INFILE=~isaacs/class/unix.part2/phone.txt # Read input parameters and flags, or get input (case statement? getopts? read statement?) rest of script 13

14 14 test! test! test! Test with no input and with bad input (the script should end nicely, with good messages) if stdin: try pipe, try “<“, try terminal if numbers, what happens with letters? (Dos to UNIX: ^M problem at ends of lines) debuging –echo statements (delete or comment when OK) –bash -x 14

15 15 for loop for name in word … do command list done 15

16 16 simple for example  for num in one two three  do  echo -$num-  done 16

17 17 Some test examples while test $# -gt 0 while test -n $1 if test $count -lt 10 if test “$answer” != “y” –(but what if it is “Y” or “yes”?) if test ! -r “$1” -o -f “$1” Can use “[ … ]” instead of “test”! 17

18 18 test, [ … ], [[ … ]] test … and [ … ] are exactly the same. You need spaces around the [ and ] test doesn’t allow any wildcards, but [[ … ]] does Things like [ $abc = “xyz” ] or [ 5 gt 3 ] need spaces around the operators 18

19 19 if conditional if expression then command list else command list fi * else is optional; can have elif. 19

20 20 simple if example  if true  then  echo yes  fi 20

21 21 case construct case word in pattern1) command list ;; pattern2) command list;; pattern3) command list ;; … esac 21

22 22 case example  case "$1"  in  0) echo zero ;;  1) echo one ;;  2) echo two ;;  3) echo three ;;  4) echo four ;;  5) echo five ;;  6) echo six ;;  7) echo seven ;;  8) echo eight ;;  9) echo nine ;;  *) echo “This is not an integer.”;;  esac 22

23 23 while (or until) loop while command list 1 do command list 2 done * can use until instead of while 23

24 24 While example echo "Type a line to process: " read input while test $input != "q" do echo processing input line: $input Echo "Type another line:" read input done echo Finished process 24

25 25 while true example  while true  do  myprogram  sleep 300  done 25

26 26 until example  until who | grep "$1" > /dev/null  do  sleep 30  done  echo "\t\t$1 is on the system."  exit 0 26

27 27 Some other shell commands shift - moves parameter $2->$1, etc exit [n] - leave a script, with an exit value break - exit the current for, while, or until continue - begin next iteration of loop 27

28 28 getopts getopts string name –string is option letters, with “:” if args –name is any variable name –returns OPTIND next option to process –returns OPTARG the argument, if any Will report if bad option, or missing arg Leading “:” silences error report 28

29 29 getopts example while getopts dclw option do case $option in d) date ;; c) cal ;; l) ls ;; w) who | wc -l ;; esac done 29

30 30 Another getopts example while getopts :ab:cd: name do case $name in a) aset=TRUE ;; b) bval=$OPTARG ;; c) cflag=1 ;; d) dvalue=“list $OPTARG” ;; \?) echo $usage; exit 1 ;; esac done shift $(( $OPTIND - 1 )) 30


Download ppt "1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs."

Similar presentations


Ads by Google