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