I'm trying to read length
characters from stdin in a bash script but I can't figure out how to pass length as a variable.
This is the script that should(tm) work:
for i in {1..9}
do
read line
done
array=(${line})
length=${array[1]}
echo "$length" >> /home/ubuntu/test
read line
read -n $length line
echo "$line" >> /home/ubuntu/test
echo "done" >> /home/ubuntu/test
but, when I cat /home/ubuntu/test
I don't see the data from the last read:
20558
done
However, if I replace read -n $length line
with read -n 20558 line
I get the expected data in the file.
How can I pass length as a variable to read
?
edit
OK, I had a trailing newline in length
. This works:
read -n ${length:0:-1} line