LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 05:51 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 455

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, 06:00 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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, 06:05 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,903
Blog Entries: 3

Rep: Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585
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, 06:06 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,390

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

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

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, 06:43 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,903
Blog Entries: 3

Rep: Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585
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, 07:12 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

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

Rep: Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470
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 07:17 AM.
 
Old 11-21-2016, 07:29 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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, 07:33 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,390

Rep: Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470Reputation: 5470
And
Code:
((i+=1))
 
Old 11-21-2016, 08:14 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,273

Rep: Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217
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 08: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 08:06 AM
User entered variable as counter in For Loop kross2100 Linux - Newbie 3 12-06-2012 01:03 PM
Bash - Creating Arrays using loop counter Mixiul Programming 4 02-08-2012 09:05 AM
BASH assistance - loop/counter rickenbacherus Programming 6 03-12-2007 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:56 AM.

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
Open Source Consulting | Domain Registration