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:
To add your bin directory to your PATH:
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:
echo "Physical memory installed (megabytes): $megabytes" | 5. | ![]() Pressing ![]() 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! |