LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2013, 08:35 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
BASH incrementing a variable


In the code below I can not get STARTPOINT to increment by 3 each time through the loop. It starts at 0 and then goes to 3 one time and wont increment anymore after that. The starnge thing to me is that my variable NUMBEROFGROUPS is incrementing by -1 without a problem and the two lines are pretty much identiacal except for the variable name.

Code:
#!/bin/bash
set -x

##############################
### FUNCTION-leading_zeros ###
##############################

leading_zeros() {
    local NUM=${1}
    local NUMBEROFDIGITS=${2}
    local NUMBEROFGROUPS=${3}

    while [[ $(( ${#NUM} / 3 )) -ne ${NUMBEROFGROUPS} ]] || [[ ${#NUM} -lt 3  ]]; do
        NUM="0${NUM}"
    done

    echo ${NUM}
}

read -p "Enter a number no higher than the billions range" NUM

NUMBEROFDIGITS=${#NUM}
NUMBEROFGROUPS=$( expr ${NUMBEROFDIGITS} / 3 )

if [[ ${NUMBEROFDIGITS} -lt 3 ]]; then # {
    NUMBEROFGROUPS=1
    NUM=$(leading_zeros ${NUM} ${NUMBEROFDIGITS} ${NUMBEROFGROUPS})
elif [[ `expr ${NUMBEROFDIGITS} % 3` -ne 0 ]]; then # } {
    let NUMBEROFGROUPS=NUMBEROFGROUPS+1
    NUM=$(leading_zeros ${NUM} ${NUMBEROFDIGITS} ${NUMBEROFGROUPS})
fi # }

while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
    STARTPOINT=0
    CURRENTGROUP=${NUM:${STARTPOINT}:3}
    echo ${CURRENTGROUP}

    let STARTPOINT=$(( ${STARTPOINT} + 3 ))
    let NUMBEROFGROUPS=$(( ${NUMBEROFGROUPS} -1 ))
done # }

Last edited by Garrett85; 01-12-2013 at 08:37 PM. Reason: forgot someting
 
Old 01-12-2013, 08:39 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Shouldn't STARTPOINT be outside of the loop?
Code:
STARTPOINT=0

while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
    CURRENTGROUP=${NUM:${STARTPOINT}:3}
    echo ${CURRENTGROUP}

    let STARTPOINT=$(( ${STARTPOINT} + 3 ))
    let NUMBEROFGROUPS=$(( ${NUMBEROFGROUPS} -1 ))
done # }
And I suggest using (( )) instead. It's more efficient and you don't have to use $ with names.

Btw, I think the condition of that loop makes it go infinite.

Last edited by konsolebox; 01-12-2013 at 08:54 PM.
 
Old 01-12-2013, 08:57 PM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I still don't how the functions must go but as a suggestion for alternative format:
Code:
#!/bin/bash
set -x

##############################
### FUNCTION-leading_zeros ###
##############################

leading_zeros() {
    local NUM=${1}
    local NUMBEROFDIGITS=${2}
    local NUMBEROFGROUPS=${3}

    while (( ${#NUM} / 3 != NUMBEROFGROUPS || ${#NUM} < 3 )); do
        NUM="0${NUM}"
    done

    echo "${NUM}"
}

read -p "Enter a number no higher than the billions range: " NUM

(( NUMBEROFDIGITS = ${#NUM}, NUMBEROFGROUPS = NUMBEROFDIGITS / 3 ))

if [[ NUMBEROFDIGITS -lt 3 ]]; then # {
    NUMBEROFGROUPS=1
    NUM=$(leading_zeros "${NUM}" "${NUMBEROFDIGITS}" "${NUMBEROFGROUPS}")
elif (( NUMBEROFDIGITS % 3 != 0 )); then # } {
    (( ++NUMBEROFGROUPS ))
    NUM=$(leading_zeros "${NUM}" "${NUMBEROFDIGITS}" "${NUMBEROFGROUPS}")
fi # }

STARTPOINT=0

while [[ NUMBEROFGROUPS -gt 0 ]]; do # {    
    CURRENTGROUP=${NUM:STARTPOINT:3}
    echo "${CURRENTGROUP}"

    (( STARTPOINT += 3, ++NUMBEROFGROUPS ))
done # }
 
Old 01-12-2013, 09:05 PM   #4
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
konsolebox

Thanks I see I was kinda stupid putting that in the loop. I saw that myself right after I posted this. Oh, and I though I was using (( )).

Quote:
Originally Posted by konsolebox View Post
Shouldn't STARTPOINT be outside of the loop?
Code:
STARTPOINT=0

while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
    CURRENTGROUP=${NUM:${STARTPOINT}:3}
    echo ${CURRENTGROUP}

    let STARTPOINT=$(( ${STARTPOINT} + 3 ))
    let NUMBEROFGROUPS=$(( ${NUMBEROFGROUPS} -1 ))
done # }
And I suggest using (( )) instead. It's more efficient and you don't have to use $ with names.

Btw, I think the condition of that loop makes it go infinite.
 
  


Reply



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
variable not incrementing in c fakie_flip Programming 3 09-11-2010 12:25 PM
Bash: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
How can I make a self-incrementing variable? OutThere Linux - General 3 04-06-2009 09:12 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 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
Open Source Consulting | Domain Registration