baiseda 发表于 2013-1-15 19:00:58

UNIX Shell 编程(6)

UNIX Shell 编程(6)
变量
UNIX Shell允许把值存在变量中。
要把值存入一个变量,只需写出变量名,后面紧跟一个等号,再紧跟变量值。
variable=value
切忌中间含空格
Shell中没有任何数据类型的概念,变量值通通认为是字符串。
如:
# count=1
# echo $count
1
再如:
# my_bin=/tools/jdk6/bin
# echo $my_bin
/tools/jdk6/bin
# ls $my_bin
appletviewerHtmlConverterjava   javap         jcontroljmap      jstack   keytool       policytool   schemagen   unpack200
apt         idlj         javac    java-rmi.cgijdb       jps         jstat      native2asciirmic         serialver   wsgen
ControlPaneljar            javadocjavaws      jhat      jrunscriptjstatd   orbd          rmid         servertoolwsimport
extcheck      jarsigner      javah    jconsole      jinfo   jsadebugd   jvisualvmpack200       rmiregistrytnameserv   xjc
例3:
# command=wc
# file=names
# option=-l
# $command $option $file
6 names

变量还能传值,如下:
# value1=10
# value2=$value1
# echo $value2
10

POSIX标准Shell为变量整数运算提供了一套机制,称为算术扩展。
格式为:$((expression))
其中,expression是由变量和运算符构成的算是表达式。
如:
# echo $a

# echo $((a=a+1))
1
# echo $a
1
再如:
# result=$((i>=0&&i<100))
如果表达式结果为真,result赋值1;否则赋值0.
# echo $result
1
例3:
# i=$((100*200/10))
# j=$((i<1000))
# echo $i $j
2000 0


单引号和双引号
单引号告诉Shell忽略所包含的所有特殊字符。
如:
# file=/tools/jdk
# echo $file
/tools/jdk
# echo '$file'
$file
# echo *
addresses collect data data2 intro lotsapaces mon names nu numbers phonebook sorted_name1 sorted_name2 stats tally temp wbb2 wbx writeback
# echo '*'
*
而双引号只要求忽略大多数。
在双引号中,有三种特殊字符不会被忽略:
1)美元符号$
2)反引号`
3)反斜杠\
如:
# x=*
# echo '$x'
$x
# echo "$x"
*
可见,在双引号内部也进行了变量名替换。
反斜杠的例子:
# echo \>
>
# x=*
# echo \$x
$x
可见,反斜杠进行了转义。

反斜杠还可用于换行:
# lines=one\
> two
# echo $lines
onetwo


命令替换:指Shell能够将一个命令的标准输出插入一个命令行中任何位置的功能。
Shell有两种方法做命令替换:
1)用反引号
2)使用$(...)结构括起来

反引号:其目的不是防止Shell处理某些字符,二是要告诉Shell执行括起来的命令,并将该命令的标准输出插在命令行中的这个位置。例如:
# echo The date and time is: `date`
The date and time is: Thu Apr 16 16:11:40 CST 2009
# echo Your current working directory is `pwd`
Your current working directory is /tools/test/programs

POSIX标准Shell支持新的$(...)命令替换结构,格式为:
$(command)
例如:
# echo The date and time is: $(date)
The date and time is: Thu Apr 16 16:13:34 CST 2009
# echo Your current working directory is $(pwd)
Your current working directory is /tools/test/programs
此结构比使用反引号要好,原因如下:
1)复杂的命令中组合使用正、反引号会很难看懂,而且在某些字体显示下难以区分;
2)$(...)结构容易嵌套。

例如:
# cat nu
echo There are $(who | wc -l) users logged in
# ./nu
There are 1 users logged in

# now=$(date)
# echo $now
Thu Apr 16 16:32:10 CST 2009

# filelist=$(ls)
# echo $filelist
addresses collect data data2 intro lotsapaces mon names nu numbers phonebook sorted_name1 sorted_name2 stats tally temp wbb2 wbx writeback

命令替换可嵌套,如下:
# filename=/tools/test/programs
# ls $filename
addressesdata   intro       mon    nu       phonebook   sorted_name2tallywbb2writeback
collect    data2lotsapacesnamesnumberssorted_name1stats         temp   wbx
# firstchar=$(echo $filename | cut -c1)
# echo $firstchar
/
# echo $filename
/tools/test/programs
# echo $filename | cut -c1
/
# filename=$(echo $filename | tr "$firstchar" "^")
# echo $filename
^tools^test^programs
页: [1]
查看完整版本: UNIX Shell 编程(6)