The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 6,218     Votes:  6 
Tags: command-line   bash   scripts  
Link: 🔍 See Original Answer on Ask Ubuntu ⧉ 🔗

URL: https://askubuntu.com/q/1138332
Title: Only one line in script output
ID: /2019/04/26/Only-one-line-in-script-output
Created: April 26, 2019    Edited:  April 27, 2019
Upload: September 15, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


You are writing to a file in a loop with this command:

echo "$serial"|sha256sum > Token.csv

However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:

echo "$serial"|sha256sum >> Token.csv

A single > tells bash to erase the file Token.csv and write the contents. A double >> tells bash to add to the end of the file.


The bash script would now look like this:

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

> Token.csv # Empty file from last run

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

There are two ways to create a new empty file > Token.csv as used above and touch Token.csv. However only > Token.csv will empty an existing file. See:

⇧ How to add LAN driver? How do I fix very slow scrolling USB wheel mouse (after waking from suspend) which started after upgrade to 19.04?  ⇩