Input Validation Shell Script

Technical, development 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……….

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Diigo
  • email
  • HackerNews
  • Identi.ca
  • LinkedIn
  • Ping.fm
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter
  • Yahoo! Buzz

2 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

Leave a Reply