- Shell and Command Questions
- Variable and Argument Questions
- File and Directory Questions
- Summary
Variable and Argument Questions
This section examines some questions that relate to variables and their use in shell scripts. It also covers questions related to command-line arguments.
How can I include functions and variable definitions located in one script in to another script?
To include functions and variable definitions defined in one script in to another script, you need to use the . command as follows:
. file
Here file is the pathname of the script you want to include. This topic is covered in detail in Chapter 21, "Problem Solving with Functions."
CAUTION
When you include a file using the . command, make sure that the file does not contain the exit command as this will cause the current instance of the shell to exit.
If you are using the . command to include a file in your login session, your session will be terminated and you will have to log in again.
Is it possible to consider each argument to a shell script one at a time?
This can be accomplished using a for loop of the following form:
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. The following function illustrates the use of this for loop:
echoargs () { for arg in "$@" do echo $arg done return 0 }
How can I forward all the arguments given to my script to another command?
A common task of shell programmers is writing a wrapper script for a 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 need to forward all the arguments given to your script to a command. Usually the following is sufficient:
cmd "$@"
Here cmd 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 cmd, not cmd "". To avoid this problem, you can use the following:
command ${@:+"$@"}
Here you are using one of the forms of variable substitution discussed in Chapter 9, "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 can I store the output of a command in a variable?
You can store the output of a command in a variable by combining the assignment operator, =, and the backquotes, '':
var=´cmd´
Here var is the name of a variable and cmd is the command whose output you want to store. For example, the following command stores the current date in the variable THEDATE:
THEDATE=´date´
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 list of commands to execute if VAR does not have a value. Usually list initializes VAR to some default value. For example, the following command initializes the variable THDATE if it does not have a value:
if [ -z "$THEDATE" ] ; then THEDATE=´date´ fi
If you are just interested in variable initialization, this can be accomplished in a much more succinct fashion 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 to execute a set of commands to obtain a default value, the backquotes (´´) can be used to obtain the value to 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. The following command also initializes the variable THEDATE:
: ${THEDATE:=´date´}