UNIX通用系统变量和shell命令行参数(转)
$?
$#
$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) |