A shell is a user interface. The Linux command shell is the prompt that allows you to interact with your computer with various system commands. With the right file permissions, you can set up commands in scripts to run when you want, even in the middle of the night. Linux shells can process commands in various sequences, depending on how you manage the input and output of each command. The way commands are interpreted is in part determined by variables and parameters associated with each shell. Some of these variables make up the environment that is carried over even if you change from one shell to another.
The default shell in Linux is bash, also known as the Bourne Again Shell. A number of other shells are popular with many users. As long as you have installed the appropriate RPMs, users can start any of these shells. As desired, you can change the default shell for individual users in the /etc/passwd file.
"Real" Linux administrators program their own scripts. They create scripts because they don't want to sit at their computers all the time. Scripts can allow Linux to back up directories automatically when nobody is in the office. Scripts can help Linux process databases when few people are using the system.
If you're not a programmer, don't worry-this is not as difficult as it sounds. For example, utilities related to the crontab command automate the creation of a number of different scripts. The cron system is discussed in more detail in Chapter 6.
If you're at all familiar with shell commands and programming expressions, you can find some examples of Red Hat Enterprise Linux shell programs in the /etc/cron .daily directory.
Variables can change. Parameters are set. The bash shell includes a number of standard environment variables. Their default values are shown in the output to the env command. One critical variable is the value of PATH, which you can check at the command line with the echo $PATH command. The directories listed in PATH are automatically searched when you try to run a command. For example, if you want to run the fdisk command from the /sbin directory, you could do it with the following command:
However, if the /sbin directory were in your PATH, you don't need the leading /sbin to call out the command; the following would work:
You can easily change the PATH variable. For example, if you want to add the /sbin directory to your PATH, just run the following commands:
# PATH=$PATH:/sbin # export PATH
The /sbin directory is already in the default PATH for the root user. The most common parameters are the settings associated with Linux configuration files, which are mostly located in the /etc directory. For example, the /etc/resolv.conf file uses the nameserver parameter to represent the DNS servers for your network. This is normally set to the IP address for that DNS server.
Exercise 1-3: Checking the PATH
In this exercise, you'll examine the PATH for a regular and the root user.
Log into the Linux command line interface as a regular user. If you're in the GUI, you can get to a command line login by pressing CTRL-ALT-F2. From the command prompt, run the following command and note the result:
$ echo $PATH
From the regular user command line interface, log in as the superuser. You'll need the root user's password.
$ su Password: #
Run the following command again and note the result. Compare it to the result as a regular user. Is there a difference?
# echo $PATH
Log out of Linux. If you followed steps 1, 2, and 3, you'll need to type the exit command twice to log out.
Now log into Linux as the root user. At the command prompt, run the following command again and note the result:
# echo $PATH
Observe the difference. You'll see more directories in the PATH for the root user. Now you can see why many Linux gurus who are doing heavy-duty administrative work log in as the root user. And that is why I also recommend that you log in as the root user during the RHCE and RHCT exams.
Any Linux file can be set up as an executable file. Then if the file includes a series of commands that can be interpreted by the shell, the commands in that file are executed. If you want Linux to run a script that you've created, you need to assign executable permissions. For additional information on executable files, read the information under the "Basic Security" objective later in this chapter.
It's easy to move from shell to shell. While the default Linux shell is bash, many experienced Unix users prefer the Korn shell. Once set with the set command, environment variables stay the same from shell to shell. In contrast, shell variables such as umask may change when you move from shell to shell, or even from user to user. For example, umask is typically different for regular users and the root user.
Linux uses three basic data streams. Data goes in, data comes out, and errors are sent in a different direction. These streams are known as standard input (stdin), standard output (stdout), and standard error (stderr). Normally, input comes from the keyboard and goes out to the screen, while errors are sent to a buffer. Error messages are also sent to the display (as text stream 2). In the following example, filename is stdin to the cat command:
# cat filename
When you run cat filename, the contents of that file are sent to the screen as standard output.
You can redirect each of these streams to or from a file. For example, if you have a program named database and a datafile with a lot of data, the contents of that datafile can be sent to the database program with a left redirection arrow (<). As shown here, datafile is taken as standard input:
# database < datafile
Standard input can come from the left side of a command as well. For example, if you need to scroll through the boot messages, you can combine the dmesg and less commands with a pipe:
# dmesg | less
The output from dmesg is redirected as standard input to less, which then allows you to scroll through that output as if it were a separate file.
Standard output is just as easy to redirect. For example, the following command uses the right redirection arrow (>) to send the standard output of the ls command to the filelist file.
# ls > filelist
You can add standard output to the end of an existing file with a double redirection arrow with a command such as ls >> filelist.
If you believe that a particular program is generating errors, redirect the error stream from it with a command like the following:
# program 2> err-list