Introduction to the C Shell

The C shell is similar to other shells in that it provides a user interface to UNIX. You can use the C shell in the following three ways:

  • Interactively type commands on the command line.

  • Group commonly executed sets of commands into command files that you can execute by typing the name of the file.

  • Create C shell programs using the structured programming techniques of the C shell.

These three techniques are listed in the order in which you'll probably use them. First, you log in and use interactive commands. Then you group together commonly used commands and execute them with a single command. Finally, you may want to create sophisticated shell scripts.

In this chapter, I cover login and interactive commands and a lot of useful ways to use the C shell.

Most of the examples in this chapter are from Solaris and HP-UX systems. You probably will find both your user setup and the operation of the C shell on other systems similar to what is covered in this chapter. Much of the setup of any shell is performed by the system administrator, so you will surely find differences in your C shell setup compared with what is shown in this chapter. In general, however, the operation of the C shell is similar from one system to another.

Issuing Commands

The first activity you perform after you log in to the system is to issue commands at the prompt. A command you may want to issue immediately is ls -al. Here is what I see on my system after executing this:

sys1 7: ls -al
total 10
drwxr-x---   2 martyp2    users         96 May  5 09:34 .
drwxr-xr-x  10 root       root        1024 May  5 10:38 ..
-rw-r--r--   1 martyp2    users        814 May  5 09:34 .cshrc
-rw-r--r--   1 martyp2    users        347 May  5 09:34 .exrc
-rw-r--r--   1 martyp2    users        341 May  5 09:34 .login
-rw-r--r--   1 martyp2    users       446 May  5 09:34 .profile
sys1 8:

The C shell prompt consists of system name (sys1) followed by the command number and a colon. I cover the prompt shortly.

ls -al shows two files related to the C shell in this user area:

.cshrc and .login

Figure 27-6 shows the contents of .cshrc:

Figure 27-6. Sample .cshrc
# Default user .cshrc file (/usr/bin/csh initialization).

# Usage:  Copy this file to a user's home directory and edit it to
# customize it to taste.  It is run by csh each time it starts up.

# Set up default command search path:
#
# (For security, this default is a minimal set.)

     set path=( $path )

# Set up C shell environment:

    if ( $?prompt ) then        # shell is interactive.
        set history=20         # previous commands to remember.
        set savehist=20        # number to save across sessions.
        set system=`hostname`      # name of this system.
        set prompt = "$system !: " # command prompt.

        # Sample alias:

        alias       status  '(date; bdf)'

        # More sample aliases:

        alias  d    dirs
        alias  pd   pushd
        alias  pd2  pushd +2
        alias  po   popda
        alias  m    more
     endif

Figure 27-7 shows the contents of .login:

Figure 27-7. Sample .login
# @(#) $Revision: 72.3 $


# Default user .login file ( /usr/bin/csh initialization )

# Set up the default search paths:
set path=( $path )

#set up the terminal
eval `tset -s -Q -m ':?hp' `
stty erase "^H" kill "^U" intr "^C" eof "^D" susp "^Z" hupcl ixon ixoff
tostop
tabs

# Set up shell environment:
set noclobber
set history=20

The .cshrc File

The sequence of events after login varies from one UNIX system to another. On many systems, the .cshrc is first read and executed by the C shell. You can modify the .cshrc file to specify the command-line prompt you wish to use, initialize the history list, and define aliases. The upcoming sections describe the way the .cshrc file shown in Figure 27-6 defines these. Let's first take a quick look at the .login file in the next section.

The .login File

On many UNIX systems, the .login file is read after the .cshrc file. There are only two issues related to setup present in the example shown. The first is the tset command, which sets the TERM environment variable. The eval preceding tset means that the C shell executes tset and its arguments without creating a child process. This allows tset to set environment variables in the current shell instead of a subshell, which would be useless. The stty command is used to set terminal I/O options. The two set commands are used to define shell variables, which I describe shortly. The noclobber does not permit redirection to write over an existing file. If you try to write over an existing file, such as /tmp/processes below, you receive a message that the file exists:


