Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件的最后一行未在shell脚本中读取_Shell_File - Fatal编程技术网

文件的最后一行未在shell脚本中读取

文件的最后一行未在shell脚本中读取,shell,file,Shell,File,我有一个文本文件foo.txt,其中包含以下文本作为内容, 1 2 3 4 5 我有一个shell脚本 file="foo.txt" while IFS= read -r line do echo "$line" done < "$file" 如何获得如下所示的预期输出 预期输出: 1 2 3 4 1 2 3 4 5 这是因为输入文件的最后一行缺少换行符 您可以使用此循环读取所有内容: while IFS= read -r line || [ -n "$line" ]; d

我有一个文本文件
foo.txt
,其中包含以下文本作为内容,

1
2
3
4
5
我有一个shell脚本

file="foo.txt" 
while IFS= read -r line
do
   echo "$line"
done < "$file"
如何获得如下所示的预期输出

预期输出:

1
2
3
4
1
2
3
4
5

这是因为输入文件的最后一行缺少换行符

您可以使用此循环读取所有内容:

while IFS= read -r line || [ -n "$line" ]; do
    echo "$line"
done < "$file"
然后正常阅读:

while IFS= read -r line; do
    echo "$line"
done < "$file"
而IFS=read-r行;做
回音“$line”
完成<“$file”

我在输入文件中包含了一个换行符,它起作用了
while IFS= read -r line; do
    echo "$line"
done < "$file"