user113531 user113531 - 1 month ago 8
Linux Question

bash script file copied with additional character in filename

I am writing a bash script to copy some config files. I run the file using

sudo bash configure.sh
.

#!/bin/bash
cp config/ocr_pattern /usr/share/tesseract-ocr/tessdata/ocr_pattern
cp config/ocr_config /usr/share/tesseract-ocr/tessdata/tessconfigs/ocr_config


However when I view the changes made,
ocr_config
is copied correctly but
ocr_pattern
is copied with
ocr_pattern?
as the filename instead of
ocr_pattern
. There is an additional character
?
behind in the filename for
ocr_pattern
. What is the issue here?

cat -A

#!/bin/bash^M
cp config/ocr_pattern /usr/share/tesseract-ocr/tessdata/ocr_pattern^M
cp config/ocr_config /usr/share/tesseract-ocr/tessdata/tessconfigs/ocr_config

Answer

As shown by the output of cat -A, you have carriage return (\r) at the end of some lines causing the mentioned issues.

Remove those:

sed -i 's/\r$//' configure.sh

or just use dos2unix:

dos2unix configure.sh
Comments