我的脚本中的while循环是从变量中获取输入,该变量有多行,并将变量处理为一行。如何让我的while循环逐行处理变量。
我正在寻找一种方法来替代我如何分配变量"输入",就像我在这里input=$(tail -n +2 $1)
,或者我如何将该变量传递给while循环,就像我在这里done <<< $(echo $input)
脚本
input=$(tail -n +2 $1)
echo "input: $input :input"
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "output: $line :output"
done <<< $(echo "$input")
输入文件($ 1)
header
test
test2
test3
test4
输出
input: test
test2
test3
test4 :input
output: test test2 test3 test4 :output
期望输出
input: test
test2
test3
test4 :input
output: test :output
output: test2 :output
output: test3 :output
output: test4 :output
更新
我一个接一个地执行了这些命令,所以我不知道你的$(echo "$input")
命令是不是逐行传递变量
machine:folder user$ cat temp
#!/bin/BASH
input=$(tail -n +2 $1)
echo "input: $input :input"
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "output: $line :output"
done <<< $(echo "$input")
machine:folder user$ cat test
header
test
test2
test3
test4
machine:folder user$ ./temp test
input: test
test2
test3
test4 :input
output: test test2 test3 test4 :output
machine:folder user$ BASH --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.5.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.