我有一个名为dosmt的脚本,我输入了几个args然后打印了一些东西:
if [ "${@: -1}" == "--ut" ]; then
echo "Hi"
fi
我想要做的是删除最后一个位置参数,如果该语句为真,则为--ut
。因此,如果我的输入是$ dosmt hello there --ut
,那么它将回显Hi
,但如果我之后要打印args,我只想拥有hello there
。所以基本上我试图删除最后一个参数并尝试使用shift,但这只是暂时的,所以不起作用...
答案 0 :(得分:5)
首先,让我们设置您想要的参数:
$ set -- hello there --ut
我们可以验证参数是否正确:
$ echo "$@"
hello there --ut
现在,让我们删除最后一个值:
$ set -- "${@: 1: $#-1}"
我们可以验证是否已成功删除最后一个值:
$ echo "$@"
hello there
要将此作为脚本的一部分进行演示:
$ cat script
#!/bin/bash
echo Initial values="$@"
set -- "${@: 1: $#-1}"
echo Final values="$@"
我们可以运行你的论点:
$ script hello there --ut
Initial values=hello there --ut
Final values=hello there