LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2013, 07:03 AM   #1
mirzasoltan
LQ Newbie
 
Registered: Oct 2010
Posts: 28

Rep: Reputation: 0
help for "read" command


hi everyone,
i am writting a script that i want to get a value (that must be just digits) and do something after that. i used "read" command to get that value, like this:

read -p "please type your number: " N

now i want to check that $N is a number not text and it is only 4 digits not less and not more like digits of a year like "2013".

how i can check this part, or is read command have a switch to accept just digits not alphabets?!
 
Old 09-09-2013, 07:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
read -p "please type your number: " N

if [[ "$N" =~ ^[0-9]{4}$ ]]
then
  echo "Yep, a 4 digit number."
else
  echo "not a 4 digit number."
fi
Too my knowledge read doesn't have an option that forces numerical input.

Last edited by druuna; 09-09-2013 at 10:04 AM. Reason: Typo: an options -> an option
 
1 members found this post helpful.
Old 09-09-2013, 08:01 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Of course to ask more than once as they may make several mistakes you could try:
Code:
while read -p "Enter a number: " N && [[ ! $N =~ ^[0-9]{4}$ ]];do echo "try again";done
The other thing to consider is, what constitutes a number, ie what if they enter 0023

Whilst druuna is correct about read, you could tell bash that the variable is expecting an integer with:
Code:
declare -i N
This will return 0 for alpha starting strings but unfortunately blows up if it starts with a number and then contains alphas
and you would still need to test 4 digit boundary with >999 <10000
 
1 members found this post helpful.
Old 09-09-2013, 09:50 AM   #4
mirzasoltan
LQ Newbie
 
Registered: Oct 2010
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Have a look at this:
Code:
read -p "please type your number: " N

if [[ "$N" =~ ^[0-9]{4}$ ]]
then
  echo "Yep, a 4 digit number."
else
  echo "not a 4 digit number."
fi
Too my knowledge read doesn't have an options that forces numerical input.
tanx man, you hit the point.
 
Old 09-09-2013, 12:56 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome

BTW: Can you put up the [SOLVED] tag (link in top right hand corner or use the Thread Tools menu)
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is there a command like "read" that doesn't show the input? TITiAN Linux - Security 5 03-15-2012 10:29 PM
"short read (fsck)" on disc access (failed command: READ FPDMA QUEUED) estellnb Linux - Hardware 3 02-22-2012 06:27 PM
chmod command fails with "Read-only file system" huklBerry Linux - General 8 06-13-2008 04:35 PM
[SOLVED] which background file is read by "useradd" command ? vikas027 Solaris / OpenSolaris 1 05-09-2008 02:02 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM


All times are GMT -5. The time now is 07:20 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration