我一直在尝试使用不同的解决方案来读取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
答案 0 :(得分:1)
听起来你的脚本文件中有windows新行。您可以使用shell程序dos2unix
进行转换或搜索和替换,这个答案有更多详细信息。
^M at the end of every line in vim
更新:
由于您在Windows中使用notepad ++,您还可以通过Edit -> EOL Conversion
选择行尾字符(请参阅Choose newline character in Notepad++)。完成此操作后,每次复制文件时都不必转换文件。