Input Validation Shell Script

development, Technical Add comments

I am Back but this time for some code sharing…..

I have been working on Unix Shell scripts for standalone work and came across the need for various types of input validation.

The basic need of Input validation comes when we have some other program’s depending on the input and those programs being sensitive to type of data  being send to them.

so i have compiled some neat hacks for this specific purpose….

You can check at the bottom for a detailed usage of the functions that i have created.

A direct download is also available here

function num_check()

{

read -p "$1" as

until expr $as - 1 >>/dev/null 2>/dev/null

do

read -p "$2 should be numeric please enter again" as

done

echo $as

}

function char_check()

{

read -p "$1" as

while expr $as - 1 >>/dev/null 2>/dev/null

do

read -p "$2 should not be numeric please enter again" as

done

echo $as

}

function null_check()

{

read -p "$1" as

while [ -z $as ]

do

read -p "$2 should not be null " as

done

echo $as

}

function alphacheck()

{

read -p "$1" as

while echo $as | grep [0-9!@#$%^&*()-_|] 2>>/dev/null >>/dev/null

do

read -p "$2 should be alphabet only" as

done

echo $as

}

#

#       sample calling for the script

#

#

#z=`num_check "Please enter the no" "Customer no" `

#echo $z

#echo `expr $z + 1`

#y=`char_check "Please enter the name" "Customer name"`

#echo $y

#echo $y | wc -c

#x=`null_check "Please enter enrollment no" "enrollment no"`

#q=`alphacheck "Please enter the name" "Customer name"`

#echo $q

I hope this helps someone

Waiting for comments and corrections……….

10 Responses to “Input Validation Shell Script”

  1. ptFoe Says:

    very useful

    This comment was originally posted on Digg

  2. Margarette Fix Says:

    Very useful Info Thank You for sharing

  3. Anonymous Says:

    Would you be able explain a bit or leave comments for newbies to understand please…
    E.g. what does the below line of code do.. especially the 1 >>/dev/null and 2>/dev/null

    until expr $as – 1 >>/dev/null 2>/dev/null

  4. admin Says:

    hi munalemj the above code secion you refered is the par which allows us to redirect the output from appearing on screen to /dev/null which is equivalent of destroying what ever is received as output.

    the reason for doing this is because we are more interested in the return state of the program instead of the output.

    also 2> /dev/null allows us to redirect the error stream to /dev/null.

    error stream is outputs which are marked as error.

  5. Angelo Kolkmann Says:

    I really enjoy the article post.Much thanks again. Fantastic.

  6. ojasvi Says:

    Its really helpful thanks

  7. Cheap Printing Says:

    Outstanding post, you have pointed out some great details , I as well believe this s a very great website.

  8. Lucy C. Kriner Says:

    Hey, Is it ok if i use some of the things i saw here for my college paper?! I will put your site’s name as reference of course!

  9. Yogi Says:

    Thanks sir! It is yery usefull

  10. cables Says:

    Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thnkx0WONDERFUL Post.thanks for share..more wait ..

Leave a Reply

CommentLuv badge