NiU February 2016

Format increment variable output in bash

I'm stuck on a rather simple problem with the increment of a variable that gets printed out as consecutive strings to a file.

Here's a sample of the code:

#!/usr/bin/bash

_SesT=`date +%Y\/%m\/%d\ %H\-%M\-%S` 

n="0"

Execution_IT(){

    while read $3
    do
        ((n++))
        printf $n 
    done < ExecLog.txt

}

echo "test" && echo $_SesT
printf "Execution $(Execution_IT) Started $_SesT\r\n" >> ExecLog.txt

The problem is that the output gets formatted like this:

Execution  Started 2016/02/08 19-06-44
Execution 1 Started 2016/02/08 19-06-44
Execution 12 Started 2016/02/08 19-06-44
Execution 123 Started 2016/02/08 19-06-45
Execution 1234 Started 2016/02/08 19-06-45
Execution 12345 Started 2016/02/08 19-06-45
Execution 123456 Started 2016/02/08 19-06-46...

... instead of:

Execution 1 Started 2016/02/08 19-06-44
Execution 2 Started 2016/02/08 19-06-44
Execution 3 Started 2016/02/08 19-06-45...

This is the most working version I got to after trying cut -d; awk; sed; and even C-like syntax for loop. There was a version very similar to this with while read line, but the output was exactly the same. Any suggestions will be well-appreciated.

Answers


Benjamin W. February 2016

Your printf needs a newline:

printf "%d\n" "$n"

or, alternatively,

echo "$((++n))"


BinaryZebra February 2016

As I understand it, you are only printing one line to the file "ExecLog.txt", and specifically, one last line is appended.

All that line contains is the count of lines in the file and the date.
That could be done better like this:

_logfile="ExecLog.txt"

n="$(wc -l <"$_logfile")"      ### count the number of lines in the log file.

_SesT="$(date +%Y\/%m\/%d\ %H\-%M\-%S)"   ### get time just before it is used.

echo "test $_SesT $_logfile"
printf "Execution %s Started %s\r\n" "$n" "$_SesT" | tee -a "$_logfile"

If you must have a loop for each line to do something else, understand that the variable n does not lose its value on exiting the function. So, it could be used later in the script.

Print it in the line to be added to the logfile:

#!/usr/bin/bash

_logfile="ExecLog.txt"

Execution_IT(){
    n="0"
    while IFS= read -r line
    do
        echo "loop $n"
        ((n++))
        # do something with $line.
    done < "$_logfile"
}

Execution_IT                              ### execute the loop.
_SesT="$(date +%Y\/%m\/%d\ %H\-%M\-%S)"   ### get time just before it is used.

echo "test time=$_SesT and count=$n"
printf "Execution %s Started %s\r\n" "$n" "$_SesT" >> "$_logfile"

Understand that this is just a simple example that does not have any control on race conditions. Some other script may write a line to the logfile between this script has counted the lines and just before the appended line is added. In that case, the count will be wrong. Or several copies of this script running could have the same (incorrect) count.

Post Status

Asked in February 2016
Viewed 1,593 times
Voted 7
Answered 2 times

Search




Leave an answer