0

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.

16
  • 1
    where do you call Execution_IT? and what is Session_IT? Commented Feb 8, 2016 at 17:24
  • 1
    did you mean to recalculate the date at every line, or just once at the beginning of the script? Commented Feb 8, 2016 at 17:26
  • I copied over the wrong func name at the bottom, which is pretty much the same. Now i call Execution_IT. I also wanted to print out d/t at every line, so yes that is as it should be. Commented Feb 8, 2016 at 17:34
  • 1
    BTW, what's with the read $3? You don't have any variable name passed to the function as $3 (as when you call the function it's with no arguments at all), and if you expect read to be setting the value in $3... well, it doesn't work that way. Commented Feb 8, 2016 at 17:47
  • 1
    ...but $3 doesn't mean column-3 in bash; it's only awk where it means that. Commented Feb 8, 2016 at 17:52

2 Answers 2

2

Your printf needs a newline:

printf "%d\n" "$n"

or, alternatively,

echo "$((++n))"
Sign up to request clarification or add additional context in comments.

5 Comments

$n, not "$n"? Do we want string-splitting and glob expansion here? (Granted, numeric IFS values aren't common, but they could happen).
@CharlesDuffy No, we don't want them here ;)
I think we definitely want string splitting here. Another thing that just didn't work was something like this: front=${texttext{$back}} and calling $back but that didnt happen as well. Somebody just suggested that to me.
@NiU, huh? "String splitting", in this context, means that if IFS=0, and your $n is 301, you would print 3 and 1 on two different lines, instead of 301 on one line. I can't conceive of why you would want that.
@NiU I think you're talking about something else here, and no that double indirection doesn't work.
1

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.

1 Comment

this is exactly what i was trying to accomplish. regarding your first example i never thought of using tee even just for the sake of testing. the second example does the job perfectly and im just not skilled enough to think of that. regarding multiple executions- im avoiding that with a condition statement beforehand to deny the execution of any other script until the ongoing one has finished, so thats ok. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.