Y is
$ echo Y is ${Y-default}
Y is default
$ Y=new
$ echo Y is ${Y-default}
Y is new
$
Think of the hyphen as a mnemonic for an optional value, as the hyphen is used to specify an option on a
UNIX command line. Like the other example, the word can be more than a single word. Here are some
examples:
${b-string}
${b-$variable}
${b-"a phrase with spaces"}
${b-"A complex phrase with variables like $HOME or `date`"}
${b-`command`}
${b-`wc -l
${b-`ypcat passwd | wc -l`}
Any command in this phrase is only executed if necessary. The last two examples counts the number of lines
in the password file, which might indicate the maximum number of users. Remember - you can use these
forms of variables in place of the simple variable reference. So instead of the command
echo Maximum number of users are $MAXUSERS
change it to
echo Maximum number of users are ${MAXUSERS-`wc -l
If the variable is set, then the password file is never checked.
${variable+value} - Change if defined
The third variation uses a plus sign, instead of a minus. The mnemonic is "plus is the opposite of the minus."
This is appropriate, as the command does act the opposite as the previous one. In other words, if the variable
is set, then ignore the current value, and use the new value. This can be used as a debug aid in Bourne shell
scripts. Suppose you wanted to know when a variable was set, and what the current value is. A simple way to
do this is to use the echo command, and echo nothing when the variable has no value by using:
echo ${A+"Current value of A is $A"}
This command does print a blank line if A does not have a value. To eliminate this, use either the Berkeley
version of echo, or the System V version of echo:
/usr/bin/echo ${A+"A = $A"}"c"
/usr/ucb/echo -n ${A+"A = $A"}
${variable=value} - Redefine if undefined
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
21 of 66
11/21/2011 12:03 PM
Don't forget that these variations are used when you reference a variable, and do not change the value of the
variable. Well, the fourth variation is different, in that it does change the value of the variable, if the variable
is undefined. It acts like the hyphen, but if used, redefines the variable. The mnemonic for this action? The
equals sign. This should be easy to remember, because the equals sign is used to assign values to variables:
$ echo Y is $Y
Y is
$ echo Y is ${Y=default}
Y is default
$ echo Y is $Y
Y is default
$
Undefining Variables
As you use these features, you may wish to test the behavior. But how do you undefine a variable that is
defined? If you try to set it to an empty string:
A=
you will discover that the above tests do not help. As far as they are concerned, the variable is defined. It just
has the value of nothing, or null as the manual calls it. To undefine a variable, use the unset command:
unset A
or if you wish to unset several variables
unset A B C D E F G
${x:-y}, ${x:=y}, ${x:?y}, ${x:+y} forms
As you can see, there is a different between a variable that has a null value, and a variable that is undefined.
While it might seem that all one cares about is defined or undefined, life is rarely so simple. Consider the
following:
A=$B
If B is undefined, is A also undefined? No. Remember, the shell evaluates the variables, and then operates on
the results. So the above is the same as
A=
which defines the variable, but gives it an empty, or null value. I think most scripts don't care to know the
difference between undefined and null variables. They just care if the variables have a real value or not. This
makes so much sense, that later versions of the Bourne shell made it easy to test for both cases by creating a
slight variation of the four forms previously described: a colon is added after the variable name:
+----------------------------------------------------------------------+
|Form
Meaning
|
+----------------------------------------------------------------------+
|${variable:?word} Complain if undefined or null
|
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
22 of 66
11/21/2011 12:03 PM
|${variable:-word} Use new value if undefined or null
|
|${variable:+word} Opposite of the above
|
|${variable:=word} Use new value if undefined or null, and redefine. |
+----------------------------------------------------------------------+
Notice the difference between "${b-2}" and "${b:-2}" in the following example:
$ # a is undefined
$ b=""
$ c="Z"
$ echo a=${a-1}, b=${b-2}, c=${c-3}
a=1, b=, c=Z
$ echo a=${a:-1}, b=${b:-2}, c=${c:-3}
a=1, b=2, c=Z
Order of evaluation
One last point - the special word in one of these formats is only evaluated if necessary. Therefore the cd and
pwd commands in the following: is only executed if the word is executed:
echo ${x-`cd $HOME;pwd`}
Also - the evaluation occurs in the current shell, and not a sub-shell. The command above will change the
current directory, but the one below will not, as it executes the commands in a new shell, which then exits.
echo `cd $HOME;pwd`
Special Variables in the Bourne Shell
Earlier, I discussed Bourne shell variables, and various ways to use them. So far I have only given you the
foundation of shell programming. It's time for discussing special Bourne shell variables, which will allow you
to write useful scripts. These special variables are identified by the dollar sign, and another character. If the
character is a number, it's a positional parameter. If it's not a letter or number, it's a special purpose variable.
Positional Parameters $1, $2, ..., $9
The most important concept in shell scripts is passing arguments to a script. A script with no options is more
limited. The Bourne shell syntax for this is simple, and similar to other shells, and awk. As always, the dollar
sign indicates a variable. The number after the dollar sign indicates the position on the command line. That is,
"$1" indicates the first parameter, and "$2" indicates the second. Suppose you wanted to create a script called
rename that takes two arguments. Just create a file with that name, that contains the following:
#!/bin/sh
# rename: - rename a file
# Usage: rename oldname newname
mv $1 $2
Click here to get file:
rename0.sh
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
23 of 66
11/21/2011 12:03 PM
Dostları ilə paylaş: |