特殊参数" $ @"包含以下字符串变量
回声$ @ 输出:a.bdf,b.bdf,c.nas,d.nas我想用扩展名' bdf'提取字符串变量。并将其保存在另一个数组中。 是否有可能在bash中这样做?
答案 0 :(得分:1)
使用for循环迭代它:
for ARG in "$@";do
if [[ "$ARG" == *.bdf ]];then
BDF_ARRAY+=("$ARG") #you don't need to initialize this array before the loop in bash
else #optional block, if you want to split $@ in 2 arrays
OTHER_ARRAY+=("$ARG")
fi
done
echo ${BDF_ARRAY[@]}
echo ${OTHER_ARRAY[@]}
答案 1 :(得分:0)
使用for循环
for i in "$@";do
if [[ "${i##*.}" == bdf ]]; then
ARRAY2+=("$i")
fi
done
答案 2 :(得分:0)
在通用案例中:
#!/bin/bash
for i in "$@"; do
case "$i" in
*.bdf) BDF_ARRAY+=("$i")
;;
*.nas) NAS_ARRAY+=("$i")
;;
esac
done
for i in "${BDF_ARRAY[@]}"; do echo "BDF: $i"; done
for i in "${NAS_ARRAY[@]}"; do echo "NAS: $i"; done