我需要使用"Do/While"循环读取文件.
如何以字符串形式阅读内容?
这是我的代码:
cat directory/scripts/tv2dbarray.txt | while read line
do
echo "a line: $line"
done
Run Code Online (Sandbox Code Playgroud)
错误:
test.sh: line 4: syntax error near unexpected token `done'
test.sh: line 4: `done'
Run Code Online (Sandbox Code Playgroud)
Ada*_*iss 71
没有理由在cat这里使用- 它没有添加任何功能并产生不必要的过程.
while IFS= read -r line; do
echo "a line: $line"
done < file
Run Code Online (Sandbox Code Playgroud)
要将文件的内容读入变量,请使用
foo=$(<file)
Run Code Online (Sandbox Code Playgroud)
(请注意,这会修剪尾随换行符).
Eri*_*rik 31
cat file | while read line
do
echo "a line: $line"
done
Run Code Online (Sandbox Code Playgroud)
编辑:
要将文件内容放入var use:
foo="`cat file`"
Run Code Online (Sandbox Code Playgroud)