Download presentation
Presentation is loading. Please wait.
Published byAndrew Skinner Modified over 9 years ago
1
1 Operating Systems Lecture 3 Shell Scripts
2
2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a comment. Comments run from # to the end of the line. 3. All shell scripts begin with the interpreter you want to use: #!/bin/sh Example: #!/bin/sh who | grep croyden exit 0
3
3 Running a shell script To run a UNIX script: 1)Type the script into a file. 2)Change the file permission to executable. 3)Execute it (by typing the filename at the prompt).
4
4 Shell Variables Shell variables are stored as strings: Example: #!/bin/sh x=1# Note: No spaces in assignment. # If space after x, thinks x is a command echo The value of x is $x # $x prints the value of variable x echo The home directory is $HOME echo The current shell is $SHELL (Note: to debug, use -x: sh -x scriptFileName This will list the commands as they are executed.)
5
5 Using Quotes Single quote: Groups together characters until end quote. $ is not processed. Example: #!/bin/sh grep Constance Royden /etc/passwd #Tries to open Royden as a file grep 'Constance Royden' /etc/passwd #Searches for Constance Royden #in passwd file x=1 echo $x#echos 1 echo '$x'#echos $x exit 0
6
6 Double Quotes Double quotes act like single quotes, except the $ is processed: #!/bin/sh x=1 echo $x#echos the value of x echo "$x"#echos the value of x address="College of the Holy Cross" echo $address#echos College of the Holy Cross echo "$address"#ditto exit 0
7
7 More Quotes Backslash (\): Places a single quote around a character: \> is the same as '>' Back quote (`): Tells shell to execute the enclosed command and insert the output here: #!/bin/sh echo There are `who | wc -l` users logged on exit 0 Try these examples out for yourself!
8
8 Evaluating Expressions Use expr to evaluate arithmetic expressions: Example: #!/bin/sh i=1 i=`expr $i + 1` #use back quote echo $i #prints out 2 exit 0
9
9 Common Mistakes Mistake 1: Leaving out expr: #!/bin/sh i=1 i=$i+1 echo $i #prints 1+1 exit 0 Mistake 2: Leaving out spaces around + #!/bin/sh i=1 i=`expr $i+1` echo $i #prints out 1+1 exit 0
10
10 Using Arguments with a Shell script Example command: >myShell arg1 arg2 arg3 arg1 is accessed by $1, arg2 is accessed by $2, etc. Question: What is accessed by $0 ? The number of arguments (not including the command) is given by $# $# for the above command line is 3.
11
11 The shift command The shift command shifts each variable down 1. $2 becomes $1, etc. $0 is not shifted. It always refers to the command name. Example command line: >cmd x y z Example script: #!/bin/sh echo $# $0 $1 $2 #prints 3 cmd x y shift echo $# $0 $1 $2 #prints 2 cmd y z shift echo $# $0 $1 $2 #prints 1 cmd z exit 0
12
12 The read command Use the read command to read in from the keyboard: Example: !/bin/sh read x y z echo "x = " $x echo "y = " $y echo "z = " $z read text echo "text = " $text exit 0 If type "I love shell programming!" at first read: $x is "I", $y is "love" and $z is "shell programming!" If type If type "I love shell programming!" at second read: $text is "I love shell programming" Use < to read in from a file: read x y z < myFile
13
13 Conditionals Example of a conditional statement: #!/bin/sh read name if ["$name" = Joe ] then echo It is Joe else echo It is "$name", not Joe fi #fi marks the end of the statement exit 0
14
14 Integer comparisons -eq (or =)is equal to -gegreater than or equal to -gtgreater than -leless than or equal to -ltless than -nenot equal to Example: if [num1 -lt num2 ] then... fi
15
15 for loops Example: #!/bin/sh for i in 1 2 3 4 5 do for j in.1.2.3.4.5 do echo $i$j done exit 0 Note: break command exits loop continue loops back to top, skipping the rest of the current iteration.
16
16 while loops Example: #!/bin/sh i=1 num=5 while [ $i -le $num ] do echo $i >> file_test i=`expr $i + 1` done exit 0 What does this do? Note: while read x loops until zero exit status not returned from read.
17
17 Defining Functions Example: #!/bin/sh nu() { who | wc -l } echo Number of users is `nu` echo Number of users is now `nu` exit 0
18
18 Writing Good Shell Scripts 1.Verify that the correct number of arguments is used: #!/bin/sh if [$# -ne 3 ] then echo "only $# parameters entered." echo "Usage: $0 arg1 arg2 arg3" exit 127 fi exit 0 2.Use good design and style (e.g. use functions and comments. 3.Return an exit status: exit 0 #things worked OK exit 127 #non-zero: error occurred. See /usr/include/sysexits.h for sample exit codes.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.