Ubuntu – Only one line in script output

bashcommand linescripts

There are multiple lines in my input file (SerialNos.csv), like this:

4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X

I want to read each line, compute a checksum,
and write the result to another file,
but my output file Token.csv only has one output line. 
How can I process every line?

My Code:

epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239

while IFS=$'\n' read -r line || [ -n "$line" ]
do
 j=$line
 serial=${j}:${epoch}:${StringToken}
 echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"

Best Answer

Put the output redirection on the entire loop, not just the sha256sum command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.

while IFS=$'\n' read -r line || [ -n "$line" ]
do
 j=$line
 serial=${j}:${epoch}:${StringToken}
 echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv
Related Question