Find the answer to your Linux question:
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.
  1. #1
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122

    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}"

  2. #2
    Just Joined!
    Join Date
    Dec 2012
    Location
    Utah
    Posts
    25
    You may need to add a $ in front to get the actual value
    Code:
    if [[ $`expr ${NUMBEROFDIGITS} % 3` -ne 0 ]]
    Never mind... See below!
    Last edited by aomimezura; 01-08-2013 at 09:56 PM. Reason: WHOOPS!

  3. #3
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122
    That part of my code seems to be running well?

  4. #4
    Just Joined!
    Join Date
    Dec 2012
    Location
    Utah
    Posts
    25
    Sorry, wrong line. Try putting quotes around the variable:

    Code:
    if [[ $"{ISEVEN}"==false ]]

  5. #5
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    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 value
    Code:
    myFunction() {
       return 1
    }
    if myFunction
    then
       echo success
    else
       echo failure
    fi
    will print "failure", because function returns more than 0.

    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.

  6. #6
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122
    Okay but I was told that there is no using return in a function in bash.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •