Linux Shell脚本参数的获取方法
清泛原创
$0 (脚本名),$1-$9 参数$# 参数个数(脚本名除外)
$? 取上个命令退出码(exit xx, 函数return xx)一般0成功,1失败
file name: ./param.sh
param num: 0
./param.sh a
file name: ./param.sh
param num: 1
./param.sh a b
file name: ./param.sh
param num: 2
the second param: b
另:echo 默认输出换行,echo -n 不换行。
$? 取上个命令退出码(exit xx, 函数return xx)一般0成功,1失败
#!/bin/sh
echo 'file name:' $0
echo 'param num: ' $#
if [ $# -gt 1 ]; then
echo 'the second param:' $2
fi
./param.shfile name: ./param.sh
param num: 0
./param.sh a
file name: ./param.sh
param num: 1
./param.sh a b
file name: ./param.sh
param num: 2
the second param: b
另:echo 默认输出换行,echo -n 不换行。
上一篇:xxx.sh: line x: [0: command not found
下一篇:required file `./ltmain.sh' not found