删除 Bash 中的位置参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6595129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:18:48  来源:igfitidea点击:

Delete positional parameters in Bash?

bashshellparameters

提问by Shelley

You can skip positional parameters with shiftbut can you delete positional parameters by passing the position?

您可以跳过位置参数,shift但可以通过传递位置来删除位置参数吗?

x(){ CODE; echo "$@"; }; x 1 2 3 4 5 6 7 8
> 1 2 4 5 6 7 8

I would like to add CODE to x()to delete positional parameter 3. I don't want to do echo "${@:1:2} ${@:4:8}". After running CODE, $@should only contain "1 2 4 5 6 7 8".

我想添加 CODE 来x()删除位置参数 3。我不想这样做echo "${@:1:2} ${@:4:8}"。运行CODE后,$@应该只包含“1 2 4 5 6 7 8”。

采纳答案by l0b0

The best way, if you want to be able to pass on the parameters to another process, or handle space separated parameters, is to re-setthe parameters:

如果您希望能够将参数传递给另一个进程或处理空格分隔的参数,最好的方法是重新set设置参数:

$ x(){ echo "Parameter count before: $#"; set -- "${@:1:2}" "${@:4:8}"; echo "$@"; echo "Parameter count after: $#"; }
$ x 1 2 3 4 5 6 7 8
Parameter count before: 8
1 2 4 5 6 7 8
Parameter count after: 7

To test that it works with non-trivial parameters:

要测试它是否适用于非平凡参数:

$ x $'a\n1' $'b\b2' 'c 3' 'd 4' 'e 5' 'f 6' 'g 7' $'h\t8'
Parameter count before: 8
a
1 2 d 4 e 5 f 6 g 7 h   8
Parameter count after: 7

(Yes, $'\b'is a backspace)

(是的,$'\b'是退格)

回答by Prince Bhanwra

x(){
    #CODE
    params=( $* )
    unset params[2]
    set -- "${params[@]}"

    echo "$@"
}

Input: x 1 2 3 4 5 6 7 8

输入:x 1 2 3 4 5 6 7 8

Output: 1 2 4 5 6 7 8

输出:1 2 4 5 6 7 8

回答by Fredrik Pihl

From tldp

来自tldp

# The "unset" command deletes elements of an array, or entire array.
unset colors[1]              # Remove 2nd element of array.
                             # Same effect as   colors[1]=
echo  ${colors[@]}           # List array again, missing 2nd element.

unset colors                 # Delete entire array.
                             #  unset colors[*] and
                             #+ unset colors[@] also work.
echo; echo -n "Colors gone."               
echo ${colors[@]}            # List array again, now empty.

回答by James

You can call set and reset the positional paramaters at any time for example

例如,您可以随时调用设置和重置位置参数

function q {
echo ${@}
set   
echo ${@}
set 
echo ${@}
}

q 1 2 3 4

then slice out what you dont want from the array, the below code does that... not sure if its the best way to do it though, was on stack looking for a better way ; )

然后从数组中切出你不想要的东西,下面的代码就是这样做的......但不确定它是否是最好的方法,正在堆栈中寻找更好的方法;)

#!/bin/bash


q=( one two three four five )

echo -e "
  (remove) { [:range:] } <- [:list:]
                | [:range:] => return list with range removed range is in the form of [:digit:]-[:digit:]
"

function remove {
  if [[  =~ ([[:digit:]])(-([[:digit:]]))?   ]]; then
    from=${BASH_REMATCH[1]}
    to=${BASH_REMATCH[3]}
  else
    echo bad range
  fi;shift
  array=( ${@} )
  local start=${array[@]::${from}}
  local rest
  [ -n "$to" ] && rest=${array[@]:((${to}+1))}  || rest=${array[@]:((${from}+1))}
  echo ${start[@]} ${rest[@]}
}

q=( `remove 1 ${q[*]}` )
echo ${q[@]}

回答by alecxs

while loop over "$@" with shift + set: move each parameter from first to last position, except "test"

while 使用 shift + set 循环“$@”:将每个参数从第一个位置移动到最后一个位置,“test”除外

# remove option "test" from positional parameters
i=1
while [ $i -le $# ]
  do
    var=""
    case "$var" in
      test)
        echo "param \"$var\" deleted"
        i=$(($i-1))
      ;;
      *)
        set -- "$@" "$var"
      ;;
    esac
    shift
    i=$(($i+1))
done