C Shell Programming Introduction -------------------------------- Shell scripts are a powerful tool for invoking and organizing UNIX commands. They can substitute for systems programs written in a general purpose language such as C. The shell scripts may be quite simple and merely execute a sequence of commands, one after another (see the online file called Making a Simple Shell Script). This keeps you from having to remember where you are in a command sequence (do I convert to a YUV file next or rename the file??), look up commands in a manual, or do a large amount of repetitive typing. The C shell allows loops and decision sequences just like in a programming language. Scripts may read input from the keyboard, display output and have their output piped. Much can be done with the C shell. We are assuming at this point that you already know some of the basic UNIX commands and terminology. Pop quiz: ---------------------------------------------------------------------------- What is a pipe? (NOT A CYLINDRICAL TUBE!) A pipe, represented by the character "|", is a means of taking the "output" from one command or program and passing it as "input" to another command or or program. Here is an example: ls -al | wc -l This takes the output from the directory listing and INSTEAD OF DISPLAYING IT, it delivers it (through the pipe) to the command "wc" as input. If you recall, the command "wc" counts words or lines (if the "-l" argument is used it counts lines). The number of lines is then counted in "wc" and output to the screen. ---------------------------------------------------------------------------- Preliminaries: -------------- If you are logged into a window that has a prompt of "$", my friends, you are in the wrong shell! You are probably in the Bourne Shell (or Born Again SHell). Cure: type the following command to get into the C Shell csh You should now see a "%" as the prompt (unless you changed the PROMPT environment variable) Scratching the Surface: ----------------------- Lets start with a simple example. This simple shell script example will a list of unix commands. Enter the commands into a file called "ls2" like this: echo "----------------------" echo "- Enhanced Directory -" echo "----------------------" echo " " ls -x echo "----------------------" Change the permissions on the file with chmod +x ls2 and then execute the file by typing ls2 You will get a fancy directory listing such as this: ---------------------- - Enhanced Directory - ---------------------- #shellingintro# makingsimple script2 shellingintro ls2 script1 script3 shellingintro~ ---------------------- Of course, you could expand the "-"'s such that they span the whole width of the directory to make it look even nicer. Let look at another example that changes a PREDEFINED shell variable or two. Let's make a very short shell script (even shorter than the last one) that changes the prompt and the history size. Let's create a file called "script4" containing: set history = 20 set prompt = '\!% ' Now change the permissions and execute the file "script4". What the "hay" happened?? Nothing at all! Certain shell scripts won't execute. Why? Shell scripts containing system shell variable assignments need to be "source"'d to execute. Try the following command instead of "script4": source script4 Now it works! Notice that if you type "history" now that only the most recent 20 commands show up. Also, notice that your command line prompt prints the current command number. The funny stuff in single quotes seemed to do that job ('\!'). Don't worry about how it comes about, the C Shell can do some amazing things. Let us try one more example. Modify your .cshrc file by adding the following alias line: alias hi echo "get a life!" Save the file and try to execute it by typing: .cshrc DON'T CHANGE THE PERMISSIONS TO EXECUTABLE. Notice it will come back and tell you "permission denied". This, of course, is due to the fact that it doesn't have execute permission. BUT how come when you log in it works? Well, the system effectively calls the .cshrc file using the source command: source .cshrc The source command will do the trick. Now when you type "hi" the computer will respond with "get a life!"--gratitude for you. Let's try a different example. Let's make a very simple assignment to a variable in a shell script and print the value on the screen. Create the file called "script5" containing: set a = 27 echo $a Follow the rules of shell scripting by changing permissions and executing it. A blank line comes back. Why? When using variable assignments in a shell script, if there is no comment line in the beginning of the script, things go haywire. When making a shell script from now on, make sure you put a comment on the first line (this acts as an indicator for the shell script processor). A comment line starts with the "#" character. Let us change the last script and call it "script6": # script6 set a = 27 echo $a Now the shell script comes back with 27 One Interesting Example Using What We Know Before We Get More Complex --------------------------------------------------------------------- First of all type xcalc & This will pop-up a calculator. Let's write a slightly more complex shell script that will automatically kill the calculator without having to search for the process id number for the "kill" command. Create the file called "script7" with the following: # simple search and kill routine ps | awk '$4 == "xcalc" {print "kill -9 " $1}' > tmp123zxy chmod +x tmp123zxy tmp123zxy unalias rm rm tmp123zxy alias rm rm -i Now execute it and notice that the calculator will disappear. What this shell script does is create a temporary file containing the "kill" command and makes it into a shell script and executes it. Then it changes the "rm" command so that it won't prompt the user with "File script7. Remove ? (yes/no)[no] :" Then it deletes the temporary file and changes the "rm" command back so that it will prompt the user from now on again.