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 W2k, namely cmd.com. Unix has many: 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.
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 W2k 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 W2k 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 W2k, you may add to your path using a command similar to the following:
In sh or bash, you may use two commands:
or in linux,
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 W2k, 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 W2k using the command
or in Unix using
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"
cd "\my documents\Math 300"
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 W2k, 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 W2k that the script is executable, so no further properties need to be assigned to the scripts. Thus, in W2k 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, W2k 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:
In Linux, the syntax is not much different.
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
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" (
echo "The answer was yes."
) else (
echo "The answer was no."
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."
echo "The answer was no."
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 W2k. 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
In this cas, 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.