如何在Bash中为位置参数赋值?我想为默认参数赋值:
if [ -z "$4" ]; then
4=$3
fi
Run Code Online (Sandbox Code Playgroud)
表示4不是命令.
msw*_*msw 33
在set内置的设置位置参数的唯一途径
$ set -- this is a test
$ echo $1
this
$ echo $4
test
Run Code Online (Sandbox Code Playgroud)
其中,--防止东西,看起来像选项(例如-x).
在您的情况下,您可能需要:
if [ -z "$4" ]; then
set -- "$1" "$2" "$3" "$3"
fi
Run Code Online (Sandbox Code Playgroud)
但它可能会更清楚
if [ -z "$4" ]; then
# default the fourth option if it is null
fourth="$3"
set -- "$1" "$2" "$3" "$fourth"
fi
Run Code Online (Sandbox Code Playgroud)
您可能还想查看参数计数$#而不是测试-z.
| 归档时间: |
|
| 查看次数: |
8702 次 |
| 最近记录: |