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……….
















June 9th, 2009 at 8:31 am
very useful
This comment was originally posted on Digg
January 15th, 2010 at 3:04 am
Very useful Info Thank You for sharing
August 4th, 2010 at 8:41 pm
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
August 6th, 2010 at 12:48 am
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.
February 19th, 2011 at 1:27 pm
I really enjoy the article post.Much thanks again. Fantastic.
March 24th, 2011 at 11:45 pm
Its really helpful thanks
March 28th, 2011 at 2:15 pm
Outstanding post, you have pointed out some great details , I as well believe this s a very great website.
March 30th, 2011 at 7:49 am
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!
May 1st, 2011 at 6:40 am
Thanks sir! It is yery usefull
May 11th, 2011 at 4:33 pm
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 ..