我试图从stdin读取一个文件列表,每个文件都用换行符分隔,但是我注意到只有第一个元素被追加到列表中。我注意到了这一点,只需输入两个字符串,然后输入Q。有人能解释原因吗?
files=()
read input
while [ "$input" != "q" ] ;
do
files+=( "$input" )
read input
done
for f in $files ;
do
echo "the list of files is:"
echo "$f"
echo "The length of files is ${#files} " #prints 1, even if 2+ are entered
done
发布于 2018-05-31 13:45:33
实际上你的files+=( "$input" )
表达式正在向数组中添加元素,但没有正确地迭代它。
最后一个循环应该是:
for f in "${files[@]}"; do
echo "element is: $f"
done
$ a+=(1)
$ a+=("hello")
$ a+=(3)
$ for i in "${a[@]}"; do echo "$i"; done
1
hello
3
https://stackoverflow.com/questions/-100004651
复制相似问题