Borrowing Other People’s Programs
Lots of times, someone else has a cool program you want to be able to use. You have two approaches to getting what you want, and both are pretty easy. Suppose that your friend Tracy has a program named pornotopia in the bin directory. (No, we don’t know what it does, either.) How can you run it?
The long way
If you use the C shell, you can run the program from Tracy’s directory by typing this line:
~tracy/bin/pornotopia
If you use BASH or the Bourne or Korn shell, you can type this line:
/usr/tracy/bin/pornotopia
The easier way
Typing this long string of letters and symbols every time you want to run the program is a pain. A better way is to put in your bin directory a link to the cool program so that you can run it directly. (Links are described in Chapter 8.) You use the ln command to create a link, which makes the file appear to be in your own bin directory, too.Try the direct approach. Move to your home directory and create a link:
cd
ln ~tracy/bin/pornotopia bin/pornotopia
With any luck, this method works, creating a link from Tracy’s file to your bin directory. Give or take a quick hash or rehash , you’re all set.The ln command doesn’t work, however, if you and Tracy have files on different hard drives. (All this stuff is explained in Chapters 8 and 16.) In this case, you may get this unhelpful message: ln: different file system
If you get this message, it’s time for Plan B. UNIX systems have symbolic links that work across different hard drives (these links also are explained in Chapter 8). Try this line:
ln -s ~tracy/bin/pornotopia bin/pornotopia .
If it works, it makes a symbolic link to the file you want. You’re all set: The link to pornotopia refers to Tracy’s version. After a hash or rehash , you’re ready to go.
Using an alias
If you were named pornotopia, you probably would want an alias, too. Fortunately, the BASH, Korn, and C shells give you the ability to invent a short name for a long command. (Bourne shell users, you’re out of luck. Skip to the next section.) Time for Plan C. In the BASH and Korn shells, type
alias dobudget=’/usr/tracy/bin/pornotopia’
This line tells the shell that, when you type dobudget , you really want to run Tracy’s program. Heh, heh. To avoid inadvertent ease of use, the C shell’s alias command works in almost the same way, but it is punctuated slightly differently:
alias dobudget ‘/usr/tracy/bin/pornotopia’
(In both cases, the single quotes are optional if the command doesn’t contain any spaces or special characters, although it never hurts to use them.) You can define aliases for any frequently used one-line command. The alias can contain spaces, pipes, and anything else you can type on a command line. In BASH, for example, you can type
alias sortnprint=’sort -r bigfile | pr -2 | lpr’
This line makes the new sortnprint command sort your bigfile in reverse alphabetical order, format it in two columns with pr , and send the result to the printer. Aliases can also be useful if you are subject (as we are) to chronic miswiring of the nerves in your fingers. We always type mroe when we mean more , and the following alias fixes it:
alias mroe=more
(That’s the BASH version; the C shell has a space rather than an equal sign between mroe and more .) Aliases you type directly to the shell are lost when you log out. If you want them available permanently, you must put the alias commands in your .login or .profile file, in the same way we mentioned earlier in this chapter, in the "Your search path" sidebar.
Using a shell script
If this method doesn’t work either, try Plan D to use Tracy’s program: a one-line shell script. Although we use the ed program because it’s easier to show in a book, you should use a real editor. Start by revving up ed :
ed bin/pornotopia
You get the following helpful response, or something like it: ?bin/pornotopia
Now tell ed to add some text to the file, by typing this command:
a
You are now in append mode. Type the command line you want to include in the shell script, followed by a dot (period) on a line by itself:
/usr/tracy/bin/pornotopia
.
The dot on a line by itself switches back to ed ’s command mode. Then type this command:
w
This command writes the new shell script file and prints the size of the file. Then type the following to quit ed :
q
Type the next command to make your new shell script runnable:
chmod +x pornotopia
If necessary, give this command to tell UNIX to redo its hash table:
rehash (or hash, if that’s what your shell needs)
Now your script named pornotopia runs Tracy’s original program named pornotopia . At least one of these three plans should work for any program lying around anywhere on your system.Warning We don’t even discuss software copyrights, licenses, and ethics here, but, if you use a copyrighted program, you should pay for it unless you like to think of yourself as a thief.