LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-14-2006, 09:46 AM   #1
FrankCappas
LQ Newbie
 
Registered: Jul 2006
Location: Nashville, TN
Posts: 1

Rep: Reputation: 0
Question Help Me!


Using Linux - bash
I am working on a shell scrip where I ask to input a "whole number". How can make a check to see if the number they input is a whole number? If is not a whole number I will write an error message and send back to "Input a Whole Number".

Please help! E-mail me back at Frank.Cappas@CIGNA.Com is you can.


Frank
 
Old 07-14-2006, 10:02 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
For future reference, please select a topic that better describes your problem, like "BASH Script - Input Whole Number" or something similar.

As far as I know, there's no easy way to confirm that the value entered by a user is an integer. I would think that the following would work, though:
Code:
if [ $(($VAR + 0)) != $VAR ]
    then echo Not an integer.
else
    echo Integer found.
fi
 
Old 07-15-2006, 08:42 AM   #3
AarnoK
LQ Newbie
 
Registered: Jul 2006
Location: Wellington, New Zealand
Distribution: RedHat9, fc5
Posts: 4

Rep: Reputation: 0
Just trying to add zero does not really work, as it will produce
syntax error if the input was not an integer.
But then we can use this fact in the following way:

Code:
#!/bin/bash

echo -n "Enter a number: "
read val
haveInteger=0
(test $(($val+0)) == $val)2>/dev/null && haveInteger=1
if [ $haveInteger == 1 ]
   then echo Got an integer.
   else echo Not an integer.
fi
Integers will pass the test command, but anything else fails
and $haveInteger remains zero.

- Aarno
 
Old 07-15-2006, 09:04 AM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
I got no syntax error in testing my code, unless $VAR is not set, or is an empty string. Can you demonstrate the circumstances in which an error occurs?
Code:
if [ -z $VAR ]
    then echo Null string\!
    return 1
fi
if [ $(($VAR + 0)) != $VAR ]
    then echo Not an integer.
else
    echo Integer found.
fi
 
Old 07-15-2006, 09:17 AM   #5
AarnoK
LQ Newbie
 
Registered: Jul 2006
Location: Wellington, New Zealand
Distribution: RedHat9, fc5
Posts: 4

Rep: Reputation: 0
The first approach works fine for separating words from integers,
but for decimal numbers I got, for example

Enter a number: 3.14
./ww.bash: line 6: 3.14 + 0: syntax error in expression (error token is ".14 + 0")

Cheers, Aarno
 
Old 07-15-2006, 07:51 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
Ah, very true. I (sadly) did not thoroughly test my code. I had tried begins-with-. decimals (i.e, .12), strings, integers, and nulls, but no >1 decimals.
 
Old 08-01-2006, 08:10 PM   #7
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Smile

May be this can help?
Code:
#!/bin/bash

read VAR
if [ `echo "$VAR/1" | bc -l | tail -c21` != 00000000000000000000 ]; then
        echo Not a whole number
else
        echo Whole number
fi

Last edited by anupamsr; 08-01-2006 at 08:16 PM.
 
Old 08-02-2006, 12:10 AM   #8
ckin2001
LQ Newbie
 
Registered: Jul 2006
Location: Chambana
Distribution: debian
Posts: 17

Rep: Reputation: 0
http://sources.gentoo.org/viewcvs.py...ash.in?rev=183

They have a function to make sure the user inputs a positive whole number. A far easier way to do this is to assume that anything without a decimal is a whole number, and anything with one is not.

Hint : AWK
 
  


Reply

Tags
bash, linuxquestions, script, scripting


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 On
HTML code is Off



All times are GMT -5. The time now is 06:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration