-->
Previous Table of Contents Next


Aliasing Commands

Command aliasing allows you to define a name for a command. Consider this example: The man command displays Linux documentation, or man pages. To make the word help an alias, or alternative, for man, enter the following:


alias help=man

Now you can enter help cp or man cp to display Linux man pages about the cp command.

You also can use aliases with commands that have options or arguments. For example, if you want to list the names of all the files in the current directory sorted in descending order by the time they were last modified (so that the most recent files are at the bottom of the list), you can use this command:


ls -art

The ls command is the command to list files; the -a option specifies all files, the -r option arranges the files in reverse, descending order, and the -t option sorts by time last modified. That’s a lot to remember. You can assign the alias timedir to this complex command with the following command:


alias  timedir=“ls  -art”

The quotation marks (“”) are necessary because the shell expects the alias for timedir to be terminated by a space or <Return>. Now, if you enter timedir, you get the directory listing you want.


NOTE:  Setting an alias from the command line keeps that alias in effect only for the current session. To have the alias active whenever you log in, include the alias definition in the .profile file if you use the Bourne shell; keep it in the .login file if you use the C shell.

Completing Commands

Command completion allows you to type the beginning of a filename and then press the <Tab> key to expand the filename. This can save time and spelling mistakes when entering a command. If two files share a common prefix, Linux expands the command to the last common character, stops expanding the filename, and then beeps. You need to provide the unique filename.

Adding Text with Cut and Paste

Red Hat Linux and Slackware Linux both offer a program that can be started at boot time that allows you to use the mouse to select text from anywhere on-screen and then paste the text onto the command line for the shell to interpret. To get a mouse cursor, you simply press one of the mouse buttons. You then select the desired text from anywhere on-screen by first clicking with the left mouse button on the beginning of the text and, while holding down the button, dragging the cursor to the desired end point of the text. After you select the text, click with the right mouse button to copy the text to the command line.

Working with Shell Scripts

The shell accepts commands, interprets them, and arranges for the operating system to execute the commands in the manner you specify. In the previous sections, you saw how the shell interprets special characters to complete filenames, redirects input and output, connects processes through pipes, and puts jobs or processes in the background.

You can type commands at the terminal, or they can come from a file. A shell script is a collection of one or more shell commands in a file. To execute the commands, you type the name of the file. The advantages to this approach include the following:

  You don’t have to retype a sequence of commands.
  You determine the steps to accomplish a goal once.
  You simplify operations for yourself and others.

By using variables and keywords, you can write programs that the shell can interpret. This is useful because it allows you to create general shell scripts you or others can use in various situations.

Suppose that after you log in, you regularly like to see who’s logged in to your system, run a program named calendar that displays your appointments for today and tomorrow, and print the current date and time to the screen. To do all that, you enter the following commands:


who

calendar

date

If you put these three commands into a file named whatsup and make that file executable, you have a shell script that you can execute just like any other command. The file whatsup must be a text file. You can use the vi or emacs text editor to put the commands in the whatsup file. To make the file executable, enter this command:


chmod +x whatsup

The chmod command modifies or sets the permissions for a file. The +x option makes the file executable—that is, it makes the file work just like a standard Linux command. Putting commands into the file and making the file executable are both one-time operations. From that point on, you can enter whatsup to execute your shell script. You can use the shell script just like any other command. For example, to print the results of the whatsup command, enter the following:


whatsup | lp

To put the results of the whatsup command into a file named info for future reference, enter this:


whatsup > info

As a review, follow these steps to create a shell script that you can use whenever you want:

1.  Use a text editor, such as vi or emacs, to put the shell commands into a text or ASCII file. In the preceding example, the commands were put in the file named whatsup.
2.  Make it so that you have execute permission on the file. Use chmod +x filename (for example, chmod +x whatsup).
3.  Test the command by typing the name of the command and pressing <Return>.

After using this process a few times, you’ll see how easy it is to create useful scripts. Of course, the hardest part is figuring out which shell commands to use and how to use the shell’s programming capabilities to express the steps you need to carry out.

You can test a shell script and see all the steps it goes through by entering this command:


sh -x script-name

In this syntax, script-name is the name of the file that holds the script you’re considering. The sh -x command displays all the steps the script goes through and is useful when you’re trying to debug a script.


Previous Table of Contents Next