LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-07-2008, 02:29 AM   #1
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Rep: Reputation: 0
Bash : how do i check the user input and make sure is only chracter or only number ?


Bash : how do i check the user input and make sure is only chracter or only number ?
 
Old 09-07-2008, 02:53 AM   #2
thedonkdonk
Member
 
Registered: Oct 2005
Location: San Antonio, TX
Distribution: Fedora, RHEL 6, CentOS 6, Debian 6, Arch, Gentoo
Posts: 136

Rep: Reputation: 16
Use a regex.
 
Old 09-07-2008, 03:05 AM   #3
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Original Poster
Rep: Reputation: 0
if i only want to check the first digit wheter is a number "0" or not .
how to do it
 
Old 09-07-2008, 03:19 AM   #4
thedonkdonk
Member
 
Registered: Oct 2005
Location: San Antonio, TX
Distribution: Fedora, RHEL 6, CentOS 6, Debian 6, Arch, Gentoo
Posts: 136

Rep: Reputation: 16
That is a lot easier. Do something like this.
Code:
#!/bin/bash
echo "Enter 0, then press <ENTER>."
read INPUT # or what ever you want to call the variable.
if [[ "$INPUT" != 0 ]]
then
echo "You pressed $INPUT, which is not the same as 0. Goodbye."
exit
else
echo "Congratulations! You can read and type. You win!"
exit
fi
 
Old 09-07-2008, 03:28 AM   #5
CheeSen
LQ Newbie
 
Registered: Sep 2008
Posts: 14

Original Poster
Rep: Reputation: 0
sorry, i mean the first character .
example user enter 0asdg

then system detected that user input no avalaible because start at 0
 
Old 09-07-2008, 03:54 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
First you can try parameter substitution to extract the first character from the input string. Second, if using BASH version 3, which supports the comparison operator to match against a regular expression =~, you can do something like this:
Code:
#!/bin/bash
read -p "Enter a string: " string
if [[ ${string:0:1} =~ [[:digit:]] ]]
then
   echo digit
elif [[ ${string:0:1} =~ [[:alpha:]] ]]
then
   echo character
else
   echo other
fi
If backward compatibility is an issue, or if you don't have BASH version 3, use grep -q instead of the double brackets expressions.
 
Old 09-07-2008, 07:02 AM   #7
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
is the following code does your work ???


Code:
echo -n "Enter user i/p : "
read ip
first_char=`expr substr $ip 1 1`
if [ $first_char = "0" ]
then
	echo user i/p not available
else
	echo user i/p available
fi
 
Old 09-07-2008, 09:26 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
@OP, check out the learning bash link in my sig.
 
Old 09-07-2008, 09:43 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
OP appears to be posting a series of homework questions. In another thread, I have asked him/her to describe his/her objectives. We should also be looking for any evidence of what solutions OP has already attempted.
 
Old 09-07-2008, 11:14 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by pixellany View Post
OP appears to be posting a series of homework questions. In another thread, I have asked him/her to describe his/her objectives. We should also be looking for any evidence of what solutions OP has already attempted.
I did not think about homework, until I saw the other posts... sorry.
 
Old 09-07-2008, 03:15 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by colucix View Post
I did not think about homework, until I saw the other posts... sorry.
Eeeek!! Please don't apologize. I respond to these things all the time.

I just seem to have an ultra-sensitive homework detector.....
 
  


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
super user privileges check for a normal user in bash script freeindy Programming 2 08-01-2008 06:08 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash Y/N user input zcrxsir88 Programming 11 04-16-2008 11:35 AM
check user input for IP ilnli Programming 7 08-08-2007 06:42 PM
Bash script to check the input string length fjkum Programming 3 06-30-2007 08:43 PM


All times are GMT -5. The time now is 01:21 PM.

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