Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在shell脚本中条件匹配时从数组中删除元素_Arrays_Shell - Fatal编程技术网

Arrays 在shell脚本中条件匹配时从数组中删除元素

Arrays 在shell脚本中条件匹配时从数组中删除元素,arrays,shell,Arrays,Shell,我在shell脚本中迭代数组,当条件匹配时,即如果找到“b”,则希望从数组中删除该元素。我没有使用索引位置来迭代。我的数组有值(a b c) 我的代码是 for h in $Arr do echo -e "Value of current element $h.\n" if [ $h == "b" ] then echo -e "Delete it\n" else echo -e "Stay here\n" fi do

我在shell脚本中迭代数组,当条件匹配时,即如果找到“b”,则希望从数组中删除该元素。我没有使用索引位置来迭代。我的数组有值(a b c)

我的代码是

for h in $Arr
do 
    echo -e "Value of current element $h.\n"

    if [ $h == "b" ]
    then
        echo -e "Delete it\n"

    else
        echo -e "Stay here\n"
    fi
done

当条件匹配时,如何删除“b”,以便在打印数组时它应该打印(a c)?

您可以使用接受的值构建临时数组
Brr
,然后使用其值分配
Arr

Arr=" a b c"
Brr=""
for h in $Arr
do
  echo -e "Value of current element $h.\n"
  if [ $h == "b" ]
  then
    echo -e "Delete it\n"
    #do not add it to Brr
  else
    echo -e "Stay here\n"
    # add it to Brr the temp array
    Brr="${Brr} $h"
  fi
done
Arr="${Brr}"
echo $Arr