I'm new to bash.
I'm trying to write a script that will read data from a text find and declare some variables. In the example below we read from a tab delimited file "ab.txt" that looks like this:
a->AA
b->BB
Where -> denotes a tab.
I'm reading this data with this code:
#!/bin/bash
while read line
do
tmp=(${line///})
fieldName=${tmp[0]}
case $fieldName in
"a")
a=${tmp[1]}
;;
"b")
b=${tmp[1]}
;;
esac
done < "ab.txt"
echo "a:"
echo $a
echo "b:"
echo $b
echo "concat a,b"
echo $a$b
echo "concat b,a"
echo $b$a
This gives me "a" and "b" nicely, but will not concatenate a and b! The output is this:
a:
AA
b:
BB
concat a,b
BB
concat b,a
AA
What am I doing wrong?
"This gives me "a" and "b" nicely"Are you sure even that part is correct? According to the code you should only be gettingAandBinstead ofAAandBB. When I ran your code I got that result and all concats were correct as well. Are you sure your input is correct?AAversusAis presumably a transcription error - however the apparent failure to concatenate is possibly another case of Windows (CRLF) line endings i.e.AA\rBB\rwill appear as justBBreading the entire line and then splitting it into an array, I'd letreaddo the splitting:while IFS=$\t\ read fieldName fieldValue