LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 01-12-2005, 12:19 PM   #1
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Rep: Reputation: 15
verifing input from bash read


Hello,

I have a simple shell script that read input from a user. The input is only a number or single letter. What would be the best way to verify the input or remove any harmful characters ?

I was thinking about using:

--snip--
read inputvariable
echo ${inputvariable} | grep -E '^[[:alnum:]]{1,2}$' > /dev/null

if [ $? -eq 0 ]; then
continue because the input is only 1-2 digits and is either a number or letter
else
exit 1
fi
--snip--

Is that enough though ??

Michael.
 
Old 01-13-2005, 09:09 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
you could use case.

you can use single characters or wildcards.
the *) at the end is like the default

So if there are no matches it falls through to the end.

e.g:
Code:
echo input  y or number or grockle_bum grockle_leg
while read x; do

    case $x in

         [yYNn]) echo ok
             ;;

         [0-9]|[0-9][0-9]) echo number
              ;;

          groc*)
              echo ooh a grockle!
              ;;
            *)
            echo doh
            break 2
            ;;
        esac
echo input  y or n or grockle_bum grockle_leg
        done
~
man bash
also look at the select statement maybe
try

Code:
select file in *;do cksum $file;done
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - how to take input as a variable? trees Linux - General 1 02-19-2004 04:09 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
Verifing ISOs zentu Mandriva 2 10-23-2003 04:35 PM
bash-script input aizkorri Programming 7 07-08-2003 06:15 AM
Text input in bash? pk21 Programming 12 06-05-2003 09:23 AM


All times are GMT -5. The time now is 06:03 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
Open Source Consulting | Domain Registration