Presentation is loading. Please wait.

Presentation is loading. Please wait.

LIN 69321 Unix Lecture 5 Unix Shell Scripts. LIN 69322 Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.

Similar presentations


Presentation on theme: "LIN 69321 Unix Lecture 5 Unix Shell Scripts. LIN 69322 Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command."— Presentation transcript:

1 LIN 69321 Unix Lecture 5 Unix Shell Scripts

2 LIN 69322 Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command 2 Description: The semicolon links two commands that are executed by the shell in simple sequential order. This means that csh will run the first command in the sequence, then once that command is finished, it will run the next command. Linking commands with semicolons is analogous to executing them as separate commands. That is, the command line below: % cd ~/public_html ; ls *.html is equivalent to this: % cd ~/public_html % ls *.html

3 LIN 69323 Command Coordination ; && || command1 && command2 Interpretation: Do command 2 only if command 1 executes successfully (and not otherwise). Description The double-ampersand links two commands as a very simple case of a conditional. This means that there is a condition under which command2 is executed: namely only if command1 executes successfully. This is only a little more complex than the semicolon operator. For example, giving a command like this: % clear && cal will attempt to run the clear command first. If and only if the clear command works (i.e., returns an exit status of 0), the shell will run the cal command. If the clear command does not work (i.e., returns a non-zero exit status), then the cal command is not even attempted.

4 LIN 69324 Command Coordination ; && || command1 || command2 Interpretation Do command 1 or command 2 (but not both). Description: || means something like "OR". The shell will attempt to execute command1 first. If and only if command1 fails (i.e., returns a non-zero exit status), the shell will run command2. If command1 exits cleanly, the shell does not even attempt running command2. For example, if you type something like this: % rm /etc/motd || echo "Sorry, can't remove file." The shell will attempt to remove the file /etc/motd first. If that works, then nothing else happens. However, if that command doesn't work for any reason (like maybe you don't have permission), the shell will run the echo command, and print the message Sorry, can't remove the file to the screen. Give it a shot to see what we're talking about.

5 LIN 69325 Shell Script File any collection of csh commands may be stored in a file, a shell script file csh can be invoked to execute the commands in that file the language used in that file is called shell script language Like other programming languages it has variables and flow control statements: e.g. if-then-else, while, for, goto.

6 LIN 69326 Invoking Shell Scripts DIRECT INTERPRETATION % csh filename [arg...] invokes the program csh to interpret the script contained in the file `filename'. INDIRECT INTERPRETATION we insert as the first line of the file #! /bin/csh (by default) Or use the information given when you execute the command % echo $SHELL /usr/local/bin/tcsh AND the file must be made executable using chmod

7 LIN 69327 A simple example: listfiles #! /usr/local/bin/tcsh echo "hello, $USER. I wish to list some files of yours" echo "listing files in the current directory, $PWD" ls # list files $USER and $PWD are standard variables defined by the csh and needn't be defined in the script the variables are expanded when the variable name is inside double quote: the shell sees the string $USER and replaces it with the variable's value then executes the command.

8 LIN 69328 Setting Variables Any programming language needs variables. You define a variable as follows: set X will set the variable X to have an empty list as its value and refer to it as follows: $X $X is used to denote the value of the variable X set X = dear

9 LIN 69329 A simple example: listfiles1 #! /usr/local/bin/tcsh set X = dear echo "hello, $X $USER. I wish to list some files of yours" echo "listing files in the current directory, $PWD" ls # list files $USER and $PWD are standard variables defined by the csh and needn't be defined in the script the variables are expanded when the variable name is inside double quote: the shell sees the string $USER and replaces it with the variable's value then executes the command.

10 LIN 693210 Command Arguments Most commands have arguments, and these are accessible via the shell variable $argv. The first parameter will be $argv[1], the second $argv[2], and so on. The number of such arguments is $#argv. Consider the following script file, named swap : #! /bin/csh set tmp = $argv[1] cp $argv[2] $argv[1] cp $tmp $argv[2] If you have files x and y, and type % swap x y then the new contents of x would be what is in y.

11 LIN 693211 Command Arguments Most commands have arguments, and these are accessible via the shell variable $argv. The first parameter will be $argv[1], the second $argv[2], and so on. The number of such arguments is $#argv. Consider the following script file, named swap : #! /usr/local/bin/tcsh set tmp = $argv[1] cp $argv[2] $argv[1] cp $tmp $argv[2] If you have files x and y, and type % swap x y then the new contents of x would be what is in y.

12 LIN 693212 Flow Control Statements Conditional If … then … endif Conditionals are commands which are executed only if a certain condition is true. Syntax of the if conditional: if ( expression ) then command endif If expression is true, then command is executed. If expression is false, command is not executed.

13 LIN 693213 Flow Control Statements Conditional If … then … endif # over100 -- checks if a number is greater than 100 if ( $1 > 100 ) then echo ”That’s a big number!” endif

14 LIN 693214 A note on Relational Operators ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to

15 LIN 693215 Flow Control Statements Conditional If expression is true, more than one command can be executed if ( expression ) then command1 # these commands are executed if expression is true command2... endif

16 LIN 693216 Flow Control Statements Conditional # over100add -- checks if a number is greater than 100, adds 1 to # it and then subtracts 2 from it. if ( $1 > 100 ) then echo ”That’s a big number!” echo "$1 is a big number." echo ”But `expr $1 + 1` is even bigger.” echo `expr $1 - 2` endif Recall: The backquotes ` around a command signify COMMAND SUBSTITUTION: The output from the backquoted command is included within the command line for another command.

