next up previous contents
Next: Redirection and Pipelines Up: The C Shell Previous: Getting Started with the

Command Execution

The C shell allows commands to be entered in different ways. Grouping multiple commands into a single command line, running commands in the background, and building complicated commands from commands inside of commands can all be handled by the C shell.

To execute multiple commands on a single command line, simply place a semicolon between each of the commands. The commands will be executed from left to right with each successive command being executed after the previous one. For example, the following command line changes to a directory containing documents which need to be moved. A new directory is created for the documents, which are then all moved into it.

% cd docs; mkdir old_docs; mv *.* old_docs

This task could have been accomplished by executing each of the commands separately on a separate line:

% cd docs
% mkdir old_docs
% mv *.* old_docs

If all of these commands execute quickly, this is a good method for carrying out tasks. If however, one or more of the tasks, takes a significant amount of time to execute, the user will be left waiting for its completion - wasting time. To get around this problem, time consuming commands can be run in the background. This means that the shell handles the execution and waits for its completion without the user having to wait to do other things. For example, if the user needs to carry out a few routine disk management tasks, including archiving all of his directories and subdirectories, he might wish to have the archiving done in the background.

% rm ~/tmp/*; tar cfv my_dirs.tar ~

Sometimes a situation might arise where the user would like to have commands executed such that one command is the argument (or input) of another command. For example, the user may wish to count the number of users logged into the system at the present time. One way to do this would be to type who and count the number of users, but a better way would be the following:

% wc -l `who`

In this example, the left single quotes tell the shell to execute what is inside first (the who command), and use the result as the input to the wc command. The user would only see a number as the result of this command. While the who command generates a list of users currently logged onto the system, this output is sent to the wc command and therefore only the number of lines that the wc command counts is output to the screen.

A group of commands can also be executed in a subshell. This means that a group of commands can be executed in a shell other than the interactive shell currently being used. To do this, the user would enter a list of commands separated by semicolons inside of parantheses (). The following example will illustrate the syntax of this type of command while also demonstrating the way in which variables are affected by use in subshells.

% set MY_name = 'N Buchanan'
% (set MY_name = 'Joe Blow'; echo $MY_name)
Joe Blow
% echo $MY_name
N Buchanan

It can be seen that the same variable is used in both the current interactive shell and in the subshell. The values of this variable are different however. The good news is that the subshell did not overwrite (or redefine) the variable in the current shell. The bad news is however that this type of variable usage can become quite confusing and cause the user to lose track of what a variable's purpose is. This type of variable usage is therefore discouraged.

The C shell provides a mechanism for repeating a command a specified of times. While on the surface this may seem like a useless feature, it can come in handy. The format of this repeat command is simply

% repeat n command

where n is the specified number of repetitions and command is the command to be repeated. A particularly useful way to use this feature is to repeatedly echo something to the screen:

repeat 2 echo ********************
echo '        Error'
repeat 2 echo ********************

which would then be executed as follows:

 ******************** 
 ********************
       Error
 ********************
 ********************

which would certainly add a sense of urgency to any error message. The alert reader might be saying that the output would be interrupted by the entry of each successive line. While this would be true in interactive mode, the omission of a prompt will be used in this book to signify a shell script or shell script fragment (which the above example is). In a shell script, the commands are executed in sequential order. This will be covered in more detail in a later section. One thing to keep in mind when using the repeat command is that the command to be repeated must be a simple command and not a compound command using redirection or pipelines (which will be covered in the next section).


next up previous contents
Next: Redirection and Pipelines Up: The C Shell Previous: Getting Started with the

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998