admin管理员组文章数量:1310466
I have installed WSL Ubuntu 20.04 on windows 11. I am trying to run the following File:
echo "Delete Lines that contain Given Word: "
echo "Before Deletion: "
cat "$1"
sed -i.bak "/$2/d" "$1"
echo " "
echo "After Deletion: "
cat "$1"
This code deletes all lines in a given text file that contain a given word. The command line argument I am giving in terminal is:
bash delfile.sh test.txt I
where delfile.sh is the shell script, test.txt is the text document and "I" is the word that gets its entire line deleted.
The text in test.txt is:
Hello Friend!
My Good Name Is
Johnny Jack.
I come from the
planet Earth
and I live in Italy.
I like Pasta.
Everytime I run the code, I get the following output:
Delete Lines that contain Given Word:
Before Deletion:
cat: 'test.txt'$'\r': No such file or directory
: No such file or directory
After Deletion:
Hello Friend!
My Good Name Is
Johnny Jack.
I come from the
planet Earth
and I live in Italy.
I like Pasta.
As you can see, the last cat $1 is able to print the contents of the file, but the first cat and sed commands give error "No such file or directory" even though all files are in same folder.
I tried running this same setup on a different system and it works fine there, but just not on the one I use frequently. I tried
sed -i 's/\r//' text.txt
to convert ASCII text, with CRLF line terminators to just ASCII text, but that also didn't work. All solutions I found on the internet indicate that the problem is something related to how windows and linux handle endline differently, but seeing as this code runs perfectly fine on a different system, and that the last cat command also works, I doubt that that is the case.
If the shell file simply contains sed -i.bak "/$2/d" "$1"
then my file gets edited and the lines containing "I" get deleted, but as soon as another cat $1 comes, the last one is executed and the ones above give error.
If anyone can help sort out this issue, it will be much appreciated.
I have installed WSL Ubuntu 20.04 on windows 11. I am trying to run the following File:
echo "Delete Lines that contain Given Word: "
echo "Before Deletion: "
cat "$1"
sed -i.bak "/$2/d" "$1"
echo " "
echo "After Deletion: "
cat "$1"
This code deletes all lines in a given text file that contain a given word. The command line argument I am giving in terminal is:
bash delfile.sh test.txt I
where delfile.sh is the shell script, test.txt is the text document and "I" is the word that gets its entire line deleted.
The text in test.txt is:
Hello Friend!
My Good Name Is
Johnny Jack.
I come from the
planet Earth
and I live in Italy.
I like Pasta.
Everytime I run the code, I get the following output:
Delete Lines that contain Given Word:
Before Deletion:
cat: 'test.txt'$'\r': No such file or directory
: No such file or directory
After Deletion:
Hello Friend!
My Good Name Is
Johnny Jack.
I come from the
planet Earth
and I live in Italy.
I like Pasta.
As you can see, the last cat $1 is able to print the contents of the file, but the first cat and sed commands give error "No such file or directory" even though all files are in same folder.
I tried running this same setup on a different system and it works fine there, but just not on the one I use frequently. I tried
sed -i 's/\r//' text.txt
to convert ASCII text, with CRLF line terminators to just ASCII text, but that also didn't work. All solutions I found on the internet indicate that the problem is something related to how windows and linux handle endline differently, but seeing as this code runs perfectly fine on a different system, and that the last cat command also works, I doubt that that is the case.
If the shell file simply contains sed -i.bak "/$2/d" "$1"
then my file gets edited and the lines containing "I" get deleted, but as soon as another cat $1 comes, the last one is executed and the ones above give error.
If anyone can help sort out this issue, it will be much appreciated.
Share Improve this question edited Feb 3 at 19:23 Shikhar asked Feb 3 at 19:13 ShikharShikhar 11 bronze badge 1 |1 Answer
Reset to default 0Most likely, you shell script has CR or spaces in the command that is failing (the first cat
)
Open you script with "vi" - and do "set list".
本文标签:
版权声明:本文标题:ubuntu - First cat in Shell File giving error File not found even though file exists, but last cat command Works - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741803507a2398363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
cat: 'test.txt'$'\r'
the script itself also contains CRLF – Fravadona Commented Feb 3 at 19:34