Consider the following CSV file:
jdbc.driverClassName,oracle.jdbc.driver.OracleDriver
jdbc.username,kshitiz
It has to be transformed into:
-Djdbc.driverClassName=oracle.jdbc.driver.OracleDriver \
-Djdbc.username=kshitiz \
Given the following bash snippet:
while read l; do
IFS=',' read -ra arr <<< "$l"
echo '-D'${arr[0]}'='${arr[1]}' \'
done <properties.txt
Bash prints:
\jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
\jdbc.username=kshitiz
What's going on here?
Best Answer
What your script has actually output is
-Djdbc.driverClassName=oracle.jdbc.driver.OracleDriver
\
(note the space)Why has your script printed a carriage return? Because your input file does not have Unix-convention newlines, but has CRLFs instead, and the carriage return at the end of each line has been considered part of the second array variable.
Why is whitespace like a carriage return being appended to a variable by
read
? Because you changedIFS
.Some words to the wise:
printf
, notecho
— especially, as here, when you are passingecho
things that look like command-line options and things with backslashes in.hexdump -C
,cat -v
, orod -t c -t x1
.dos2unix
, it is easy to do the equivalent withtr
,sed
, orperl
.