Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


How do I determine whether the shell can find a particular command?

You can check to make sure that the shell can find a command or shell function by using the type command covered in Chapter 18, “Miscellaneous Tools”:

type name > /dev/null 2>&1 ; if [ $? -ne 0 ] ; then list ; fi

Here name is the name of the command you want check for, and list is the list of commands to execute if the shell does not know about name. Usually list is used to determine a fallback command.

The type command is a builtin in sh and bash. In ksh, it is usually an alias, whence -v.

How do I determine whether job control is available in the shell?

Job control, covered in Chapter 6, “Processes,” is the shell feature that enables you to control background processes based on a job ID. This feature is not available in the Bourne shell, sh. It is available in ksh and bash.

A common method used to check whether job control is enabled is to check whether the jobs command is defined:

if type jobs > /dev/null 2>&1 ; then
    echo "We have job control"
fi

This check is effective in most cases because the jobs command is not available in most versions of the Bourne shell.

Unfortunately, some versions of UNIX such as Sun Solaris, include a version of the Bourne shell that has a built-in command called jobs. On these systems when the shell is invoked as /bin/sh, the jobs command exists but does nothing. If the shell is invoked as /bin/jsh (as in job control shell), the jobs command behaves normally.

Variable and Argument Questions

In this section I will examine some questions related to variables and their use in shell scripts. I will also cover some questions related to command line arguments.

How can I include functions and variable definitions from one file into another file?

To include functions and variable definitions defined in one file into another file you need to use the . command as follows:

. file

Here file is the name of the file you want to include. I covered this topic in Chapter 22, “Problem Solving with Shell Scripts.”

Is it possible to consider each argument to a shell script one at a time?

You can do this using a for loop:

for arg in "$@"
do
    list
done

Here the variable arg will be set to each argument in turn. The specified list of commands, list, will be executed for each argument.

You use $@ in this example for the arguments instead of $*, because $@ preserves the quoting used when the command was issued. The difference between $@ and $* was discussed in Chapter 12, “Parameters.”

How can I forward all the arguments given to my script to another command?

A common task for shell programmers is writing a wrapper script for command. A wrapper script might need to define a set of variables or change the environment in some way before a particular command starts executing.

When writing wrapper scripts, you will need to forward all the arguments given to your script to a command. Usually the following is sufficient:

command "$@"

Here command is the name of the command you want to execute.

The one problem with this is that if no arguments were specified to your script, some versions of the shell will expand “$@” to “”. If no arguments were specified, you want to execute command, not command “”.To avoid this problem, use the form:

command ${@:+"$@"}

Here you are using one of the forms of variable substitution discussed in Chapter 8, “Substitution.” In this case you check to see whether the variable $@ has a value. If it does, you substitute the value “$@” for it. If your script was not given any command line arguments, $@ will be null; thus no value will be substituted.

How do I use the value of a shell variable in a sed command?

The simplest method to use variables in a sed command is to enclose your sed command in double quotes () instead of single quotes (). Because the shell performs variable substitution on double-quoted strings, the shell will substitute the value of any variables you specify before sed executes.

For example, the command

sed "/$DEL/d" file1 > file2

deletes all the lines in file1 that contain the value stored in the variable $DEL.

How do I check to see whether a variable has a value?

There are several methods for determining this. The simplest is the if statement:

if [ -z "$VAR" ] ; then list ; fi

Here VAR is the name of the variable, and list is the command to execute if VAR does not contain a value. Usually list initializes VAR to some default value.

You can initialize variables more succinctly using variable substitution. For example, the previous if statement can be written as

: ${VAR:=default}

Here default is the default that should be assigned to VAR, if VAR does not have a value.

If you need execute a set of commands to obtain a default value, use command substitution with the backquote (`) operator to obtain the value that should be substituted:

: ${VAR:=`default`}

Here default is a list of commands to execute. If VAR does not have a value, the output of these commands will be assigned to it.


Previous Table of Contents Next