UNIX For Dummies [Electronic resources]

John Levine, Margaret Levine Young

نسخه متنی -صفحه : 213/ 88
نمايش فراداده

You Too Can Be a Script Writer

You can make your own commands (that is, shell scripts) and put them in your bin directory. A shell script is a text file that contains a list of shell commands — the same commands you type at the shell prompt. You can store a list of commands as a shell script and run the commands any time by typing the name of the shell script. This section tells you how.

Shelling a script

To create a shell script, use any text editor (refer to Chapter 10). Enter the commands one per line, just as you type them at the shell prompt. Save the file in your bin directory.

Here’s an example — if you frequently search for files with names that begin with budget , you probably are tired of typing this command over and over:

find . -name budget* -print (Check out Chapter 8 to see how the find command works.) Instead, you can put this command in a shell script and perhaps name the script findbud . To do it, create a text file named findbud that contains just one line: the command.

First you move to your bin directory because that’s where your programs live:

cd bin Then you use a text editor to create a text file containing the commands you want in your script. In this example, we use ed , a creepy editor, but you can use the editor of your choice instead. Type ed findbud UNIX responds with this line:

?findbud or maybe findbud: No such file or directory Either way, you are editing the findbud file. Type this command:

a This command tells ed to start appending text to the end of the findbud file. (Remember that because you’re using ed , you have to type weird commands.) Then type these two lines:

find . -name budget* -print . The dot on a line by itself tells ed to return to command mode. To save the file, type w UNIX responds with the information that you saved a file with 29 (or so) characters:

29 Quit ed by typing this command:

q You see the shell prompt again. Great! You created a shell script!

Getting your script to play

After you create the text file, you must tell UNIX it is executable — that it’s more than a mere text file. Type this line:

chmod +x findbud This line marks the findbud file as executable (it’s a script the shell can run).

Running and rehashing your script

To run the shell script, just type its name:

findbud Voil! You just created your own UNIX command! UNIX runs the find command to look for your budget files.

Technical Stuff Your search path
You can ignore this section unless you have put a command in your bin directory, and the shell can’t find it. Still reading? Sorry to hear it. The shell has a list of directories that contain commands; this list is known as the search path. On any sensible UNIX system, the bin directory is already in your search path. If not, you have to put it there. You do it in two stages: putting it in once and putting it in permanently.

To see what your current search path is, type the following line if you are using the C shell:

echo $path If you have BASH or the Bourne or Korn shell, type this line:

echo $PATH Yes, one’s uppercase and one’s lowercase. Arrgh! The C shell responds with something like this:

/bin /usr/bin /usr/ucb/bin /usr/local/bin BASH or the Bourne or Korn shell shows something like this:

/bin:/usr/bin:/usr/ucb/bin:/usr/local/bin:.

What you have to do is add your bin directory to the path.

If you use the C shell, type this magical incantation:

set path=($path ~/bin) That’s a tilde (~) in the middle. This line tells the C shell to set the path the same as the current path ($path ), plus the bin subdirectory of your home directory (~ ).

If you use BASH or the Bourne or Korn shell, type this even more magical incantation:

PATH=$PATH:$HOME/bin export PATH Note that the second time you type PATH and HOME in the first command, you include a dollar sign ($) in front of them. This line tells the Bourne or Korn shell to set the path the same as the current path ($PATH), plus the bin subdirectory of your home directory ($HOME ). Same song, different words.

Now you should be able to run your new script regardless of which directory you’re using.

This new, improved path lasts only until you log out. To put your bin directory on the path every time you log in, you must add the incantation to the end of the shell script that runs automatically whenever you log in. If you use the C shell, add it to the .login file. If you use the Bourne or Korn shell, add it to the .profile file.

Yes, these filenames begin with periods. Filenames that start with periods usually don’t show up in file listings, which is why you haven’t noticed these files in your home directory. Type the following line to list all your files, including these hidden ones:

ls -a In principle, you only have to edit the file, go to the end, and add the necessary lines. In practice, screwing up is easy, so — unless you’re feeling particularly brave — you’re probably better off asking for expert assistance.

You’re not quite finished, though. Observe what happens when you go to another directory. Type the following two commands to go to your home directory and give the findbud command there:

cd findbud UNIX may respond with this message:

findbud: Command not found.

If so, type one of these commands to get UNIX to do what you want:

hash findbud or rehash (Try the first; if it doesn’t work, try the second.) Now when you type findbud , it works.

What’s going on? Well, it’s Mr. too-smart-for-his-own-good Shell. Because programs don’t appear and disappear very often, when the shell starts up, it makes a list of all the commands it can access and where they are. Because five or six command directories frequently exist, this process saves considerable time (the alternative is to check every directory for every command every time you type one). The hash and rehash commands tell UNIX to rebuild its list (known in geekspeak as a hash table) because you have added a new command (the findbud file is really a command, remember?). If the command still doesn’t work, you have to fiddle with your search path — not a pretty job. See the nearby sidebar, "Your search path." Remember Type hash or rehash to tell the shell that you have added a new command and that you want it to rebuild its list of available commands to include this one. If you don’t give the hash or rehash command and you change directories, you can’t use the newly created shell script during this login session.

One could write an entire book about shell scripts (others have done so, in fact). The finer points naturally vary depending on which shell you use, although this explanation gives you the general idea. Shell scripts aren’t limited to one line: They can be as long as you want, which is handy when you have a long list of commands you want to run regularly.

Tip Don’t give me any arguments!
Shell scripts can be complete programs. Every shell program has lots of swell programming features you don’t want to know about. One is so useful, however, that we’re going to tell you anyway: Your shell scripts can use information from the command line. That is, if you type foogle dog pig , your script named foogle can see that you ran it saying dog and pig . The things on the line after the name of the command are called arguments. The word dog is the first argument, and pig is the second one. In shell scripts, the first argument is named $1; the second, $2; and so on. In shorthand, $* means "all the arguments." Suppose that you want to write a script named 2print that prints files in two-column format. (You do that by using the pr command, described in Chapter 9.) Create a file named 2print that contains this line:

pr -f -2 $* | lp Then use the chmod and, if necessary, hash or rehash commands to make 2print an executable script. If you want to print several files, one right after the next, in two-column format, you can type this line:

2print onefile anotherfile yetanotherfile In reality, you are saying pr -f -2 onefile anotherfile yetanotherfile | lp This line prints all three files in two-column format. (Note that you may need to use lpr rather than lp in this shell script. Refer to Chapter 9.)