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