17 LIN 693217 Flow Control Statements Conditional an if statement can also have a set of commands which run if expression is false, using the else command: if ( expression ) then command1 # these commands are executed if expression is true command2... else command11 # these commands are executed if expression is false command12... endif

18 LIN 693218 Flow Control Statements Conditional #! /bin/csh # string_check -- checks two strings # first, make sure the user typed in two arguments following the command if ( $# != 2 ) then echo "This script needs exactly two (2) arguments." echo "Exiting...(annoyed)" exit 666 endif # now, compare them if ( $1 == $2 ) then echo "$1 and $2 are the same, aren't they?" else echo "$1 and $2 are different, aren't they?" endif

19 LIN 693219 Flow Control Statements foreach ‘loop’ The syntax of foreach loop construct is foreach var ( wordlist ) command(s) end The command(s) is executed once for each “word” (or item) in the wordlist, and each time the variable var will contain the value of that word.

20 LIN 693220 Flow Control Statements foreach ‘loop’ % vi marks 16 - | - | - | - | - | - | - | - | - |Fred Dexter 19 - | - | - | - | A | - | - | - | - |Waqas Younis 16 A | A | A | B | - | A | A | A | B |David Gower 16 - | - | - | - | A | - | - | - | - |Mickey Stewart 20 A | A | A | A | A | A | A | A | A |Graham Thorpe

21 LIN 693221 Flow Control Statements foreach ‘loop’ % vi countmarks #!/bin/csh # counts the number of students associated with a number of # particular marks foreach number(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) set ms=`grep "^$number " marks | wc -l` if ($ms != 0) then echo “$ms ' student(s) got ' $number marks” endif end Note: set ms= will set the variable ms to have the value that corresponds to the output of the grep and wc command line utilities.

22 LIN 693222 Flow Control Statements foreach ‘loop’ % chmod +x countmarks ; countmarks 3 student(s) got 16 marks 1 student(s) got 19 marks 1 student(s) got 20 marks Recall: The semicolon ; links two commands that are executed by the shell in simple sequential order; csh will run the first command in the sequence, then once that command is finished, it will run the next command.

23 LIN 693223 Flow Control Statements foreach ‘loop’ Example: % mkdir city; cd city; mkdir sf ny la; touch sf/a ny/b la/c Note: touch is a standard Unix program used to create a blank file; it is also used to change a file's access and modification timestamps.

