LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   while loop not printing correct counter code (https://www.linuxquestions.org/questions/linux-newbie-8/while-loop-not-printing-correct-counter-code-4175593948/)

sysmicuser 11-21-2016 05:51 AM

while loop not printing correct counter code
 
Hey Guys,

I'm having an issue with my code here:
Code:

#!/bin/bash

set -x
ARGS=1

if [ $# -ne "$ARGS" ]; then
    echo "you passed $# parameters"
    echo "Usage: `basename $0` batchsize"
    exit
fi
i=0# I treid with set i=0 as well
export myvar=geting count of rows with mysql command

echo $myvar

export batchsize=$1

echo $batchsize

while [[ $myvar -gt 0 ]]
do
i=i+1
start=$(date +%s)
sleep 5
myvar=getting an updated count of rows with select after delete
end=$(date +%s)
runtime=$((end-start))
echo -e "\n The execution runtime for $i iteration for batchsize of $(batchsize) is $runtime"
done

exit 0

The output I am getting is below:

The execution runtime for i+1 iteration for batchsize of is 5

Whereas what I want is
The execution runtime for 1 iteration for batchsize of is 5
The execution runtime for 2 iteration for batchsize of is 5

so on and so forth...


Any direction would be sincerely appreciated Guys.

jpollard 11-21-2016 06:00 AM

I think you want ${batchsize} or $batchsize instead of $(batchsize).

Personally I prefer to put all environment variables in ${} for use in strings just for emphasis as it prevents such accidents.

Turbocapitalist 11-21-2016 06:05 AM

Also, to increment your counter in bash it would be done with arithmetic expansion

Code:

i=$(($i+1));

michaelk 11-21-2016 06:06 AM

Quote:

i=i+1
Try:
i=$(($i+1))

Too late...

sysmicuser 11-21-2016 06:37 AM

Super good Guys, Working. But I just don't want solution. Tell me why i=$(($i+1)) is working ? Good tip to use {} for variable. Implemented it.

Turbocapitalist 11-21-2016 06:43 AM

The lame, unsatisfying response is that it is that way just "because" That's what POSIX has to say about how it's written:

http://pubs.opengroup.org/onlinepubs...l#tag_18_06_04

and POSIX is the standard.

The $( ) by itself is command substitution and any programs run inside are treated as if they were static text, so that might have something to do with it.

schneidz 11-21-2016 07:12 AM

i suck at memorizing syntax so i do it the long way:
Code:

i=`expr $i + 1`

michaelk 11-21-2016 07:15 AM

All languages have grammar or syntax rules and actually Bash as several ways that an arithmetic expression can be evaluated. The (( )) is compound command and basically equivalent to the let.

i=0
let i=$i+1

declare -i a
a=$i+1

https://www.gnu.org/software/bash/ma...rithmetic.html
http://wiki.bash-hackers.org/syntax/basicgrammar

grail 11-21-2016 07:29 AM

You can also perform the whole task inside the brackets and then your original code would have worked ;)
Code:

(( i = i + 1 ))
Or alternatively you could also do:
Code:

(( i++ ))

michaelk 11-21-2016 07:33 AM

And
Code:

((i+=1))

BW-userx 11-21-2016 08:14 AM

or
Code:

((++var)) or ((var=var+1)) or ((var+=1))
Your initiating the variable like this is good,
Code:

var=0
But, because you're doing it like this. It over writes your variable giving you this instead.
Code:

userx@voided1.what~>> i=0
userx@voided1.what~>> i=i+1
userx@voided1.what~>> echo $i
i+1

whereas doing it like this.
We get this:
Code:

userx@voided1.what~>> i=0
userx@voided1.what~>> ((++i))
userx@voided1.what~>> echo $i
1

try it out first in your command line, if it works in that then it will work in a BASH script.

Saves a lot of, "Why is that not working?"

When you run your script thinking it's going to work.


All times are GMT -5. The time now is 09:11 AM.