最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ubuntu - First cat in Shell File giving error File not found even though file exists, but last cat command Works - Stack Overflo

programmeradmin0浏览0评论

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
  • cat: 'test.txt'$'\r' the script itself also contains CRLF – Fravadona Commented Feb 3 at 19:34
Add a comment  | 

1 Answer 1

Reset to default 0

Most 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".

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论