24 LIN 693224 Flow Control Statements foreach ‘loop’ Example: % mkdir Try; cd Try; mkdir d1 d2 d3; touch d1/a d2/b d3/c % vi check #!/bin/csh set start=`pwd` set command=$0 foreach name (`ls`) if (-d $name) then (cd $name ; $start/$command) ; echo "end of directory $name" else echo "$name is a file" endif end

25 LIN 693225 Flow Control Statements foreach ‘loop’ Example: % chmod +x check ; check check is a file c is a file end of directory la b is a file end of directory ny a is a file end of directory sf

26 LIN 693226 Del Script #! /bin/csh # usage: del * foreach name ($argv) if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?" else echo -n "delete the entire directory '${name}' (y/n/q)? " endif set ans = $< switch ($ans) case n: continue case q: exit case y: rm -r $name continue endsw end

27 LIN 693227 Del Script #! /bin/csh foreach name ($argv) # $argv shell variable for an argument of a command if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?" else echo -n "delete the entire directory '${name}' (y/n/q)? " endif set ans = $< switch ($ans) case n: continue case q: exit case y: rm -r $name continue endsw end

28 LIN 693228 Del Script #! /bin/csh foreach name ($argv) if ( -f $name ) then # tests to see if the file whose name is in $name is an ordinary file, as opposed to a directory file. echo -n "delete the file '${name}' (y/n/q)?" else echo -n "delete the entire directory '${name}' (y/n/q)? " endif set ans = $< switch ($ans) case n: continue case q: exit case y: rm -r $name continue endsw end

29 LIN 693229 A note on File Operations Using the if command, filenames can be tested for the following: -d filename true if filename is a directory -e filename true if filename exists -f filename true if filename is a text file -o filename true if you own filename -r filename true if filename is readable -w filename true if filename is writable -x filename true if filename is executable -z filename true if filename is empty

30 LIN 693230 Del Script #! /bin/csh foreach name ($argv) if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?” #The echo -n prevents a skip to a new line; the -n option of echo tells the shell not to print the newline # character, so that our answer, y/n/q, will be on the same line. else echo -n "delete the entire directory '${name}' (y/n/q)? " endif set ans = $< switch ($ans) case n: continue case q: exit case y: rm -r $name continue endsw end

31 LIN 693231 Del Script echo -n vs. echo The echo utility writes any specified operands, followed by a newline \n character The following option is available: -n Do not print the trailing newline character \n.

32 LIN 693232 Del Script #! /bin/csh foreach name ($argv) if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?” else echo -n "delete the entire directory '${name}' (y/n/q)? " endif set ans = $< # the symbol $< means the input from the keyboard switch ($ans) case n: continue case q: exit case y: rm -r $name continue endsw end

33 LIN 693233 Del Script #! /bin/csh foreach name ($argv) if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?" else echo -n "delete the entire directory '${name}' (y/n/q)?" endif set ans = $< switch ($ans) # Control flow is switched to where the first match occurs case n: continue # go to the top of the enclosing loop case q: exit case y: rm -r $name continue endsw end

34 LIN 693234 A note on SWITCH command The switch command takes the general form: switch ($variable)# Starts a switch case pattern1:# Defines a label in a switch command action breaksw case pattern2: action breaksw...... default : action endsw

35 LIN 693235 Del Script case y: rm -r $name # rm stands for "remove" and the -r option means recursively, if an argument of # rm -r is a directory, the rm -r command will remove that directory, and all the # files (and subdirectories, etc.) within it. # You can also use *, a wildcard for everything; rm will go through the current # working directory and remove all the files in it, and also through every subdirectory # and remove all the files in them. In short, # rm -r * deletes everything in the current directory and below.


Download ppt "LIN 69321 Unix Lecture 5 Unix Shell Scripts. LIN 69322 Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command."

Similar presentations


Ads by Google