Previous Section  < Day Day Up >  Next Section

9.3. The C/TC Shell Command Line

After logging in, the C/TC shell displays its primary prompt, a % or >, respectively. The shell is your command interpreter. When the shell is running interactively, it reads commands from the terminal and breaks the command line into words. A command line consists of one or more words (or tokens) separated by whitespace (blanks and/or tabs) and terminated with a newline, generated by pressing the Enter key. The first word is the command, and subsequent words are the command's options and/or arguments. The command may be a UNIX/Linux executable program such as ls or pwd, an alias, a built-in command such as cd or jobs, or a shell script. The command may contain special characters, called metacharacters, that the shell must interpret while parsing the command line. If the last character in the command line is a backslash, followed by a newline, the line can be continued to the next line.[3]

[3] The length of the command line can be 256 characters or more; it can be even higher on different versions of UNIX.

9.3.1 The Exit Status

After a command or program terminates, it returns an exit status to the parent process. The exit status is a number between 0 and 255. By convention, when a program exits, if the status returned is 0, the command was successful in its execution. When the exit status is nonzero, the command failed in some way. The C/TC shell status variable is set to the value of the exit status of the last command that was executed. Success or failure of a program is determined by the programmer who wrote the program.

Example 9.5.

1   % grep "ellie" /etc/passwd

    ellie:GgMyBsSJavd16s:9496:40:Ellie Quigley:/home/jody/ellie

2   % echo $status

    0

3   % grep "nicky" /etc/passwd

4   % echo $status

    1

5   % grep "scott" /etc/passsswd

    grep: /etc/passsswd: No such file or directory

6   % echo $status

    2


EXPLANATION

  1. The grep program searches for the pattern ellie in the /etc/passwd file and is successful. The line from /etc/passwd is displayed.

  2. The status variable is set to the exit value of the grep command; 0 indicates success.

  3. The grep program cannot find user nicky in the /etc/passwd file.

  4. The grep program cannot find the pattern, so it returns an exit status of 1.

  5. The grep program fails because the file /etc/passsswd cannot be opened.

  6. Grep cannot find the file, so it returns an exit status of 2.

9.3.2 Command Grouping

A command line can consist of multiple commands. Each command is separated by a semicolon and the command line is terminated with a newline.

Example 9.6.

% ls; pwd; cal 2004


EXPLANATION

The commands are executed from left to right until the newline is reached.

Commands may also be grouped so that all of the output is either piped to another command or redirected to a file. The shell executes commands in a subshell.

Example 9.7.

1   % ( ls ; pwd; cal 2004 ) > outputfile

2   % pwd; ( cd / ; pwd ) ; pwd

    /home/jody/ellie

    /

    /home/jody/ellie


EXPLANATION

  1. The output of each of the commands is sent to the file called outputfile. Without the parentheses, the output of the first two commands would go to the screen, and only the output of the cal command would be redirected to the output file.

  2. The pwd command displays the present working directory. The parentheses cause the commands enclosed within them to be processed by a subshell. The cd command is built into the shell. While in the subshell, the directory is changed to root and the present working directory is displayed. When out of the subshell, the present working directory of the original shell is displayed.

9.3.3 Conditional Execution of Commands

With conditional execution, two command strings are separated by two special metacharacters, double ampersand and double vertical (&& and ||). The command on the right of either of these metacharacters will or will not be executed based on the exit condition of the command on the left.

Example 9.8.

% grep '^tom:' /etc/passwd && mail tom < letter


EXPLANATION

If the first command is successful (has a 0 exit status), the second command, after the &&, is executed. If the grep command successfully finds tom in the passwd file, the command on the right will be executed: The mail program will send tom the contents of the letter file.

Example 9.9.

% grep '^tom:' /etc/passwd || echo "tom is not a user here."


EXPLANATION

If the first command fails (has a nonzero exit status), the second command, after the ||, is executed. If the grep command does not find tom in the passwd file, the command on the right will be executed: The echo program will print tom is not a user here. to the screen.

9.3.4 Commands in the Background

Normally, when you execute a command, it runs in the foreground, and the prompt does not reappear until the command has completed execution. It is not always convenient to wait for the command to complete. By placing an ampersand at the end of the command line, the shell will return the shell prompt immediately so that you do not have to wait for the last command to complete before starting another one. The command running in the background is called a background job and its output will be sent to the screen as it processes. It can be confusing if two commands are sending output to the screen concurrently. To avoid confusion, you can send the output of the job running in the background to a file, or pipe it to another device such as a printer. It is often handy to start a new shell window in the background, so you will have access to both the window from which you started and the new shell window.

Example 9.10.

1   % man xview | lp&

2   [1] 1557

3   %


EXPLANATION

  1. The output from the man pages for the xview program is piped to the printer. The ampersand at the end of the command line puts the job in the background.

  2. There are two numbers that appear on the screen: The number in square brackets indicates that this is the first job to be placed in the background, and the second number is the PID of this job.

  3. The shell prompt appears immediately. While your program is running in the background, the shell is prompting you for another command in the foreground.

9.3.5 Command-Line History

The history mechanism is built into the C/TC shell. (For tcsh enhancements to history see "TC Shell Command-Line History" on page 466.) It keeps a numbered list of the commands (called history events) that you have typed at the command line. You can recall a command from the history list and re-execute it without retyping the command. The history substitution character, the exclamation point, is often called the bang character. The history built-in command displays the history list.

