LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2016, 06:51 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Rep: Reputation: 0
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.
 
Old 11-21-2016, 07:00 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,601

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
1 members found this post helpful.
Old 11-21-2016, 07:05 AM   #3
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Also, to increment your counter in bash it would be done with arithmetic expansion

Code:
i=$(($i+1));
 
2 members found this post helpful.
Old 11-21-2016, 07:06 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,931

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
Quote:
i=i+1
Try:
i=$(($i+1))

Too late...
 
1 members found this post helpful.
Old 11-21-2016, 07:37 AM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Original Poster
Rep: Reputation: 0
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.
 
Old 11-21-2016, 07:43 AM   #6
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
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.
 
Old 11-21-2016, 08:12 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
i suck at memorizing syntax so i do it the long way:
Code:
i=`expr $i + 1`
 
Old 11-21-2016, 08:15 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,931

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
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

Last edited by michaelk; 11-21-2016 at 08:17 AM.
 
Old 11-21-2016, 08:29 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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++ ))
 
Old 11-21-2016, 08:33 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,931

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
And
Code:
((i+=1))
 
Old 11-21-2016, 09:14 AM   #11
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 2,078

Rep: Reputation: Disabled
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.

Last edited by BW-userx; 11-21-2016 at 09:31 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help on Adding one counter loop at the end of each line in a file nexsus Linux - Newbie 7 11-22-2013 09:06 AM
User entered variable as counter in For Loop kross2100 Linux - Newbie 3 12-06-2012 02:03 PM
Bash - Creating Arrays using loop counter Mixiul Programming 4 02-08-2012 10:05 AM
BASH assistance - loop/counter rickenbacherus Programming 6 03-12-2007 06:34 PM


All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration