Shell脚本传参数方法总结

2019-09-23 09:26:46王冬梅

echo "我有 $# 参数"

count=1
while [ "$#" -ge "1" ];do
    echo "参数序号为 $count 是 $1"
    let count=count+1
    shift
done

一个参数执行

[root@svn shell_example]# sh manyparams.sh one

我的名字是 manyparams.sh - 我是调用自 manyparams.sh
我有 1 参数
参数序号为 1 是 one

5个参数执行


[root@svn shell_example]# sh manyparams.sh one two three four five

我的名字是 manyparams.sh - 我是调用自 manyparams.sh
我有 5 参数
参数序号为 1 是 one
参数序号为 2 是 two
参数序号为 3 是 three
参数序号为 4 是 four
参数序号为 5 是 five