【发布时间】:2018-08-07 14:56:15
【问题描述】:
我有一个输入文本文件,如下所示:
# string name | String type (x,y,or z)
name_1 | x
name_2 | y
name_3 | z
我要阅读并填写
- 包含所有字符串名称的全局数组
- 三个数组(比如 list_x、list_y 和 list_z),具体取决于字符串类型
这是我的脚本:
# Array initialization
list_global=();list_x=();list_y=();list_z=()
# Remove blank lines if there are some
sed -i '/^[[:space:]]*$/d' input_tab.txt
# Reading file
while read line
do
name=$(echo $line |awk -F "|" '{print $1}'|sed 's/ //g')
type=$(echo $line |awk -F "|" '{print $2}'|sed 's/ //g')
# Checking data are correctly read
printf "%6s is of type %2s \n" "$name" "$type"
# Appending to arrays
list_global+=("$name")
if [ "$type"==x ]
then
list_x+=("$name")
elif [ "$type"==y ]
then
list_y+=("$name")
elif [ "$type"==z ]
then
list_z+=("$name")
fi
done < input_tab.txt
# Print outcome
echo global_list ${list_global[@]}
echo -e "\n \n \n "
echo list_x ${list_x[@]}
echo list_y ${list_y[@]}
echo list_z ${list_z[@]}
这会产生以下输出
name_1 is of type x
name_2 is of type y
name_3 is of type z
global_list name_1 name_2 name_3
list_x name_1 name_2 name_3
list_y
list_z
意味着我的输入文件被正确读取,并且我填充数组的方式是有效的。 我无法理解为什么它会系统地满足它通过的第一个“如果”。如果我首先测试 if [ "$type"==z ],那么一切都会转到 list_z。
注意事项:
- 使用 switch/case 代替 if 会导致相同的结果
- 我运行 bash 4.1.2
任何帮助/解释将不胜感激, 提前致谢
【问题讨论】:
-
如果您在
[ ]中的==(或者,更便携的=)周围不留空格,则内容将被解释为单个字符串并始终评估为真。由于case不包含这样的比较,我看不出您如何使用case获得相同的结果。 -
旁注:代替
while read line然后使用awk,您可以让Bash 拆分您的行:while read name _ type为您删除空格。 -
如果你有 Bash 4.3 或更新版本,你可以使用 namerefs 并简化为
while read name _ type; do declare -n list=list_$type; list+=("$name"); done