0

I have below script:

#!/bin/bash
cat list.txt | while IFS='' read -r a; 
#Remove all illegal characters
do
   a=${a//  /_}
   a=${a// /_}
   a=${a//\'/}
   a=${a//-/_}
   a=${a,,}
   filepath="/applications/"
   b=$filepath$a".csv"
   echo $b
done

The expected results should be:

/applications/ahold_region
/applications/alb_sfy_region
/applications/awg_census_region
/applications/bjs_total_ta
/applications/delhaize_region
/applications/dollar_value_area

The real results are:

.csvlications/ahold_region
.csvlications/alb_sfy_region
.csvlications/awg_census_region
.csvlications/bjs_total_ta
.csvlications/delhaize_region
.csvlications/dollar_value_area

It looks like bash is using .csv to replace the first 4 characters in $filepath.

How to generate the expected results?

6
  • 2
    You have Windows-style CRLF line endings in the file. The CR gets left in, before you add the .csv and it moves the cursor to the beginning of line when printing.
    – ilkkachu
    Jan 19, 2018 at 17:35
  • @ilkkachu I have changed to Linux line end (LF) and still got the same results. Jan 19, 2018 at 17:41
  • @lovechillcool, did you remove the CRs from both the list.txt file and your script file? Jan 19, 2018 at 17:42
  • 1
    @glennjackman, it's not the script, a CR at the end where, say the do keyword was expected, would cause a syntax error (the shell would see do\r instead)
    – ilkkachu
    Jan 19, 2018 at 17:47
  • 1
    Kindly post list.txt content Jan 19, 2018 at 17:50

1 Answer 1

1

I have CRLF not only in the script but also in the list.txt. After I changed to LF in both shell and list.txt, the issue is resolved.

Not the answer you're looking for? Browse other questions tagged or ask your own question.