While Loop to Check for Integer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While Loop to Check for Integer
# 1  
Old 05-05-2013
While Loop to Check for Integer

Hello All,

In ksh I am trying to ensure that user inputs are integers otherwise redisplay prompt with the following code;

Code:
a=0
b=0
c=0

read b?"Please enter a number: "
read c?"Please enter another number: "
while [ $b != [0-9] ] && [ $c != [0-9] ]; do
read b?"Not a number. Please enter a number: "
read c?"Not a number. Please enter another number: "
done

a=`expr b \\* c`
echo $a

...but the while loop is not evaluating, I am getting an error such as (assuming l and p were entered into the prompt)
Code:
myscript[50]: l: bad number

And when the entries are actual numbers, it still generates an error such as;
myscript[50]: [0-9]: syntax error

...so it does seem there is a syntax error with the while loop. Any idea?

Last edited by techieg; 05-05-2013 at 03:03 PM.. Reason: code tags
# 2  
Old 05-05-2013
Hi, what shell is this the syntax of?
# 3  
Old 05-05-2013
Code:
read b?"Please enter a number: "

What happens when you enter the above as a command from the command prompt? Does it work? Normally, I would expect to see something like:
Code:
read -p "Please enter a number: " b

a=`expr b * c` will not work, needs to be a=`expr b \* c` to protect the * from being interpreted by the shell.
# 4  
Old 05-05-2013
Also [ $c != [0-9] ] is not valid syntax, in bash/ksh93 you would need double square brackets [[ ... ]] and then this would test for a single digit..
# 5  
Old 05-05-2013
Quote:
Originally Posted by Scrutinizer
Hi, what shell is this the syntax of?
Korn shell. I have updated the initial post to reflect this.

---------- Post updated at 02:11 PM ---------- Previous update was at 02:04 PM ----------

Quote:
Originally Posted by hanson44
Code:
read b?"Please enter a number: "

What happens when you enter the above as a command from the command prompt? Does it work? Normally, I would expect to see something like:
Code:
read -p "Please enter a number: " b

a=`expr b * c` will not work, needs to be a=`expr b \* c` to protect the * from being interpreted by the shell.
Yes, you are right, that was a forum typo on my part, it actually needed double backslahes for it to work.

Entering
Code:
read b?"Please enter a number: "

works just fine elsewhere, I just cannot get past this while loop.

---------- Post updated at 02:22 PM ---------- Previous update was at 02:11 PM ----------

Quote:
Originally Posted by Scrutinizer
Also [ $c != [0-9] ] is not valid syntax, in bash/ksh93 you would need double square brackets [[ ... ]] and then this would test for a single digit..
Hello Gurus,

[[.....]] for each evaluation was a key point. I have to ensure each evaluation was put into double square brackets such as he following

while [[ $b != [0-9] ]] && [[ $c != [0-9] ]]; do

Thanx a million.
I'm not sure why the forum is bundling together all my responses even though I am responding to each response individually.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check two condition in while loop

Hi, I Have to check two condition in while loop every 2 minutes. while loop is accompanied with number of times it will check.Please help in putting the two condition in while loop as appropriate. z= input value, A=1 while do 1.check the file output,if the file output is N then keep on... (2 Replies)
Discussion started by: netdbaind
2 Replies

2. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

3. Shell Programming and Scripting

Script to raise a integer by a exponent (while loop)

I am trying to write a script that raises a integer (m) by a exponent (n) using a while loop ex. 5 raised to the power of 2 .. I am a beginner and i dont know what is the opperand or command i have to use to make this happen..this is what i have so far... echo "Enter a integer for the... (3 Replies)
Discussion started by: jibz
3 Replies

4. UNIX for Dummies Questions & Answers

Check if input is an integer or a floating point?

Hiii I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ? Thanking you (2 Replies)
Discussion started by: krishnampkkm
2 Replies

5. Shell Programming and Scripting

WHILE LOOP CONDITION CHECK

Hello I want to compare values of two variables as CHECK condition in a while loop. eg: var1=0 var2=10 while do echo " $var1 " var1=`expr $var1 + 1` done However this is giving error.How to do it in a proper manner? Thanks. (3 Replies)
Discussion started by: dashing201
3 Replies

6. Shell Programming and Scripting

Loop check

Hi Gurus I have a recquirement where my loop should work if i have more than 4 listed files in a particular directory. Like the loop in while should continue checking if the directory has more than 4 files and should exit after there are 4 or more files ; if not more than 4 files it should... (5 Replies)
Discussion started by: r_t_1601
5 Replies

7. UNIX for Dummies Questions & Answers

Integer check (again)...

I have search the forum for an easier way to write this code. I have two separate 'if' to do this and it works but am wondering if someone knows a quick way to combine them. I want anything between 1 and 100 but not '01' or '005', '0010', etc. if ) ]] || ]; then echo "Try... (3 Replies)
Discussion started by: giannicello
3 Replies

8. Shell Programming and Scripting

loop and check

Hi, We are doing a process manually and i am trying to automate it. This is the process. We have a set of files in the directory /temp/d1. the number of files are dynamic. For ex: if i have 5 files i want to compare the chatacters 45-58 of the last line of the first file with the same... (2 Replies)
Discussion started by: dnat
2 Replies

9. Shell Programming and Scripting

Loop to check for file up to 3 times

Please forgive this I think rather basic question. I have been away from UNIX for a very long time and am in need of some help. I need to be able to check for the existance of a specific file name say 'file.dat' in a particular location If the file exists then run a second process (at... (2 Replies)
Discussion started by: JohnCrump
2 Replies

10. Shell Programming and Scripting

looking for help with a dd loop check script

Hi Can anyone please help with the following script I need - .ksh preferably? I have external disks attached to a system that I have to label and then run the dd command on all external disk found. I have kicked off the dd command as follows manually to see what its output is like first... (1 Reply)
Discussion started by: angusyoung
1 Replies
Login or Register to Ask a Question