sys1 1:  ps  -ef  > /tmp/processes
/tmp/processes:  File exists

The ">" means to take the output of ps and rather than write it to your screen, write it to /tmp/processes. The file /tmp/processes will not be written over, however, with the output of ps -ef because /tmp/processes already exists and an environment variable called noclobber has been set. If noclobber is set, then redirecting output to this file will not take place. This is a useful technique for preventing existing files from being accidently overwritten. There are many forms of redirection that you'll find useful. Redirection is covered later in this chapter.

Initialize History List in .cshrc

The C shell can keep a history list of the commands that you have issued. If you wish to reissue a command or view a command you earlier issued, you can use the history list.

The commands issued are referred to by number, so you want to have a number appear at the command prompt. The following line in .cshrc provides a number following the system name:

set prompt = "$system !: "

sys1 1:

We get into shell and environment variables shortly, but for now it is sufficient to know that $system corresponds to system name "sys1."

You can specify the number of previously issued commands that you want to save and view when you issue the history command. The following line in .cshrc sets the history list to 20:

set history = 20

The last 20 commands issued are displayed when you issue the history command.

The savehist variable allows you to save a specified number of history commands after logout. By default, when you log out, the history list is cleared. This variable has a value of 20, so that upon the next login there will be 20 commands from the previous session will be saved.

Command-Line History

You can view the history list a variety of different ways. Let's first view the last 20 commands by simply issuing the history command:

sys1 23: history
     4 whoami
     5 pwd
     6 find / -name login -print &
     7 hostname
     8 who
     9 more /etc/passwd
    10 history
    11 history 5
    12 echo $PATH
    13 more .login
    14 cat .login
    15 exit
    16 exit
    17 history -h
    18 pwd
    19 whoami
    20 cd /tmp
    21 cat database.log
    22 cd
    23 history
sys1 24:

We can also print the history list without line numbers, as shown in the following example:

sys1 24: history -h
pwd
find / -name login -print &
hostname
who
more /etc/passwd
history
history 5
echo $PATH
more .login
cat .login
exit
exit
history -h
pwd
whoami
cd /tmp
cat database.log
cd
history
history -h
sys1 25:

Next, let's print the history list in reverse order:

sys1 25: history -r
    25 history -r
    24 history -h
    23 history
    22 cd
    21 cat database.log
    20 cd /tmp
    19 whoami
    18 pwd
    17 history -h
    16 exit
    15 exit
    14 cat .login
    13 more .login
    12 echo $PATH
    11 history 5
    10 history
     9 more /etc/passwd
     8 who
     7 hostname
     6 find / -name login -print &
sys1 26:

We can also select the number of events we want to print from the history list. The following example prints only the last ten commands from the history list:

sys1 26: history 10
    17 history -h
    18 pwd
    19 whoami
    20 cd /tmp
    21 cat database.log
    22 cd
    23 history
    24 history -h
    25 history -r
    26 history 10
sys1 27:

You can see that you have a variety of ways to produce a list of commands previously issued. Table 27-5 summarizes the commands issued in this section:

Table 27-5. Command-Line History
CommandDescriptionExample
historyThe history list is produced with each command numbered.history
history -hThe history list is produced without line numbers.history -h
history -rThe history list is produced in reverse order with each command numbered.history -r
history nThe last n commands from the history list are produced with each command numbered.history 10

Re-Executing Commands from the History List

You can re-execute commands from the history list using a variety of techniques. We'll re-execute commands from the history list using the most common techniques.

You can repeat the last command with !!, the second command with !2, and the last command that started with "c" with !c. Let's issue the history command to get a list of the last 20 commands and then re-execute some of them:

sys1 27: history
     8 who
     9 more /etc/passwd
    10 history
    11 history 5
    12 echo $PATH
    13 more .login
    14 cat .login
    15 exit
    16 exit
    17 history -h
    18 pwd
    19 whoami
    20 cd /tmp
    21 cat database.log
    22 cd
    23 history
    24 history -h
    25 history -r
    26 history 10
    27 history
