unix/linux

47
1 UNIX/Linux Shell Programming

Upload: vesta

Post on 15-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

UNIX/Linux. Shell Programming. Shell Scripts. To pass parameters through the command line, codes are used when referring to the parameter in the body of the script file. Shell Scripts. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UNIX/Linux

1

UNIX/Linux

Shell Programming

Page 2: UNIX/Linux

2

Shell Scripts

To pass parameters through the command line, codes are used when referring to the parameter in the body of the script file.

Page 3: UNIX/Linux

3

Shell Scripts

Suppose that you are always typing the dir command (out of habit from DOS) when you mean to type ls –l and you want to pass a command line parameter to the scriptThe solution would be to create a script file named dirr that contained the following text:

ls -l $1

Page 4: UNIX/Linux

4

Shell Scripts

Script Codes from the command line:$1 = 1st argument$2 = 2nd argument$3 = 3rd argument$* = all arguments$0 = name of the shell script$# = number of arguments

Page 5: UNIX/Linux

5

Shell Scripts

Example:

$ cat dirrls -l $1$

Page 6: UNIX/Linux

6

Shell Scripts

Example:$ cat dirrls -l $1What happens now?

$ dirr A*$ dirr A*

Page 7: UNIX/Linux

7

Shell Scripts

Another example:

$ cat > renamemv $1 $2

And run it….

$ rename abc xyz$ rename abc xyz

Page 8: UNIX/Linux

8

echo Command

Purpose The echo statement writes character

strings to standard output.

Syntaxecho [ String ... ]

Page 9: UNIX/Linux

9

echo Command

Description Writes character strings to standard

output. Strings are separated by spaces, and

a new-line character follows the last String parameter specified.

If no String parameter is specified, a blank line (new-line character) is displayed.

Page 10: UNIX/Linux

10

echo Command

The echo command recognizes the following escape conventions:\b Displays a backspace character.\c Suppresses the new-line

character that otherwise follows the final argument in the output. All characters following the \c sequence are ignored.

Page 11: UNIX/Linux

11

echo Command

The echo command recognizes the following escape conventions:\f Displays a form-feed character.\n Displays a new-line character.\r Displays a carriage return

character.

Page 12: UNIX/Linux

12

echo Command

The echo command recognizes the following escape conventions:\t Displays a tab character.\v Displays a vertical tab character.\\ Displays a backslash character.

Page 13: UNIX/Linux

13

echo Command

Example:

$ cat > example1echo Bad Filename$$ ./example1Bad Filename

Page 14: UNIX/Linux

14

echo CommandAnother example:

$ cat > example2echo –e "\n\nI'm at lunch.\nI'll be back at 1:00.“

$ ./example2I'm at lunch.I'll be back at 1:00.

Page 15: UNIX/Linux

15

echo Command

Note: You must put the message in quotation marks if it contains escape sequences. And you must use the –e switch so echo will read the escape sequences

Page 16: UNIX/Linux

16

read Command

Purpose The read statement reads one line

from standard input and stores values in shell variables.

Syntaxread [ VariableName ... ]

Page 17: UNIX/Linux

17

read Command

Description Reads one line from standard input

and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.

Page 18: UNIX/Linux

18

read Command

Description The VariableName parameter

specifies the name of a shell variable that takes the value of one field from the line of input. The first shell variable is assigned the

value of the first field, the second shell variable specified is assigned the value of the second field, and so on, until the last field is reached.

Page 19: UNIX/Linux

19

read Command

Description If the line of standard input has more fields

than there are corresponding shell variables specified by the VariableName parameter, the last shell variable specified is given the value of all the remaining fields.

If there are fewer fields than shell variables, the remaining shell variables are set to empty strings.

Page 20: UNIX/Linux

20

read CommandExample:

$ cat > example3echo ‘Please enter the name of the file to delete:\c’read delfilerm $delfileecho $delfile Deleted$ ./example3Please enter the name of the file to delete: xyzxyz Deleted$

Page 21: UNIX/Linux

21

for Command

Purpose The for statement provides a simple

method of looping.

Syntaxfor variable in this list of values

do all the following commands until the ‘done’ statement

done

Page 22: UNIX/Linux

22

for Command

Example:

$ for x in `ls`> do> echo Filename is $x> done

Page 23: UNIX/Linux

23

if Command

Purpose The if statement provides conditional

execution of statements.

Syntaxif this command is successfulthen execute all these commands up

to the ‘fi’fi

