转载自:http://blog.sina.com.cn/s/blog_6151984a0100eki8.html
UNIX系统变量
$?
$#
$0,1,2,3
$*
$@
$$
$ ls 111.txt 111.txt $ echo $? 0 前一条命令成功,返回0 |
[macg@machome ~]$ ls sdsdf ls: sdsdf: No such file or directory [macg@machome ~]$ echo $? 1 前一条命令失败(出错),返回1 |
[mac@machome ~]$ vi test.sh ls -l $1 if [ $? != 0 ] ;then echo command fail else echo command success fi |
[mac@machome ~]$ sh test.sh 111.txt -rw-rw-r-- command success [mac@machome ~]$ sh test.sh 222.txt ls: 222.txt: No such file or directory command fail |
while [ -n "$1" ]; do 为什么循环总是$1(第一个参数)? case $1 in -h) h=$1 echo "your choice is $h" -f) f=$1 echo "your choice is $f" -c) c=$1 echo "your choice is $c" -z) z=$1 echo "your choice is $z" *) echo " $1 is wrong paratism" 不符合条件的参数,不再循环(break)(等于以后的参数不再检查) esac done |
[macg@machome ~]$ sh test.sh -h -z -c -f your choice is -h your choice is -z your choice is -c your choice is -f [macg@machome ~]$ sh test.sh -6 -h -6 is wrong paratism [macg@machome ~]$ sh test.sh -h -z -6 your choice is -h your choice is -z |
case start) stop) restart) status) |
1.命令行参数
$0 $1 $2 |
echo –n “ …:” read … |
aa=`cat param.txt | gawk '/input:/{print $1}'` echo "aa is $aa" $ sh tb.sh aa is echo |
如果参数没输全时([ $# -lt 3 ]),显示这个help if [ $# lt 3 ] ;then fi |
[macg @machome ~]$ sh test.sh 23 23 test.sh: line 18: [: lt: binary operator expected |
错在哪里? 改成if [ $# -lt 3 ] ;then |
[ -z "$1" ] && help
[ "$1" = "-h" ] && help