13、Linux Shell 笔记(6)
时间:2010-10-17 来源:edwardlewiswe
Variables
Each shell supports environment and user-defined variables, but they differ
on just how they support variables. This section describes the things you
need to watch out for when working with variables in your shell scripts.
Environment variables
The bash, dash, ksh93, and zsh shells all use all uppercase letters for
environment variables. The zsh shell provides both upper- and lower-case
environment variables for the common variables, so if you refer to uppercase
environment variables in your script, they’ll work just fine in the
zsh shell.
Unfortunately, the tcsh shell uses lower-case variables for special shell
variables. However, it does provide a limited number of upper-case environment
variables that match some of the basic Bourne shell environment
variables to provide limited compatibility. If your shell script relies heavily on bash shell environment variables, you’ll want to check to make sure that those variables are available in the tcsh shell.
One thing to watch out for is that the tcsh shell sets environment variables differently from the other shells. It uses the setenv command for setting environment variables:
setenv variable value This is different from the set command (described in the ‘‘User-defined variables’’ section next),
which the other shells use for environment variables.
User-defined variables
All shells allow users to define their own variables both on the command line and in shell scripts.
All of the shells except the tcsh shell allow you to set variables directly from the command prompt without a special command:
$ testing=100
In the tcsh shell you must use the set command:
$ set testing=100
You can also use the set command in other shells as well, but it’s not required. The tcsh shell also uses the at symbol for setting variables:
$ @ testing=100
In all shells, to make a variable accessible by child shells and processes, you must use the export command:
$ export testing
The typeset command is available in all shells to declare attributes for the variable before using it. In the ksh93 and zsh shells you must use the typeset command to define associative array variables:
$ typeset -A testing
The ksh93 and zsh shells also allow you to define floating-point variables using the typeset command.
Array variables
All of the shells support one-dimensional numeric arrays:
$ mytest=(one two three four five)
$ echo $mytest
one
$ echo ${mytest[2]}
three
$ echo ${mytest[*]}
one two three four five
$
In the bash, ksh93, and tcsh shells, numeric arrays start at 0 for the first element. The zsh shell starts arrays with an index value of 1 instead of 0.
The ksh93 and zsh shells also support associative arrays, which allow you to use a text value as the array index. In the ksh93 shell, you define an associative array like this:
$ typeset -A test2
$ test2=( [fruit]=banana [vegetable]=carrot [age]=18)
whereas in the zsh shell you do it like this:
% typeset -A test
% test=( fruit banana vegetable carrot age 18 )
Notice the subtle difference in the way that the data elements are defined.
Structured Commands
All of the shells support the basic structured commands for manipulating the flow of the shell script. Some of them use a slightly different syntax though. This section discusses the differences between the shells.
The if-then, while, and until statements
The bash, dash, ksh93, and zsh shells use the same format for if-then, while, and until statements. The format for the if-then statement is:
if command
then
commands
else
commands
fi
The same format applies to the while and until statements. Each statement executes a command, then checks the exit status of the command. The if-then statement executes the first set of commands if the exit status is zero, and the second set of commands if the exit status is non-zero.
The while and until statements loop through the specified commands until the exit status of the evaluation command is zero.
The tcsh shell uses a slightly different format for each of these statements:
if (expression) then
commands
else
commands
Endif
The while and until statements use a similar format, using the expression instead of a command as the evaluation. In both formats, the result of the expression determines which section the shell executes. If the expression exits with an exit status of 0, the shell executes the commands in the then section. If the expression exits with a non-zero exit status, the shell executes the commands in the else section.
The bash, dash, ksh93, and zsh shells use the test command (or its alias, []) to evaluate values and files:
if [ -d $HOME ]
then
echo "Your HOME directory exists"
else
echo "There’s a problem with your HOME directory"
fi
The tcsh shell uses this expression, which incorporates testing expressions similar to those used
by the test command:
if ( -d $HOME ) then
echo "The HOME directory exists"
endif
This subtle difference can cause all sorts of problems for shell scripts.
The for statement
The for statement is another problem area. The bash, dash, ksh93, and zsh shells all use the same format for the basic for statement:
for var in list
do
commands
done
The bash and zsh shells also support the C-style for statements:
for (( variable assignment ; condition ; iteration process ))
However, the tcsh shell doesn’t support the for statement. Instead, it uses the foreach statement:
foreach var1 (wordlist)
statements
end
This is a huge compatibility problem when migrating shell scripts within the tcsh environment.
All of the shells allow you to perform some level of mathematical operations within them. This is the area where the ksh93 and zsh shells shine.
The bash and dash shells use the expr command to evaluate a mathematical expression:
$ expr 1 + 5
6
They both also provide the $[] and $(()) shortcuts for defining mathematical operations:
$ var1=$[1 + 5]
$ echo $var1
6
The tcsh shell uses the at symbol for defining mathematical operations:
$ set test1 = 10
$ set test2 = 15
$ @ test3 = ( $test1 * $test2 )
$ echo $test3
150
$
The bash, dash, and tcsh shells can only perform integer mathematical operations.
Both the ksh93 and zsh shells use two methods for defining mathematical operations. The let command:
% let value1=" 4 * 5.1 / 3.2 "
% echo $value1
6.3749999999999991
and the $(()) shortcut:
% value1=$(( 4 * 5.1 ))
% (( value2 = 4 * 5.1 ))
% printf "%6.3f\n" $value1 $value2
20.400
20.400
%
Also, both the ksh93 and zsh shells fully support floating-point arithmetic in all mathematical operations.
The ksh93 shell includes many common mathematical functions built into the shell:
$ value1=$(( sqt(9) ))
$ echo $value1
3
The zsh shell doesn’t include mathematical functions built-in, but provides them as a separate
loadable module:
% zmodload zsh/mathfunc
% value1=$(( sqrt(9) ))
% echo $value1
3.
Obviously, if you’re interested in writing shell scripts with advanced mathematical functions,
you’ll want to choose the ksh93 or zsh shells over the others.
参考:
[1] Linux命令行和SHELL脚本编程