9. Shell Scripting

Note: See Shell Scripting for sample shell scripts

Shell scripting is setting up a “program” that will perform a function. In order to shell script you must be able to do the process by hand (knowledge of the process), you should also set it up so you don’t need to look at the screen to complete it. The tools you need for shell scripting: files, commands, environment info, user input, and a name for your command. To use a shell script you should put it in a /bin directory, make it executable, edit path, and run the scripts. To run a script when you are in the directory of the script and you have not set up the environment, type ./[script name], this will run the script name that is in your current directory, i.e. ./helpscript.

Sample shell script.

#! /usr/bin/sh (this tells UNIX what shell you want the script to run in)
echo $path (this command would show your current path, like pwd)
sh –x [script name] will print each command as it is executed

Then after you wrote and saved this in a directory (i.e. /export/home/joe/bin) you would need to edit your path, so that UNIX would know where to find the command. (edit .cshrc for cshell, .profile for Korn and Bourne shell)

If you don’t know what shell you are using do grep [username]/etc/passwd

For your new shell to work (after you edit the path) you will need to reboot or do the following command: ../.[profile or cshrc whichever you are using] The first . (period) tells the computer to reread, the second . (period) tells the computer your directory, and the /.profile tells it what to do the preceding to. Make sure that your script is executable, so do the following:

ls –l (to see if it has execute privilege) the output will look like this:

_rw-r—r-r— (which means that nobody has execute privilege so do a chmod u+rwx[filename] (u means user, g means group, and o means others))

echo $Path shows your path, the path tells the system where to look for commands (.profile for Bourne .cshrc for Korn, and .login for cshell)

To change the path:

To script a simple thing in Bourne shell, this is a simple way: x=[what you want it to do] (i.e. x=/export/home/joe) so if you type cd $x you would be changed to the /export/home/joe directory. X is a variable in this instance. This type of command is only good in your current shell (unless you export it) and will only last for that session.

Bourne shell scripting

Sample commands:
echo “What is your name? \c” the \c means no carriage return, the echo command just repeats what you wrote.
read [variable name] i.e. read x , this will wait for user input and will store it in the variable you specify.

(see sample shell scripts)


  Go to Chapter 8       Go to Index        Go to Chapter 10