Page 24: UNIX/Linux

24

if Command

Example:

if cd /users/smiththen echo the smith directory existsfi

Page 25: UNIX/Linux

25

if Command

Purpose The else statement adds an if-then-

else construct to if statements.

Page 26: UNIX/Linux

26

if Command

Syntaxif this command is successfulthen execute all these commands up

to the ‘else’else execute all these commands up

to the ‘fi’fi

Page 27: UNIX/Linux

27

if Command

Example

if cd /users/smiththen echo the smith directory existselse echo the smith directory does not existfi

Page 28: UNIX/Linux

28

test Command

Purpose The test command provides

additional testing in an if statement

There are 3 types of tests: tests on numerical values tests on file types tests on character strings

Page 29: UNIX/Linux

29

test Command

Tests on numerical valuesSyntax:test N <primitive> M

The primitives are: -eq -ne -gt -lt -ge -le

Page 30: UNIX/Linux

30

test Command

Example:

users=`who|wc -l`if test $users -gt 8then echo “more than 8 people are logged on”fi

Page 31: UNIX/Linux

31

test Command

Another example:

if test $# -eq 0then echo “You must give a file name after the command”else sort -k3,3 -k2,2 $1fi

Page 32: UNIX/Linux

32

test Command

Tests on File TypesSyntax:test <primitive> filename

Page 33: UNIX/Linux

33

test Command

The most common primitives are:-s check that the file exists and is not empty-f check that the file is an ordinary file (not a directory)-d check whether the file is really a directory-w check that the file is writable-r check that the file is readable-x check that the file is executable

Page 34: UNIX/Linux

34

test CommandExample:

if test -d $1then cd $1

ls -lcd

else echo “$1 directory does not exist”fi

Page 35: UNIX/Linux

35

test Command

Tests on Character StringsSyntaxtest String1 <primitive> String2

The two primitives are:= test that the strings are

equal!= test that the strings are not

equal

Page 36: UNIX/Linux

36

test Command

Example:

if test $LOGNAME != plotnickithen echo this command is restricted to john plotnickielse ...fi

Page 37: UNIX/Linux

37

test Command

Tests on Character StringsAlternate Syntaxtest <primitive> String

The two primitives are:-z check if the string has zero

length-n check if the string has non-zero

length

Page 38: UNIX/Linux

38

test Command

Example:

if test -z “$1”then echo “You must input a parameter”else ...fi

Page 39: UNIX/Linux

39

test Command

Three operators can be used to combine or negate test:

-o stands for the logical “or”-a stands for the logical “and” ! stands for negation

Page 40: UNIX/Linux

40

test CommandExample: The following shell would test to see if we had write permission on thatfile and read permission on thisfile:

if test -w $2 -a -r $1then cat $1 >> $2else echo cannot appendfi

Page 41: UNIX/Linux

41

test CommandAn alternate form (and probably more readable) is to enclose the test in square brackets rather than using the word test. if [ -x /usr/bin/fortune ] then echo /usr/bin/fortune echo fi

Page 42: UNIX/Linux

42

while Command

Purpose Provides a method of looping while a

certain condition exists.

Syntaxwhile this command is successfuldo all these commands until the

‘done’done

Page 43: UNIX/Linux

43

while CommandExample:

while test $# -gt 0do sort -k3,3 -k2,2 $1 >> bigfileshiftdone

The shift command renumbers the arguments and decrements $#.

Page 44: UNIX/Linux

44

break and continue Commands

Purpose The break statement causes the

current loop to be terminated. The continue statement causes the

rest of the commands in the current iteration of the loop to ignored and the loop restarted for another iteration.

Page 45: UNIX/Linux

45

case Command

Purpose The case statement provides an easy

way to provide a menu of choices.

Page 46: UNIX/Linux

46

case Command

Syntaxcase string instring1) if ‘string’ is the same as ‘string1’ then

execute all these commands until the ‘;;’ ;;string2) if ‘string’ is the same as ‘string2’ then

execute all these commands until the ‘;;’ ;;string3) ... etc...*) if none of the previous strings matched

then execute all these commands until the ‘;;’ ;;

esac

Page 47: UNIX/Linux

47

case Commandecho Please enter the letter for the program you wish to execute:echo ‘a - For the current date’echo ‘b - For who is logged on the system’echo ‘c - For a list of your files’read choicecase $choice ina) date;;b) who;;c) ls;;*) echo That was not a valid choice;;esac