OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Validating Input

With our new ability to have keyboard input comes an additional programming challenge, validating input. Very often the difference between a well-written program and a poorly written one lies in the program’s ability to deal with the unexpected. Frequently, the un- expected appears in the form of bad input. We’ve done a little of this with our evaluation programs in the previous chapter, where we checked the values of integers and screened


out empty values and non-numeric characters. It is important to perform these kinds of programming checks every time a program receives input, to guard against invalid data. This is especially important for programs that are shared by multiple users. Omitting these safeguards in the interests of economy might be excused if a program is to be used once and only by the author to perform some special task. Even then, if the program per- forms dangerous tasks such as deleting files, it would be wise to include data validation, just in case.

Here we have an example program that validates various kinds of input:



#!/bin/bash

# read-validate: validate input invalid_input () {

echo "Invalid input '$REPLY'" >&2 exit 1

}


read -p "Enter a single item > "


# input is empty (invalid)

[[ -z $REPLY ]] && invalid_input


# input is multiple items (invalid)

(( $(echo $REPLY | wc -w) > 1 )) && invalid_input


# is input a valid filename?

if [[ $REPLY =~ ^[-[:alnum:]\._]+$ ]]; then echo "'$REPLY' is a valid filename."

if [[ -e $REPLY ]]; then

echo "And file '$REPLY' exists."

else

echo "However, file '$REPLY' does not exist."

fi


# is input a floating point number?

if [[ $REPLY =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then echo "'$REPLY' is a floating point number."

else

echo "'$REPLY' is not a floating point number."

fi


# is input an integer?

if [[ $REPLY =~ ^-?[[:digit:]]+$ ]]; then echo "'$REPLY' is an integer."

else

echo "'$REPLY' is not an integer."

#!/bin/bash

# read-validate: validate input invalid_input () {

echo "Invalid input '$REPLY'" >&2 exit 1

}


read -p "Enter a single item > "


# input is empty (invalid)

[[ -z $REPLY ]] && invalid_input


# input is multiple items (invalid)

(( $(echo $REPLY | wc -w) > 1 )) && invalid_input


# is input a valid filename?

if [[ $REPLY =~ ^[-[:alnum:]\._]+$ ]]; then echo "'$REPLY' is a valid filename."

if [[ -e $REPLY ]]; then

echo "And file '$REPLY' exists."

else

echo "However, file '$REPLY' does not exist."

fi


# is input a floating point number?

if [[ $REPLY =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then echo "'$REPLY' is a floating point number."

else

echo "'$REPLY' is not a floating point number."

fi


# is input an integer?

if [[ $REPLY =~ ^-?[[:digit:]]+$ ]]; then echo "'$REPLY' is an integer."

else

echo "'$REPLY' is not an integer."

Validating Input


fi else

echo "The string '$REPLY' is not a valid filename."

fi

fi else

echo "The string '$REPLY' is not a valid filename."

fi


This script prompts the user to enter an item. The item is subsequently analyzed to deter- mine its contents. As we can see, the script makes use of many of the concepts that we have covered thus far, including shell functions, [[ ]], (( )), the control operator

&&, and if, as well as a healthy dose of regular expressions.


Top OS Cloud Computing at OnWorks: