-->
Page 90
this is a line this is another line
The end-of-file, or EOF character, is used to close the file, and is entered by holding down the Ctrl key and pressing the d key. Add a new feature to this simple editor by using the << redirection operator, which tells the shell that the end of the input immediately follows the << operator:
# cat >file.txt <<. This is a line of text This is another line of text . # cat file.txt This is a line of text. This is another line of text.
You'll notice that the file is closed after typing a period (.) on a line by itself, which is handier than using a control key combination to close the file. Try this approach to build a simple, self-contained database file by combining the grep command (discussed in Hour 5, "Manipulation and Searching Commands") and input redirection. If you have a list of addresses, wrap the list by putting the grep commmand at the beginning, and the end of input character string at the end of the file. Call this database db. See the following example.
# cat >db <<. egrep -i $1 <<zzzz Debby, 275 Collins Ave., Vestal NY 13850 Cathy, 1001 N. Vermont St., Arlington, VA 22003 Scotty, 2064 N. 16th St., Arlington, VA 22001 Bill, 4000 N. Pennsylvania Ave., Washington, DC 10000 Fred, Slip 417, N. Woodward Ave., Boca Raton, FL 46002 zzzz .
This text creates your short address database. The command line uses the cat command to redirect typing into the file called db until you type the end-of-input string, a period, on a line by itself. The database works by using the egrep command to read the file until the end-of-input string zzzz. The -i command line option tells the egrep command to ignore upper- and lowercase characters. The $1 string is a shell variable (discussed in the next section, representing the command-line argument to be fed to the egrep command.
Input and output redirection with the shell may be used in many different ways. This discussion continues as you're introduced to shell variables and shown some handy tricks for customizing your shell.
When you use a shell, you're running the shell in an environment that contains environment variables. Environment variables are pre-defined in various resource text files, found under your home directory, and the /etc directory. For the bash shell, the default environment
Page 91
variables are defined in the /etc/profile file.
There are many different environment variables. Use the printenv command to see a list of the variables currently in use:
# printenv ... PATH=/usr/local/bin:/bin:/usr/bin:.:/usr/X11R6/bin:/home/bball/bin HOME=/home/bball SHELL=/bin/bash ...
This hour doesn't list all of the environment variables, but you should know that one of the most important is the $PATH variable. This variable tells the shell where to find executable programs. Without this variable, you'd have to type the complete path, or directory hierarchy of a command, to run a program. For example, if you want to run the ifconfig command to check the status of your network connections, you might at first try to type its name on the command line:
# ifconfig bash: ifconfig: command not found # whereis ifconfig ifconfig: /sbin/ifconfig
As you can see, this doesn't mean that the ifconfig command doesn't exist, or isn't installed on your system, it's just that your shell doesn't know where to find the program. To run the ifconfig command, you can type the full pathname before the command, but if you need to use this program repeatedly, include its directory in the list of known paths in your shell's $PATH environment variable.
Do this at the command line by adding the /sbin directory to your $PATH variable, using the bash shell's export command:
# ifconfig bash: ifconfig: command not found # PATH=$PATH:/sbin ; export PATH # ifconfig lo Link encap:Local Loopback inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0 UP BROADCAST LOOPBACK RUNNING MTU:3584 Metric:1 RX packets:436 errors:0 dropped:0 overruns:0 TX packets:436 errors:0 dropped:0 overruns:0
As you can see, your shell now knows where to find the ifconfig command. But this is only temporary, and only lasts as long as you're logged in, or running a particular terminal. Make this change effective for each time you log in by adding the path to the file .bash_profile in your home directory, or if you're the root operator and want all users to benefit, to the file profile under the /etc directory.
Look for the line
PATH=$PATH:$HOME/bin
Page 92
in the .bash_profile file, and add the /sbin directory.
PATH=$PATH:$HOME/bin:/sbin
If you're the root operator, look for the line
PATH="$PATH:/usr/X11R6/bin:/usr/games:/usr/lib/games"
in the profile file under the /etc directory. In this file, you'll also see a line like the following:
PS1="[\u@\h \W]\\$ "
Although this line may seem somewhat cryptic, this is the prompt string definition for the $PS1 environment variable. You can change this string to define nearly any type of prompt string. The preceding definition is in the following form:
[username@host base_working_directory]$
The bash shell has 15 different prompts (18 for tcsh, and 35 for ksh) you can combine with character strings to customize your prompt. For example, you can have the current date and time in your prompt. If you use the bash shell export command, you can test different prompts from the command line:
# PS1='Date: \d Time: \t-> `;export PS1 Date: Sat Dec 27 Time: 20:09:53->
The time is updated each time you enter a command or press the Enter key. If you'd rather have the shell's name, along with the current directory in the command line, try the \s and \w prompt characters:
# PS1='\s:\w> `; export PS1 bash:/usr/bin>
You also can use different escape characters and terminal sequences to control the attributes of your prompt strings. You can use underlining, boldfacing, blinking, or other modes for your prompt strings:
# PS1='[\033[4m\u\033[0m@\033[4m\h\033[0m]:';export PS1 [bball@localhost]:
This example uses the proper escape sequences for the xterm X11 terminal (found with the printenv command, and by looking at the $TERMCAP variable) to change the prompt by underlining the username and hostname in the prompt. For more examples, and other prompt strings, consult your shell's manual page.
Changing the prompt is only one way to customize how you work in your shell. You also can define command shortcuts, or aliases, to tailor how your favorite commands work.