Validating user input in unix

Each of the four numbers has a valid range of 0 to 255.

Number=

I wrote a bash script which takes numbers for calculation via user input.

/bin/bash# SCRIPT: validinteger3.sh# USAGE: validinteger3[ Input value to be validated ]# PURPOSE: validate integer input, allow negative integers also## \\\\ ////# \\ - - //# @ @# ---o OOo-( )-o OOo---## In this method input value validated using ASCII value range of# numbers (48 - 57).# Sample script provided by Bond, Thank you Bond.####################################################################### Arguments Checking ######################################################################if [ $# -eq 0 ]then echo -n "Enter input to test: " read Numberelse Number=$1fi# You can also use bellow one liner#[ $# -eq 0 ] && || Number=$1###################################################################### Main Script Starts Here ####################################################################### Check first character is - ?

# This is another method to extract substring using Substring Removal# method.

The other nice thing it does is leave the cursor at the end of your prompt, as shown here: I wouldn't normally show that, but invoking man like this brings up the "Bash builtins" man page, instead of the normal Bash shell man page, and in this case, this documentation is more of what you need.

For your reference, here is the Bash shell "read" documentation from that man page: read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...] One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name.

||

I wrote a bash script which takes numbers for calculation via user input./bin/bash# SCRIPT: validinteger3.sh# USAGE: validinteger3[ Input value to be validated ]# PURPOSE: validate integer input, allow negative integers also## \\\\ ////# \\ - - //# @ @# ---o OOo-( )-o OOo---## In this method input value validated using ASCII value range of# numbers (48 - 57).# Sample script provided by Bond, Thank you Bond.####################################################################### Arguments Checking ######################################################################if [ $# -eq 0 ]then echo -n "Enter input to test: " read Numberelse Number=$1fi# You can also use bellow one liner#[ $# -eq 0 ] && || Number=$1###################################################################### Main Script Starts Here ####################################################################### Check first character is - ?# This is another method to extract substring using Substring Removal# method.The other nice thing it does is leave the cursor at the end of your prompt, as shown here: I wouldn't normally show that, but invoking man like this brings up the "Bash builtins" man page, instead of the normal Bash shell man page, and in this case, this documentation is more of what you need.For your reference, here is the Bash shell "read" documentation from that man page: read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...] One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name.

###################################################################### Main Script Starts Here ####################################################################### Check first character is - ?

# This is another method to extract substring using Substring Removal# method.

The other nice thing it does is leave the cursor at the end of your prompt, as shown here: I wouldn't normally show that, but invoking man like this brings up the "Bash builtins" man page, instead of the normal Bash shell man page, and in this case, this documentation is more of what you need.

For your reference, here is the Bash shell "read" documentation from that man page: read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...] One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name.

Leave a Reply