Creating a Simple Unix Shell Script


A shell script is a text file that contains a series of shell commands. Shell scripts are used for a wide range of tasks in Unix; Chapter 9 covers complex shell scripts that contain loops , functions, and other features associated with computer programming. But here we'll talk about simpler shell scripts, which are often just a series of command lines intended to be executed one after the other.

When you create a script you'll be using frequently, you should save it in a place where your shell normally looks for commands. This way, you can run the script by simply typing its name , as you would for any other command. The list of places where your shell looks for commands is called your PATH , and we teach you how to change your PATH in Chapter 7.

The standard place to store scripts for your personal use (as opposed to scripts intended for use by all users) is the bin directory inside your home directory. Mac OS X (as of version 10.4) ships without this directory's having been created for each user and without its being on the list of places where your shell looks for commands (your PATH ), so before we have you create a script, we show you how to create this directory and add it to your PATH . (See Chapter 7 for more on your PATH .)

To create your personal bin directory:

1.
cd

This ensures that you are in your home directory.

2.
mkdir bin

This creates a new directory called bin (a standard Unix name for directories that contain commands, scripts, or applications).

Tip

  • See Chapter 5 for more on the mkdir command.


To add your bin directory to your PATH:

1.
cd

This ensures that you are in your home directory. (You can skip this step if you have just done the task above, but it doesn't hurt to do it again.)

2.
export PATH=$PATH:~/bin

This adds the bin directory inside your home directory to the list of places your shell searches for commands. The next step takes care of doing this for all future Terminal windows you open .

3.
echo 'export PATH=$PATH:~/bin' >> .bash_profile

This command line adds a line of code to a configuration file used by your shell. Every new Terminal window you open will have the new configuration.

Be careful to type it exactly as shownthe placement of spaces and the use of single quotes must be replicated exactly. Be sure to type both greater-than signs (that way, if you happen to already have a .bash_profile, you will be appending to it, not replacing it).

The text contained inside the single quotes is added as a new line to the end of the file .bash_profile that is inside your home directory. You may check that this was successful by displaying that file with

cat bash_profile

The last line of the output should be

export PATH=$PATH:~/bin

Unix shell scripts can be written for any of the available shells , but the standard practice is to write shell scripts for the sh shell. The sh shell can be expected to behave in the same way on any Unix system.

Here is an example of creating a simple shell script that shows you a variety of status information about your computer.

To create a shell script to show system status:

1.
cd

This makes sure you are in your home directory.

2.
cd bin

This changes your current directory to the bin directory.

3.
nano status.sh

This starts up the nano editor, telling it to edit (and create) the file status.sh ( Figure 2.41 ).

Figure 2.41. This is what you see when you start the nano editor.

We name the new script with a .sh extension as a reminder that it is written using the sh scripting language.

4.
Type in the script from Figure 2.42 . Note that the highlighted lines use the backquote , or backtick , characterthis is the character, which is usually in the upper left of your keyboard (to the left of .

Figure 2.42. This is the code listing of a system-status script. Pay special attention to the backtick ( ) characters .
 #!/bin/sh # This is a comment. Comments are good. # This is my first shell script. echo "System Status Report" date echo -n "System uptime and load:" ;uptime echo -n "Operating System: " ; sysctl -n kern.ostype echo -n "OS Version: " ; sysctl -n kern.osrelease echo -n "OS Revision number: " ; sysctl -n kern.osrevision echo -n "Hostname: " ; sysctl -n kern.hostname 

 bytes=`sysctl -n hw.physmem` megabytes=`expr $bytes / 1024 / 1024` 


echo "Physical memory installed (megabytes): $megabytes"

5.


Pressing causes the nano editor to quit. nano asks if you want to save the changes you have made ( Figure 2.43 ).

Figure 2.43. The nano editor asks if you want to save changes.

6.
y

Type a y to tell nano that yes, you want to save the changes you have made.

nano then asks you to confirm the filename to save to ( Figure 2.44 ).

Figure 2.44. The nano editor asks you to confirm the filename you're using.


7.
Press .

nano exits, and you are back at a shell prompt.

8.
chmod 755 status.sh

The chmod command ( change mode ) sets the file status.sh to be an executable file. See Chapter 8, "Working with Permissions and Ownership," for more on the chmod command.

Next you will actually run the script you have created.

9.
./status.sh

Figure 2.45 shows typical output from the command. You have just created your first new command.

Figure 2.45. Typing ./status.sh shows you output from the command you have just created.
 user-vc8f9gd:~/bin vanilla$  status.sh  System Status Report Sun Mar6 23:03:28 PST 2005 System uptime and load:23:03up 4 days,1:01, 4 users, load averages: 0.45 0.19 0.09 Operating System: Darwin OS Version: 8.0.0 OS Revision number: 199506 Hostname: user-vc8f9gd.biz.mindspring.com Physical memory installed (megabytes): 640 user-vc8f9gd:~/bin vanilla$ status.sh System Status Report Sun Mar6 23:11:59 PST 2005 System uptime and load:23:11up 4 days,1:10, 4 users, load averages: 0.09 0.09 0.07 Operating System: Darwin OS Version: 8.0.0 OS Revision number: 199506 Hostname: user-vc8f9gd.biz.mindspring.com Physical memory installed (megabytes): 640 user-vc8f9gd:~/bin vanilla$ 

Welcome to Unix!



Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net