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 |
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 ] |
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 and $haveInteger remains zero. - Aarno |
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 ] |
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 |
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.
|
May be this can help?
Code:
#!/bin/bash |
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 |
All times are GMT -5. The time now is 02:19 AM. |