only a number can be entered no letters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting only a number can be entered no letters?
# 1  
Old 11-29-2011
only a number can be entered no letters?

ok the user can only enter a number if a letter is entered it shouldnt be accepted
This is what i have so far

Code:
read -p "How many cars to enter:" cars
until [ "$cars" -ge 1 -a "$cars" -ne [A-Za-z]
do
read -p "Invalid number. Please re-enter:" $tags
done

# 2  
Old 11-29-2011
-ne doesn't work that way.

What you can do depends on what system and shell you have.

What's your system?

What's your shell?
# 3  
Old 11-29-2011
You can use grep and test the return of it ($?)

For instance :
Code:
read NUM
echo $NUM | grep "[a-zA-Z]"
if [ $? -eq 1 ]; then
printf "number \n"
else
printf "not a number \n"
fi

Hope that helps.
Regards
Peasant.
# 4  
Old 11-29-2011
That repeats it back to the screen, does many unnecessary things, and will return false results for strings which have characters like ! @ # $ % ^ & * ( ) _ = + .

How to do it depends on what system you have and what your shell is.

What's your system?

What's your shell?
# 5  
Old 11-29-2011
Haven't we been here before with gangsta?
https://www.unix.com/shell-programmin...ng-number.html
We left the last thread with an error on line 52 of a 3-line script.
In addition to posting the Operating Systems and version and Shell , please post the actual script and explain what you are trying to do.
# 6  
Old 11-29-2011
There's also a good answer from cfajohnson to a quite similar question here:

https://www.unix.com/shell-programmin...g-command.html

Closing this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob question: How to check the total number of inputs entered by user?

Say I have this line: read -p "Enter 3 numbers: " num1 num2 num3; I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3. I don't know the correct command to find the number of inputs entered. Help, please? (4 Replies)
Discussion started by: jejemonx
4 Replies

2. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

3. Shell Programming and Scripting

Checking whether the entered text is file or not

how to check that the "file path" entered by the user has the valid file. (6 Replies)
Discussion started by: Rashid Khan
6 Replies

4. Shell Programming and Scripting

Trying to find number of uppercase and lowercase letters.

Hello Guys, I am writing a script which check each line for how many Uppercase and Lowercase letter of a given file. Please check my script as follow: l=0 while read line do echo Line `expr $l + 1` has ` tr -cd "" < $line | wc -c` uppercase letters and `tr -cd "" < $line | wc -c`... (3 Replies)
Discussion started by: kasparov
3 Replies

5. Shell Programming and Scripting

How to continue script if right word is not entered?

Hello, I am writing a script and in this script, I want to be able to have the script continue running if the correct word is not entered... Here is an excerpt from me script: read request if ; then echo "You have asked for the System Temperature..." cat... (1 Reply)
Discussion started by: niconico96
1 Replies

6. Shell Programming and Scripting

validating user entered date

I need the date validation. I searched in the google but i didn't find my requirements. requirements: 1) user has to enter the date in YYYY/MM/DD format 2) MM validations 3) DD validations. and if the month is april it should allow 30 days only and for May month it should allow 31 days like... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

7. Programming

output the letters of the alphabet with the number of occurrences

hi, I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!... (10 Replies)
Discussion started by: svd
10 Replies

8. Shell Programming and Scripting

Repeat last entered command ?

Hi, how to do that ? I mean only print it but not execute. I'm using putty to interact with ksh. (in windows cmd up arrow does the job) thanks vilius (5 Replies)
Discussion started by: vilius
5 Replies

9. Shell Programming and Scripting

getting rid of $ when entered at the command line

Hi everyone, Can someone possibly help me with this problem I am having please. I wrote a Korn shell script to manipulate currency amounts in a way that a person could use this script to determine the minimum number of coins required to make a certain amount. for example when entered on the... (2 Replies)
Discussion started by: bashirpopal
2 Replies
Login or Register to Ask a Question