gpt4 book ai didi

linux - 在 linux 中读取文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:47 26 4
gpt4 key购买 nike

我一直在尝试不同的解决方案来读取 Linux 中的文件,但一点运气都没有。我的主要目的是从应用程序获取 http 响应并将其存储在文件中。

这是我在 shell 脚本文件中的代码

wget localhost:8080/abc/rest/eventservice/event -O /dev/null -S --quiet 2>&1 | grep "200 OK" > out.txt
while read -r LINE || [[ -n $LINE ]];
do
echo "$LINE";
done < out.txt

在我执行了上面的 sh 文件之后,我得到了下面的错误

'ervercheck.sh: line 6: syntax error near unexpected token `
'ervercheck.sh: line 6: ` done < out.txt

这一行 wget localhost:8080/abc/rest/eventservice/event -O/dev/null -S --quiet 2>&1 | grep“200 OK”打印HTTP/1.1 200 OK

我还注意到,当我尝试查看文件内容并键入 vi out.txt 时,内容是空的,但是当我键入 vi out.txt^M 那么内容就在那里。

请帮助我解释为什么执行 shell 文件会出现这样的错误以及为什么我的文件名末尾有 ^M

最佳答案

听起来您的脚本文件中有 windows 新行。你可以用一个shell程序dos2unix来转换它或者搜索和替换,这个答案有更多的细节。

^M at the end of every line in vim

更新:由于您在 Windows 中使用 Notepad++ ,您还可以通过 Edit -> EOL Conversion 选择行尾字符(参见 Choose newline character in Notepad++ )。完成此操作后,您不必在每次复制文件时都进行转换。

关于linux - 在 linux 中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27123021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com