?f 引用变量的值时,可以用花括号{}将变量名称括起来,使变量名称与它的后续字符分隔开,如果紧跟在变量名称后面的字符是字母.数字或下划线时,必须要使用花括号.?
?? 例: # str=´this is a string´??# echo "${str}ent test of variables"??this is a stringent test of variables??# echo "$strent test of variables"??test of variables6.2.2输入输出命令1.read命令??????? 可以利用read命令由标准输入读取数据,然后赋给指定的变量格式:read? 变量1 [变量2…]例如:read name??????????? read a b c?f 若变量数少于数据个数
例如执行 read n1 n2时,用户输入:first second third 则 n1取值1, n2取值2,n3为空串.2.echo命令
??????? echo命令将其后的参数在标准输出上输出.?echo参数中可以有一些特殊字符? p139例:echo “enter the file name->\c”2.3? 位置参数?????? 位置变量顾名思义是与位置有关的变量,这是一种特殊的变量.命令行的shell过程名本身被指定为位置变量$0,依次参数为$1 …. $9 .?例:ls???? /?? /bin? /etc? /usr/bin? /dev?? …???????????? ×???? ×?????? ×?????? ×???????? ×???????????? ×?? $0?? $1?? $2??? $3???? $4???????? $5??? …???? 例:# cat? finduser?????????????? who | grep $1??????????? # finduser fke例6-5 位置参数的使用
$ cat m1.c? main( )?{?? printf(“bebin\n”)?}$cat m2.c? #include? main( )? {? printf(“ok\n”)? printf(“end\n”)? }$cat ex5
#ex5:shell sript to combine files and count #lines????? cat $1 $2 $3 $4 $5 | wc –l#end$ ex5 m1.c m2.c?10f在shell程序中可以利用set命令为位置参数赋值.例如:
??? set m1.c m2.c m3.c就把字符串m1.c赋给$1,字符串m2.c赋给$2,字符串m3.c赋给$3.但$0不能用set赋值例6-6? 用set 设置位置参数值?$ cat ex6?? set m1.c m2.c?? cat $1 $2 $3 $4 $5 | wc-l$ ex6106.2.5? 预定义的特殊变量?? 在shell中有一组特殊的变量,其变量名与变量值只有shell本身才可以设置.?f? $#? — 记录传递给shell的自变量个数;? 例1: # myprog? a? b? c????????? 则? $#的值为3? 例2: if? test $#? -lt? 2?????????? then???? echo? “two or more args required”???? exit ???????? fi?例6-2
$ cat ex2?if test $# = 0?? then lselse??? for i????? do????????? ls –l $i?????? donefi2.5? 预定义的特殊变量(续1)f $? —? 取最近一次命令执行后的退出状态(返???????? 回码) :执行成功返回码为0, 执行失???????? 败返回码为1;?例:# test? -r? my_file?? (假设my_file文件不可读)???? ?? # echo?? $? ??????????? 1f $$?? —? 当前进程的进程号 ;p142f $! —? 取最后一个在后台运行的(使用“&”)????????? 进程的进程号 ;?????????? ??2.5? 预定义的特殊变量(续2)?f? $-? —? shell的标志位,既在shell启动时使用 的选项,或用set命令方式所提供的选项.... 下一页