# Shells and Scripts

One particular type of process common to most computers is the "shell". This is simply the command line interface that we have been using. There is only one version of a shell on Windows, namely Powershell, which you can get using the cmd.com executable. Unix has many shells: sh (the Bourne Shell), bash (the Bourne Again Shell), csh (the C Shell), tcsh (the Trendy C Shell, or something like that...), and ksh (the Korn Shell) are but a few. You probably use bash. Bash is widely considered to be technically superior to most other shells, but for what most users do, it doesn't matter (sort of like Linux vis a vis Unix).

There are only a few places where you must interact directly with your shell in a way that depends on which shell you use. Environment variables are one such area. Every shell (including cmd.com) has certain variables that are used by the shell in its operations. The most critical is the PATH variable (both Windows and Unix). This gives a list of the directories in which the shell will look for commands that are not built-in. For example, on your Windows machine you may look in the c:\winnt\system32 directory to find a number of commands you may be familiar with, including cmd.exe and cacls.exe. Likewise, in Unix you will find most of the commands you know in the /bin and /usr/bin directories. These directories should always be in your path.

In Windows, you may add to your path using a command similar to the following:

set path=%path%;newdirectory

In sh or bash, you may use two commands:

PATH=$PATH:newdirectory export PATH or in linux, export PATH=$PATH:newdirectory

Notice the peculiar notation of the PATH variable. This variable contains whatever you want it to contain, regardless of operating system. If you want to view the contents of the variable in Windows, you should surround it by percentage signs, while in Unix, you should precede it by a dollar sign. You make view the contents of any variable called "myvariable" in Windows using the command

echo %myvariable%

or in Unix using

echo $myvariable ## Scripts All of these commands may be used as part of a file in order to automate processes. For example, consider a situation for the instructor of a course. He records grades for the course in a spreadsheet, but then saves the file in both the native format for the spreadsheet program, and also as a comma separated value file. He then runs a program that converts the comma separated file into an HTML table, and then uses ftp to send the file to a web server where his students can view it. The instructor might try to automate this process to some extent using the following script. excel "c:\my documents\Math 300\grades.xls" pause cd "\my documents\Math 300" txt2html grades.txt ftp www.myserver.wsu.edu The first command runs excel on the proper file for the instructor. The second holds the program until the instructor indicates that he is ready to continue by hitting any key (this gives him time to enter the grades and save the files). The third switches to the directory where the files are. The next command runs the program that does the conversion (which might be a script itself), and the last runs ftp for him. Thus, to carry out the entire process the instructor only needs to type the name of the script (or double-click it in the file manager), and hit any key after the grades are saved. This is the simplest kind of script one could write. Ultimately, all it does is save a few keystrokes for the instructor. Most scripts are much more powerful. They store and retrieve values, and make decisions based on conditions on the computer. To create a script, we need to edit a file. In Windows, we recommend notepad.exe or write.exe for editing the files. They are both intuitive to use. In Linux, we recommend gedit or gnp. Again, these are both simple programs to use. In Linux a script may have any name you want to give it. The only critical property of the file is that it must be executable. You can make the file called myscript executable by typing chmod +x myscript. Many people like to make their bash-shell scripts end with .sh, e.g. myscript.sh, but this is not mandatory. By contrast, in Windows scripts should end with the extension .bat. This tells Windows that the script is executable, so no further properties need to be assigned to the scripts. Thus, in Windows we might call our script "myscript.bat". The first line of a bash-shell script should be #!/bin/bash. This tells the operating system that the commands in the script should be interpreted using the bash shell, instead of a C shell or Korn shell. On the other hand, there is only one shell language in Windows, so it does not need to be told how to process the commands. However, Windows echos all commands back to you unless you tell it otherwise. Therefore, the first line of a Windows script should be @echo off. This has the effect of turning off the echo property. The initial "at symbol" tells Windows not to echo that line either. After we get the script started, we typically assign some variables containing directory names or names of the files we will work with. For example, we might be working with the grades from Math 300, so we look for all of our files in the M300 directory. That way if we are also teaching Math 171, we can use the same script for both courses by entering different variable values. To assign a variable in Windows, we use the command set: set VAR="M300" In Linux, the syntax is not much different. VAR="M300" Whenever we want to get the value "M300" out of the variable, in Windows we surround it with percent signs, while in Unix we precede it with a dollar sign. For example, to echo the value of the variable VAR, we would use the commands echo %VAR% echo$VAR

in Windows and Unix, respectively.

The real power of scripts lies in their ability to make decisions based on the existence of files and directories, the values of variables, and other factors relating to the machine they run on. In particular, it is important to be able to choose which of several actions to take in response to input or environment. Thus, every scripting language includes some sort of "if" construct. In Windows, this takes the following form.

if %VAR%=="yes" (
) else (
)

There are several things to note about this. In particular, the spaces must be placed exactly as noted. The parentheses are parentheses - not braces or brackets. The command could all be placed on one line. Indeed, the parentheses are there in order to fool the shell into thinking that the command is on one line. The use of parentheses is only obligatory for the multiline command. It is also worth noting that the the "else" part of the command only made its appearance in Windows NT - it was not available in Windows 95/98/ME.

The Unix/Bourne shell command is no less tortured. It looks like this.

if [ $VAR=="yes" ]; then echo "The answer was yes." else echo "The answer was no." fi Obviously, the ending term "fi" is "if" spelled backwards. The spaces before and after the brackets are required. The Bourne shell provides a number of other powerful constructs that have no parallel in Windows. For example, there is a "for" loop command in the Bourne shell. It looks like this. for VAR in "Linux" "Redhat" "Unix"; do echo We like$VAR
done

In this case, the first line defines a variable called VAR that takes on the values from a list, which here comprises the strings "Linux", "Redhat", and "Unix". This block of code executes three times. The first time, the variable VAR contains the string "Linux", the second time it contains "Redhat", and, obviously, the third time it contains "Unix". Thus, the output from the command is

We like Linux
We like Redhat
We like Unix

There are other commands in the Bourne shell that would be familiar to C programmers, including a "while" construct and a "case" construct. We refer you to the man pages for details.

The "final exam" for this course will take place at 8:00 AM on Tuesday, 12 December. This will be an ordinary 50 minute test. It will be comprehensive, but weighted toward the latter half of the semester. As always, paper notes will be permitted, but no electronic devices will be allowed. A sample exam is available.

A Solution example is available for the quiz. The solution to Test 1 is still available too.

The ultimate assignment is posted.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us