0

So I am trying to read each line of a file but it gives me an error.

No such file or directory

But the file does exist

here is the code:

echo the file path is $pathToGo
while read p
do 
    echo $p
done < $pathToGo

output:

error image

Now if I hard code the path it works just fine:

pathToGo="C:\Users\sorel\Bash\CW3\4\files\indexFiles\3346"
echo the file path is $pathToGo
while read p
do 
    echo $p
done < $pathToGo

I have also tried this code on a Linux machine, with a different path, and the same error is showing...

any help would be much appreciated.

Augustin
  • 33
  • 1
  • 7
  • 1
    Your script has dos line endings. Remove them. – KamilCuk Nov 17 '21 at 15:39
  • We know this because the error message has the colon at the start of the line. A "normal" bash error message looks like `bash: /some/file: No such file or directory` -- the "hidden" carriage return at the end of the contents of `$pathToGo` returns the cursor to the start of the line for the `: No such file...` part of the error message. – glenn jackman Nov 17 '21 at 15:49
  • Thanks for the help. I have changed my source code to Unix (LF) with notepad++. But the error remains. Any suggestion on how to remove the dos line ending? Thanks again. – Augustin Nov 17 '21 at 16:04
  • 1
    Going forward, please try http://shellcheck.net/ before asking for human assistance. – tripleee Nov 17 '21 at 17:46
  • You don't show where the variable `pathToGo` gets into your script, so we can't say how the carriage return got into it. A [mre] needs to be complete enough to reproduce the problem described. – Charles Duffy Nov 17 '21 at 18:14
  • @Augustin Where is the variable `pathToGo` coming from? If it's read from some other file, that file may have DOS/Windows line endings. – Gordon Davisson Nov 17 '21 at 18:20
  • the pathToGo is coming from a windows .txt file... I switched the EOL of the .txt with notepad++. Now it is working perfectly fine. Thanks – Augustin Nov 17 '21 at 18:41

1 Answers1

0

try below : I just added -r option .

echo the file path is $pathToGo
while read -r p
do 
    echo $p
done < $pathToGo
SuS
  • 61
  • 1
  • 10