Arrays bash中的非工作数组
有人能帮我解决BASH中数组的问题吗? 我有以下代码:Arrays bash中的非工作数组,arrays,bash,ksh,Arrays,Bash,Ksh,有人能帮我解决BASH中数组的问题吗? 我有以下代码: i=1 cat test.txt | while read LINE; do string=$(echo $LINE | sed -e 's/\(^[^=]*\):[^=]*$/\1 /') log_content[$i]="$string" echo -e "\t $i) ${log_content[$i]}" i=$(expr $i + 1) done pattern=$(echo ${log_conte
i=1
cat test.txt | while read LINE; do
string=$(echo $LINE | sed -e 's/\(^[^=]*\):[^=]*$/\1 /')
log_content[$i]="$string"
echo -e "\t $i) ${log_content[$i]}"
i=$(expr $i + 1)
done
pattern=$(echo ${log_content[1]}) - this is zero :(
当我使用ksh而不是bash时,一切都很好。当我使用BASH时(aj出于许多其他目的希望使用BASH),变量“pattern”desnt有任何值。即使我想显示“log_content”数组的全部内容,也没有任何内容。
非常感谢。您的
log\u内容
变量正在运行while
循环的子shell中填充,因此外壳中log\u内容
变量的值永远不会改变
要缓解此问题,应通过使用输入重定向而不是管道来避免创建子shell:
while read LINE; do
...
done < test.txt
读行时;做
...
完成
这应该行得通