LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-08-2013, 04:24 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
failed if condition still executing


On the program below the if statement that is within the while block is executing even when that if condition fails. Any ideas?

Code:
#!/bin/bash -x

# Program to take a user numerical input number and return it as a sentence.
#
#
#
read -p "Enter a number no higher than the billions range" NUM

NUMBEROFDIGITS=${#NUM}


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}"
 
Old 01-08-2013, 08:40 PM   #2
NullTerminator
LQ Newbie
 
Registered: Jul 2006
Posts: 8
Blog Entries: 15

Rep: Reputation: 0
I tried a test like this

if [ ${ISEVEN} = false ]; then

I don't think the == is a bash construct. I tried with [[ ]] or [ ] and is seemed to work either way. I don't know why to use [[ ]] (I'm bash noob)

I did not try your entire scrpt as I have to retype it completely. My browser corrupts whitespace when I cut and paste. (running raspberrypi)
 
Old 01-08-2013, 09:41 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
my bad. doh!
 
Old 01-09-2013, 07:21 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by Garrett85 View Post
On the program below the if statement that is within the while block is executing even when that if condition fails. Any ideas?
It works for me:
Code:
+ read -p 'Enter a number no higher than the billions range' NUM
Enter a number no higher than the billions range120
+ NUMBEROFDIGITS=3
++ expr 3 / 3
+ NUMBEROFGROUPS=1
+ ISEVEN=true
+ FindNumberOfGroups
++ expr 3 % 3
+ [[ 0 -ne 0 ]]
+ echo 1
1
+ '[' 1 -gt 0 ']'
+ [[ true == false ]]
+ D1=1
+ D2=2
+ D3=0
+ echo '1 2 0'
1 2 0
+ let NUMBEROFGROUPS--
+ '[' 0 -gt 0 ']'
However, the condition is quite confusing because the variable is misnamed: ISEVEN actually means number of digits is a multiple of 3, nothing to do being even at all...

Quote:
Originally Posted by NullTerminator
I tried with [[ ]] or [ ] and is seemed to work either way. I don't know why to use [[ ]]
What is the difference between test, [ and [[ ?
 
Old 01-10-2013, 04:23 PM   #5
NullTerminator
LQ Newbie
 
Registered: Jul 2006
Posts: 8
Blog Entries: 15

Rep: Reputation: 0
Good tip on [ vs [[

ntubski

Thanks for the tip on the nuances of [ and [[
 
  


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
Condition ust Linux - Newbie 3 09-26-2012 04:18 AM
[SOLVED] if condition shashv2 Programming 2 07-29-2010 09:52 AM
Executing 'grub-install /dev/sda' failed. This is a fatal error. Melhisedek Linux - Newbie 17 07-03-2009 09:07 AM
[Dapper-upgrade] /etc/gdm/Xsession: Executing /usr/bin/gnome-session failed (dbus??) kiwibird Linux - Software 0 06-05-2006 05:12 PM
online_update failed - ERROR(Media:connection failed)[Connect failed] rover SUSE / openSUSE 8 02-22-2005 07:57 AM

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

All times are GMT -5. The time now is 07:54 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