使用bash脚本读取文件
我需要使用“Do/While”循环读取文件。使用bash脚本读取文件,bash,Bash,我需要使用“Do/While”循环读取文件。 如何将内容作为字符串读取 这是我的密码: cat directory/scripts/tv2dbarray.txt | while read line do echo "a line: $line" done 错误: test.sh: line 4: syntax error near unexpected token `done' test.sh: line 4: `done' 编辑: 要将文件内容放入变量,请使用: foo="`cat f
如何将内容作为字符串读取 这是我的密码:
cat directory/scripts/tv2dbarray.txt | while read line
do
echo "a line: $line"
done
错误:
test.sh: line 4: syntax error near unexpected token `done'
test.sh: line 4: `done'
编辑:
要将文件内容放入变量,请使用:
foo="`cat file`"
没有理由在这里使用
cat
——它不添加任何功能,并生成不必要的进程
while IFS= read -r line; do
echo "a line: $line"
done < file
而IFS=read-r行;做
回音“一行:$line”
完成<文件
要将文件内容读入变量,请使用
foo=$(<file)
foo=$(这应该可以:
file=path/of/file/location/filename.txt
while IFS= read -r varname; do
printf '%s\n' "$varname"
done < "$file"
file=path/of/file/location/filename.txt
而IFS=read-r varname;do
printf“%s\n”$varname
完成<“$file”
试试看
while read p
do
echo $p
done < directory/scripts/tv2dbarray.txt
在读取p时
做
回声$p
完成
test.sh:第5行:意外标记附近的语法错误done'test.sh:第5行:
done'请检查一次:此代码没有问题。脚本中还有其他内容吗?可能是另一个while
或if
或不平衡的引号、大括号、括号或圆括号?我希望我能再多投几次在过去的一个小时里,我用了很多种方式来表达这个问题,直到现在,每次我都会按照“使用猫”的思路找到答案。最后,明智的一行回答(与猫不同,它很有效。)谢谢你的编辑,@gniourf_gniourf--你每天都能学到新东西!
while read p
do
echo $p
done < directory/scripts/tv2dbarray.txt