LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   verifing input from bash read (https://www.linuxquestions.org/questions/programming-9/verifing-input-from-bash-read-276953/)

michael_util 01-12-2005 12:19 PM

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.

bigearsbilly 01-13-2005 09:09 AM

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


All times are GMT -5. The time now is 06:43 AM.