sys1 28:

Let's first reissue the last command with !!:

sys1 28: !!
history
     9 more /etc/passwd
    10 history
    11 history 5
    12 echo $PATH
    13 more .login
    14 cat .login
    15 exit
    16 exit
    17 history -h
    18 pwd
    19 whoami
    20 cd /tmp
    21 cat database.log
    22 cd
    23 history
    24 history -h
    25 history -r
    26 history 10
    27 history
    28 history
sys1 29:

Let's now reissue the 19th command with !19:

sys1 29: !19
whoami
martyp2
sys1 30:

Let's now reissue the last command beginning with "p" with !p:

sys1 30: !p
pwd
/home/martyp2
sys1 31:

Table 27-6 includes some of the more commonly used history list recall commands:

Table 27-6. Recalling from the History List
CommandDescriptionExample
!NIssue command N!2
!!Issue last command!!
!-NIssue Nth command from last command issued!-N
!strIssue last command starting with str!c
!?str?Issue last command that had str anyplace in command line!?cat?
!{str}str2Append str2 to last command with str1!{cd} /tmp
^str1^str2^Substitute str2 for str1 in last command^cat^more^

Aliases in .cshrc

An alias is a name that you select for a frequently used command or series of commands. Many aliases are predefined for you.

You can use the .cshrc file as a place where your aliases are stored and read every time you log in. You can also define aliases at the command-line prompt, but these are cleared when you log out.

The alias command, without any arguments, lists all aliases. This list includes both preset aliases as well as those you have set. The following alias command shows all preset aliases on the system on which I am working:

sys1 7: alias
d       dirs
m       more
pd      pushd
pd2     (pushd +2)
po      popd
status (date; bdf)
sys1 8:

You are not restricted to using only the preset aliases. To create your own alias, you first issue the alias command, the name of the alias, and then the command or commands that are executed when the alias is executed.

Let's now create a few simple aliases. The first creates an alias of "h" for the history command:

sys1 1: alias h history
sys1 2: h
        history

Every time you type h, the history command is executed.

The following example creates an alias of a command that contains spaces, so the command is surrounded by single quotes:

alias ls='ls -al'
alias
ls

The first command creates an alias for "ls" that executes the ls -al command. We then issued the alias command to see whether indeed the new alias appears in the list of aliases. Then we run ls to see whether ls -al is run.

You don't have to keep an alias for the duration of your session after having set it. If you don't like an alias, you can use the unalias command to remove an alias.

To see unalias work, let's again produce a list of aliases, use unalias to unset the h alias, and run the h command to see if, indeed, it has been removed:

unalias h
h

When we issued unalias to remove the h alias and then try to run h, we were told that h is not found.

sys1 3:alias procs 'echo "Number of processes are: c";
							ps -ef | wc -l'
                     # single quote on outside
                     # double quote on inside

When you run procs, you see the following:

sys1 4: procs
Number of processes are: 44

A lot of quoting takes place in this command line. To understand what is taking place on this line, consult Table 27-7 for help.

Table 27-7. Shell Quoting
Character(s)Description
'cmd'Single quote means to take the string character literally
"str"Double quote means to allow command and variable substitution
cEscape character that prevents everything following it from printing, including new line
'str'Grave means to execute command and substitute output

Applying Table 27-7 to the earlier procs alias, we can see what comprises this alias. The alias begins with a single quote, which means to execute the command(s) within the single quotes. The first command is the echo command, which uses double quotes to specify the characters to echo. Embedded in the double quotes is the escape character c, which prevents a new line from being printed. The semicolons separate commands. ps is then run to produce a list of processes, and the output is piped (|) to word count (wc) which produces a count of the number of lines. There are actually 43 processes running, because an extra line consisting of the ps headings is reported by wc.


As you can see in Figure 27-8, some of the quoting becomes tricky. An understanding of quoting is important if you wish to modify and reuse existing shell scripts or craft your own.

Figure 27-8. Quoting Example


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.56.45