Results 1 to 6 of 6
In my script below the if statement is executing even when the condition (ISEVEN=false) is false. It is the if statement that's in the while loop.
Code:
#! /bin/bash -x
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-08-2013 #1Linux Newbie
- Join Date
- Dec 2010
- Posts
- 110
BASH - false if executing
In my script below the if statement is executing even when the condition (ISEVEN=false) is false. It is the if statement that's in the while loop.
Code:#! /bin/bash -x # Program to take user input number and return it as a sentence. # # # read -p "Enter a number no higher than the billions range" NUM NUMBEROFDIGITS=${#NUM} # Groups array NumberOfGroupsArray=() ############################### ### FUNCTION-ProcessNumbers ### ############################### # am remvoing #################### ### END FUNCTION ### #################### ################################### ### FUNCTION-FindNumberOfGroups ### ################################### FindNumberOfGroups() { if [[ `expr ${NUMBEROFDIGITS} % 3` -ne 0 ]]; then # line is good for determining if there is an odd number of digits when dividing by 3 { let NUMBEROFGROUPS=NUMBEROFGROUPS+1 ISEVEN=false fi #} } #################### ### END FUNCTION ### #################### NUMBEROFGROUPS=$( expr ${NUMBEROFDIGITS} / 3 ) ISEVEN=true FindNumberOfGroups echo ${NUMBEROFGROUPS} while [ ${NUMBEROFGROUPS} -gt 0 ] do # this code is to deal with the first two digits when and if NUM is not even when divded by 3 if [[ ${ISEVEN}==false ]]; then EVENDIGITS=$(( NUMBEROFDIGITS / 3 )) echo ${EVENDIGITS} let EVENDIGITS=EVENDIGITS*3 echo ${EVENDIGITS} #echo ${NUM:EVENDIGITS} ODDDIGITS=${NUM:EVENDIGITS} echo ${ODDDIGITS} fi D1=${NUM:0:1} D2=${NUM:1:1} D3=${NUM:2:1} #echo ${NUMBEROFGROUPS:0:4} echo "${D1} ${D2} ${D3}" let NUMBEROFGROUPS-- #echo ${NUMBEROFGROUPS:0:3} #echo "${D1} ${D2} ${D3}" done #echo "${D1} ${D2} ${D3}"
- 01-08-2013 #2Just Joined!
- Join Date
- Dec 2012
- Location
- Utah
- Posts
- 25
You may need to add a $ in front to get the actual value
Never mind... See below!Code:if [[ $`expr ${NUMBEROFDIGITS} % 3` -ne 0 ]]Last edited by aomimezura; 01-08-2013 at 09:56 PM. Reason: WHOOPS!
- 01-08-2013 #3Linux Newbie
- Join Date
- Dec 2010
- Posts
- 110
That part of my code seems to be running well?
- 01-08-2013 #4Just Joined!
- Join Date
- Dec 2012
- Location
- Utah
- Posts
- 25
Sorry, wrong line. Try putting quotes around the variable:
Code:if [[ $"{ISEVEN}"==false ]]
- 01-09-2013 #5Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
hi,
aomimezura: learn before teaching! everything you're saying is wrong.
Garrett85: for code sake, please stop using `expr'; I tell you again: it's a useless external command when you're coding with bash.
there should be spaces around test operator `if [ $isEven = false ]`
we've already told to use function's return valuewill print "failure", because function returns more than 0.Code:myFunction() { return 1 } if myFunction then echo success else echo failure fi
if you don't use [[ feature then keep on using [, or `test'.
uppercase variable names is for environmental variables, not your own; you can mix lower and uppercase for readibility.
- 01-09-2013 #6Linux Newbie
- Join Date
- Dec 2010
- Posts
- 110
Okay but I was told that there is no using return in a function in bash.