Example 9.11.

(The Command Line)

% history

1 cd

2 ls

3 more /etc/fstab

4 /etc/mount

5 sort index

6 vi index


EXPLANATION

The history list displays the last commands that were typed at the command line. Each event in the list is preceded with a number.

Setting History

The C shell history variable is set to the number of events you want to save from the history list and display on the screen. Normally, this is set in the .cshrc file, the user's initialization file.

Example 9.12.

set history=50


EXPLANATION

The last 50 commands typed at the terminal are saved and may be displayed on the screen by typing the history command.

Saving History

To save history events across logins, set the savehist variable. This variable is normally set in the .cshrc file, the user's initialization file.

Example 9.13.

set savehist=25


EXPLANATION

The last 25 commands from the history list are saved and will be at the top of the history list the next time you log in.

Displaying History

The history command displays the events in the history list. The history command also has options that control the number of events and the format of the events that will be displayed. The numbering of events does not necessarily start at 1. If you have 100 commands on the history list, and you have set the history variable to 25, you will only see the last 25 commands saved. (The TC shell supports the use of the arrow keys. See "Accessing Commands from the History File" on page 470.)

Example 9.14.

% history

1 ls

2 vi file1

3 df

4 ps –eaf

5 history

6 more /etc/passwd

7 cd

8 echo $USER

9 set


EXPLANATION

The history list is displayed. Each command is numbered.

Example 9.15.

% history –h          # Print without line numbers

ls

vi file1

df

ps –eaf

history

more /etc/passwd

cd

echo $USER

set

history –n


EXPLANATION

The history list is displayed without line numbers.

Example 9.16.

% history –r          # Print the history list in reverse

11 history –r

10 history –h

 9 set

 8 echo $USER

 7 cd

 6 more /etc/passwd

 5 history

 4 ps –eaf

 3 df

 2 vi file1

 1 ls


EXPLANATION

The history list is displayed in reverse order.

Example 9.17.

% history 5        # Prints the last 5 events on the history list

7    echo $USER

8    cd

9    set

10   history –n

11   history 5


EXPLANATION

The last five commands on the history list are displayed.

Re-executing Commands

To re-execute a command from the history list, the exclamation point (bang) is used. If you type two exclamation points (!!), the last command is re-executed. If you type the exclamation point followed by a number, the number is associated with the command from the history list and the command is executed. If you type an exclamation point and a letter, the last command that started with that letter is executed. The caret (^) is also used as a shortcut method for editing the previous command.

Example 9.18.

1   % date

    Mon Apr 26  8 12:27:35 PST 2004

2   % !!

    date

    Mon Apr 26 12:28:25 PST 2004

3   % !3

    date

    Mon Apr 26 12:29:26 PST 2004

4   % !d

    date

    Mon Apr 26 12:30:09 PST 2004

5   % dare

    dare: Command not found.

6   % ^r^t

    date

    Mon Apr 26 12:33:25 PST 2004


EXPLANATION

  1. The date command is executed at the command line. The history list is updated. This is the last command on the list.

  2. The !! (bang bang) gets the last command from the history list; the command is re-executed.

  3. The third command on the history list is re-executed.

  4. The last command on the history list that started with the letter d is re-executed.

  5. The command is mistyped.

  6. The carets are used to substitute letters from the last command on the history list. The first occurrence of an r is replaced with a t.

Example 9.19.

1   % cat file1 file2 file3

        <Contents of file1, file2, and file3 are displayed here>

    % vi !:1

    vi file1



2   % cat file1 file2 file3

        <Contents of file1, file2, and file3 are displayed here>



    % ls !:2

    ls file2

    file2



3   % cat file1 file2 file3

    % ls  !:3

    ls file3

    file3



4    % echo a b c

    a b c

    % echo !$

    echo c

    c



5   % echo a b c

    a b c    % echo !^

    echo a

    a



6   % echo a b c

    a b c

    % echo !*

    echo a b c

    a b c



7    % !!:p

    echo a b c


EXPLANATION

  1. The cat command displays the contents of file1, file2, and file3 to the screen. The history list is updated. The command line is broken into words, starting with word number 0. If the word number is preceded by a colon, that word can be extracted from the history list. The !:1 notation means: get the first argument from the last command on the history list and replace it in the command string. The first argument from the last command is file1. (Word 0 is the command itself.)

  2. The !:2 is replaced with the second argument of the last command, file2, and given as an argument to ls. file2 is printed. (file2 is the third word.)

  3. ls !:3 reads: go to the last command on the history list and get the fourth word (words start at 0) and pass it to the ls command as an argument (file3 is the fourth word).

  4. The bang (!) with the dollar sign ($) refers to the last argument of the last command on the history list. The last argument is c.

  5. The caret (^) represents the first argument after the command. The bang (!) with the ^ refers to the first argument of the last command on the history list. The first argument of the last command is a.

  6. The asterisk (*) represents all arguments after the command. The bang (!) with the * refers to all of the arguments of the last command on the history list.

  7. The last command from the history list is printed but not executed. The history list is updated. You could now perform caret substitutions on that line.

    Previous Section  < Day Day Up >  Next Section