|
The C Shell
ObjectiveThis section teaches the student how to work within the C shell environment. The C shell is one of the three main login shells in use today, and may be the most widely used. This section teaches the student how to use the features available within the C shell.
Introduction
BackgroundThe C shell was created by Bill Joy at the University of California at Berkeley. It is probably the most widely used shell today, but as the Korn shell becomes available on more platforms it appears to be replacing the C shell. The C shell is referred to as the "csh".
Your Login FilesThere are three files that affect your environment when you log in to a Unix computer:
(1) /etc/cshrc (2) .cshrc (in your home directory) (3) .login (in your home directory) These files allow you to customize your C shell environment.
/etc/cshrc This file is created and maintained by your system administrator. It is read each time you start a new C shell.
.cshrc The .cshrc file is also read each time you start a new csh shell. You are allowed to modify this file. This file is read after the /etc/cshrc file.
.login The .login file is read only once, when you log in to your Unix system. It is read after the /etc/cshrc and .cshrc files. These files let you:
Once these files are created and saved, these changes will be in effect for you when you log out and then back in again. That way, once you've created aliases, set up your path, your terminal type, etc., your changes will take affect automatically each time you log onto the system in the future.
Example 5-1: A typical .cshrc file:
# # ----- Aliases ----- # alias h history alias rm "rm i" alias dir "ls -al | more" alias md mkdir alias rd rmdir alias vt100 "set term=vt100" alias wy60 "set term=wyse60" # # ----- Environment Control ----- # mesg y # let other users write messages to my screen notify -y # inform me when mail arrives set history = 50 # remember the last 50 commands set path = ( /usr/bin /bin /usr/ucb/bin /usr/local/bin . ) # $ _
C Shell VariablesWhat are variables used for within your shell? They typically interact with your shell to determine how your environment works, and what it appears like to you. Variables are used to customize your environment, define the appearance of your prompt, define your path, tell the system what type of terminal you are using, tell the system where your mailbox is and whether you want to be notified when you receive mail, etc. The C shell has two distinct variable types:
Pre-Defined Shell VariablesA Predefined Shell Variable is a data variable that exists when your csh is started. These variables are set in one of two ways:
set var=value or, set var These variables help you customize your environment by controlling where to look for executable files, the appearance of the prompt, whether you are notified when you receive mail, etc. Pre-defined shell variables are only meaningful within your current shell. NOTE: These variable names are all lower-case.
Important Pre-Defined Shell VariablesSome of the most important pre-defined shell variables:
history ignoreeof noclobber path prompt term
historyThe csh allows you to access and modify commands that you had previously typed at the command line. To do this, you must set up your "history". This will be covered in the following sections.
set history=100
ignoreeofignoreeof can keep you from accidently logging out by sending the computer an EOF signal. To keep from accidently logging out when typing a CTRL-D at the command line, put this line in your .cshrc file:
set ignoreeof
noclobbernoclobber can keep you from accidently destroying your existing files by redirecting input over an already-existing file.
set noclobber
pathYour path variable controls where the computer looks when you enter a command name at the command line. If you want to execute a command located in the directory /home/stu1/bin, you either have to type the full path name (such as /home/stu1/bin/dir), or else you include /home/stu1/bin in your path and the shell will look in the directory once you type dir by itself.
set path=( /usr/bin /bin /home/stu1/bin . )
promptThis variable sets the appearance of the prompt.
set prompt='yes, master: '
termThis variable defines the type of terminal you are using. If you are physically logged in to the Unix host computer on a DEC VT100, then you will want to set your terminal environment variable equal to vt100.
set term=vt100
Environment VariablesThese variables are distinct from the predefined shell variables, and they are exported automatically. These variables are set as follows:
setenv VARIABLE value
Certain environment variables have a direct relationship to predefined shell variables, and have the same values as the pre-defined variables. If one variable changes, so does the other.
HOME PATH TERM USER Other environment variables are distinct from the pre-defined variables:
DISPLAY EXINIT
DISPLAYIf you are using X-Windows, DISPLAY tells the computer where to display your new windows. If you are working on an X-terminal named "goofy", you would type:
setenv DISPLAY goofy:0 to set your DISPLAY environment variable.
Displaying VariablesTo look at the value of your environment variables, type env, setenv, or printenv. Suppose on a Unix system I type:
my_sys% env > env.list This puts a list of all my environment variables into a file called env.list. Sample env output on a Univel UnixWare system (Unix SVR4.2) from the file env.list:
MANPATH=/usr/flib/books/man LANG=c HZ=100 VISUAL=vi PATH=/usr/local/bin:/usr/bin:/usr/dbin:/usr/ldbin:/usr/ ccs/bin:/usr /X/bin:/usr/ucb:. COUNTRY=1 XGUI=MOTIF WKSHLIBDIR=/usr/X/lib/wksh HISTFILE=/home/aja1/.ksh_hist LOGNAME=aja1 MAIL=/var/mail/aja1 XDM_LOGIN=yes XWINHOME=/usr/X DESKTOPDIR=/home/aja1 DISPLAY=unix:0.0 SHELL=/usr/X/bin/wksh HOME=/home/aja1 TERM=xterm PWD=/home/aja1/nhome/docs/class TZ=:US/Eastern ENV=/usr/X/lib/wksh/olwksh.rc KEYB=us
On an (old) SCO Xenix system, you would use
HOME=/u/stu1 PATH=/bin:/usr/bin:/u/local/bin: LOGNAME=stu1 TERM=wy60 TZ=EST8EDT MAIL=/usr/spool/mail/stu1 MAILCHECK=1 SHELL=/bin/csh
Your Search PathYour search path tells the computer what directories to look in when you type a command at the system prompt. The search path is a list of directories that the computer searches, in sequence, to find your command. If the command you type in is found in your search path, it is executed. If it is not found in your search path, you will get an error message such as "csh: cmd: not found", where cmd is the name of a command not found in your search path.
Setting Your Search PathUse the set command to view your current environment variables:
sys_name% set cwd /home/stu1 home /home/stu1 path (/usr/local/bin /usr/bin /usr/ucb /usr/X/bin .) prompt sys_name% shell /bin/csh sys_name% _ One of these variables is your path variable. As shown above, the path variable can be set as follows:
set path=( /bin /usr/bin/ usr/ucb . )
Hashing and Hash TablesWhen your csh shell is started, it searches through the directories listed in your path environment variable and then builds an internal table that contains a listing of the full pathnames of all of the executable programs it finds in these directories. Then, when a command is executed the shell can find the executables within this table rather than actually searching through the path at the time you decide to execute a code. The hash table was originally designed for performance reasons. But what else does this mean to you? It means that if you create a new executable program, it will not be found, because your "path" variable will not be searched again. If you want to execute your new program you have to do one of two things: 1. Specify the full path that leads to the executable program. Assume you are in the directory "/home/stu1/bin", and you have created a new executable file called "dirlist". To execute this file, you would type either of these two commands:
my_sys% /home/stu1/bin/dirlist my_sys% ./dirlist 2. Use the rehash command. This command re-searches your path variable at the time you execute it, and then rebuilds the "hash table". In this way it will include any new executable files/programs you have created.
AliasesAn alias allows you to create your own names for commands. Aliases are used for two purposes: (1) you can rename exisitng commands, or (2) you can group commands together to create entirely new commands. This is a very powerful feature of the Unix Korn and C shells.
alias rm "rm -i" alias dir "ls -al | more" The syntax for the alias command is:
alias name value The first argument to the alias command is the name of the alias you are creating, such as dir. The second argument to the alias command is the actual command(s) that will be executed when you type in the name of the new alias. Using the examples above, when you type dir, the actual command that is executed by your C shell is "ls -al | more". In your home directory it would look something like this:
sys_name% dir total 12 drwxr-xr-x 2 stu1 class 1024 Mar 25 10:56 . drwxrwxr-x 7 root sys 96 Mar 25 10:22 .. -rw-r--r-- 1 stu1 class 0 Mar 22 14:28 .news_time -rw-r--r-- 1 stu1 class 1956 Mar 25 10:52 .profile -rw------- 1 stu1 class 222 Mar 25 10:56 .sh_history -rw-r--r-- 1 stu1 class 64 Feb 3 13:39 .vtlrc -rw-r--r-- 1 stu1 class 531 Feb 3 13:39 .vtlrc.els sys_name% _ As mentioned, you can assign new names to existing Unix commands with the alias command. Why would you want to rename an existing Unix command? How about the following example?
alias rm "rm -i" In this example, you have "safeguarded" the remove (rm) command. In this way, each time you attempt to remove a file by typing "rm filename", you will be prompted with "are you sure" before the file is removed. This gives an extra layer of protection from accidently deleting valuable files. Why else would you want to rename a Unix command? How about "grep"? If you type the command alias by itself, the C shell will list all of the currently defined aliases:
sys_name% alias dir "ls -al | more" disk "df -k" h history k9 "kill -9" md mkdir rd rmdir vt100 "set term=vt100" what "ps -ef" wy60 "set term=wyse60" sys_name% _
Other alias notes:Quotes are not necessary when you are creating aliases, but they do make your intended meaning more clear. To remove an alias while you are logged in, just type unalias. Aliases that you create will take precedence over original Unix commands in your path. For instance, if you create an alias named 'ls', it will be executed instead of the Unix 'ls' command. So, if you create an alias such as:
alias ls "echo no files found" you could really cause quite a bit of confusion the next time you type "ls" at the command line.
Command History and Command Line EditingThe C shell allows you to access commands that you previously typed. You can re-submit these commands just as they were typed the last time you used them, or you can modify them and then execute them. When using the csh, you must have an environment variable named 'history', such as that shown in the examples above, for this feature to work properly:
set history=100
Displaying the History ListTo view the list of commands that are stored in memory, just type history at the prompt:
my_sys% history 1 pwd 2 ls 3 ls ?al|more 4 who 5 ps ?ef 6 cat filename
Enlarging/Reducing the Number of Items Stored in HistoryThe history variable controls the number of items that are saved in memory so that they can be re-used. It is set as follows:
set history=20 set history=200
Re-using Existing CommandsRe-submitting of commands is done with the "bang" character - !. (Yes, it is the exclamation mark, but in Unix it is known as the bang character.) This has two general forms:
!'command-number' !'string'
Examples:
Example #1 To re-submit the last command that you typed, you would simply enter '!!'. Example #2 To submit command #3 again (ls -al|more), you would simply type !3. Example #3 To re-submit the last command that began with the letter 'l', you would type '!l'. In our example, this would execute command number 3 (ls -al|more). Example #4 To add to a command previously submitted, such as command #5, you would type '!5 | more' to execute the command "ps -ef | more". To modify (edit) previously submitted commands takes a little more work.
Input/Output Redirection With the C ShellWe examined redirection symbols in a previous section. There, we learned that the following symbols are used for redirecting standard input and standard output:
> and >> for redirecting standard output < for redirecting standard input To redirect standard error with the C shell, we use the following symbol:
>& and >>& for redirecting standard error
SummaryThis section has given you an in-depth introduction to the C shell environment. You were shown how to customize your C Shell environment by controlling your prompt, path, and term variables. You were also shown how to create aliases within the C shell to make your environment more "user friendly". "The C Shell" Exercises
1. Change your prompt variable to "yes, master: ".
2. What are the values of your TERM and HOME environment variables?
4. Hit the CTRL-D key combination. Note what happens.
If the ignoreeof variable was not set, you should have been logged off of the computer system. Now set the ignoreeof variable (enable it). Then hit the CTRL-D key combination. What happens this time?
5. Create an alias named dir that you can use that stands for the following pipeline command:
ls -aF | more
Create an alias named where that stands for
How is this syntax different than the Korn shell alias syntax?
6. What is the value of your history variable?
Enter the following commands:
cd
ls
ls -al
ps -ef
who
Use the history command to list your previous commands. (What command did you use to list your previous commands with the Korn shell?)
Use the
Use the
Note that the output scrolls off of the screen. Repeat the
|
|