0

I'm trying to make a script that runs through a list of paths provided from a file and edits a URL in each file, but I've run into a strange issue. Here's a reproducible example.

read -r line < paths.txt
echo $line
sed -i 's/word/otherword/' $line

The paths are coming from paths.txt, which simply contains one line that reads bat.properties with a newline character.

The output of the script is below.

$ bash urlReplacer.sh
bat.properties
: No such file or directoryies

For reference, the error sed produces in this case would normally be sed: No such file or directory. By changing the bat.properties line, I've confirmed that the 'ies' in 'properties' is the part that's being appended to the end of the error, but I really don't know what's going on with the missing 'sed' at the beginning. As you can also see, bat.properties is read in perfectly. I'm guessing this is an issue with sed misuse, but I'm not sure where the issue lies.

Grizzly
  • 179
  • 1
  • 2
  • 12
  • 3
    Redirect the output of `bash urlReplacer.sh` to a file and inspect its content using an editor (`vi`, for example). I suspect there is nothing added to the error message but, on screen, it overwrites the beginning of the second line of text. A [](https://en.wikipedia.org/wiki/Carriage_return) character included in the error message (at the end of the `bat.properties` string included in the error message) sends the cursor to the beginning of the line and the rest of the error message overwrites its beginning. Is the `paths.txt` file generated on Windows? – axiac Jul 12 '19 at 13:42
  • `dos2unix paths.txt` will probably fix it. Good luck. – shellter Jul 12 '19 at 14:05
  • Soo also [When to wrap quotes around a shell variable?](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jul 12 '19 at 14:46

1 Answers1

3

I think paths.txt has DOS line endings, also known as \r or ^M.
Delete them first or in your script. You can use dos2unix, tr -d '\r' or:

IFS=$'\r' read -r line : < paths.txt
echo "$line"
sed -i 's/word/otherword/' "$line"

I quoted line for filenames with a space.

Walter A
  • 19,067
  • 2
  • 23
  • 43