Advanced C Shell Programming ---------------------------- Refer to the Expressions and Operators online document for a review of arithmetic, assignment and comparison operators. The "if-then-else" Statement ---------------------------- When it comes time to make a decision, what do you do? You formulate the possible scenarios. Let's say you are deciding to drop this class. You weigh the options you have and these are the decisions you must make: (1) If I am failing right now then I should drop. (2) else if I am doing alright now, but I really hate the class then I should probably drop (3) else if the teacher is rotten I will kind of think about dropping (4) else I will stay in the class Look at how the decisions were formulated: IF something THEN that ELSE IF some other thing THEN this ELSE IF some other other thing THEN the other thing to do ELSE do whatever This structure is the format of a C shell command called "if-then-else". There are a few formats for using this command as listed below. (1) "if-then" containing no "else" or "else if" parts: if (expression) then command1 command2 : : commandN endif (2) "if-then-else" containing no "else if" part: if (expression) then command1 command2 : : commandN else command1 command2 : : commandN endif (3) "if-then-else-if": if (expression1) then command1 command2 : : commandN else if (expression2) then command1 command2 : : commandN else if (expression3) then : : else if (expressionM) then command1 command2 : : commandN else command1 command2 : : commandN endif ** Note that you can have multiple "else-if-then" parts. Now for a few examples to help get a grasp on this command. First a simple shell script that will test to see if it is Friday and then output a message and if it is not Friday it will output something else. # csh to test for Friday and output a message set d = `date` echo "Today is " $d[1] if ($d[1] == Fri) then echo "It is time to hit Duddley's" echo "and blow off school the rest of the day" else echo "I must do this darn homework" endif A sample output is: Today is Wed I must do this darn homework Another sample output is for Friday: Today is Fri It is time to hit Duddley's and blow off school the rest of the day Let's try a sample without the "else" part, but first we introduce a new command: exit(0) This is the same command used in the shell window. If you type "exit" or "exit(0)" the window will close up or the current processes will be killed. If used in a shell program, the shell script will automatically end at that point. So exit(0) essentially means don't execute any more commands and exit the program. Here is another example: # csh for testing min and max set d = 10 set e = 15 if ($d < $e) then echo $d "is the min value" echo $e "is the max value" exit(0) endif echo $e "is the min value" echo $d "is the max value" The output is: 10 is the min value 15 is the max value As you can see, the "if-then" statement checks to see if the value of "d" is < the value of "e" and it is! So the commands inside the "if-then" and "endif" parts get executed. When it reaches "exit(0)" it stops the execution of the shell script. Let's change the script slightly: # csh for testing min and max set d = 20 set e = 15 if ($d < $e) then echo $d "is the min value" echo $e "is the max value" exit(0) endif echo $e "is the min value" echo $d "is the max value" The output is: 15 is the min value 20 is the max value Once again, the value of "d" is compared with "e" (see Comparison Operators table in Expressions and Operators handout). In the online document Intermediate C Shell Programming, two built-in shell variables were mentioned called: argv #argv The second one, #argv refers to the number of arguments on the command line. In general, if you use the # sign in front of a variable, you will get the number of fields returned. In the case of the date: Wed Sep 28 02:49:21 CDT 1994 there are 6 fields. If we used this assignment for the date: set d = `date` then we printed the date with "echo $d" command: Wed Sep 28 02:49:21 CDT 1994 We could look at how many fields are in this output by: echo $#d This outputs: 6 Here is a sample script illustrating this: # csh a simple example showing the use of #var set d = `date` echo $d echo $d[2] echo $#d This outputs: Wed Sep 28 02:49:21 CDT 1994 Sep 6 Here is another sample script using arguments: # csh using args that tests for min and max if ($#argv != 2) then echo "Usage: script20 number1 number2" exit(0) else if ($argv[1] < $argv[2]) then echo $argv[1] " < " $argv[2] else echo $argv[1] " > " $argv[2] endif endif # notice multiple if statements # can be used and of course multiple # comment lines anywhere To execute this script which we call "script20" type: script20 20 21 The two numbers are arbitrary. The output for those 2 numbers is: 20 < 21 The first line checks to see if there are 2 arguments. ($#argv statement). If not, it prints the error message and exits. If there are 2 arguments, then the script continues down and executes more commands testing the 2 numbers. The "while" Statement --------------------- The "while" statement is used for doing loops. In others words, if you have tasks you need repeatedly done for 2 hours, you give instructions to someone to do them for you like this: Hey you, yeah you, while I am out for 2 hours, I want you to clean my desk over and over until I get back. If we formulated this statement into a command, it would be of the form: while (I am out) clean desk end In the C shell, it looks like this: while (expression) command1 command2 : : commandN end Here is a sample of it's use: # csh to loop 10 times and stop set a = 0 while ($a < 10) @ a = $a + 1 echo "This is iteration" $a end This outputs: This is iteration 1 This is iteration 2 This is iteration 3 This is iteration 4 This is iteration 5 This is iteration 6 This is iteration 7 This is iteration 8 This is iteration 9 This is iteration 10 The first command the value 0 to variable a. Then the while loop begins. First it checks to see if the value is < 10. If it is, which is should be at first since it is 0, then is increments the value stored in a by the arithmetic addition statement (@ a = $a + 1). When the value of a reaches 10, it exits the while loop and jumps down to the "end" statement. Let look at one more important example. This script is a bit more complicated and essentially copies the script files in a directory. # csh to copy script files given args if ($#argv != 3) then echo "Usage: script22 newprefix low high" echo "-----" echo "newprefix: is the new filename prefix to copy to" echo "low: is the start file number" echo "high: is the end file number" exit(0) else echo "Converting" script$argv[2] "thru" script$argv[3] "to" echo " " $argv[1]$argv[2] "thru" $argv[1]$argv[3] set low = $argv[2] while ($low < $argv[3]) echo "Copying" script$low "to" $argv[1]$low cp script$low $argv[1]$low @ low = $low + 1 end endif Note the "Usage" part tells how to run this script which is called "script22". The output is for the command: script22 ee 2 5 Converting script2 thru script5 to ee2 thru ee5 Copying script2 to ee2 Copying script3 to ee3 Copying script4 to ee4