您可以使用模式匹配在已加载子系统列表中搜索子系统。
#! /bin/bash
commands=('subsystem1 reload' 'subsystem2 reload' 'subsystem3 reload')
loaded='subsystem1,subsystem2';
new_commands=()
for c in "${commands[@]}" ; do
subsystem=${c%% *}
if [[ $loaded, == *$subsystem,* ]] ; then
new_commands+=("$c")
fi
done
for c in "${new_commands[@]}" ; do
echo "$c"
done
更强大的方法是使用关联数组(需要 bash 4):
#! /bin/bash
commands=('subsystem1 reload' 'subsystem2 reload' 'subsystem3 reload')
loaded='subsystem1,subsystem2';
declare -A loaded_a
IFS_=$IFS
IFS=,
for subsystem in $loaded ; do
loaded_a[$subsystem]=1
done
IFS=$IFS_
new_commands=()
for c in "${commands[@]}" ; do
subsystem=${c%% *}
if [[ ${loaded_a[$subsystem] } ]] ; then
new_commands+=("$c")
fi
done
for c in "${new_commands[@]}" ; do
echo "$c"
done