- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Input Validation of comma separated values
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 09:58 AM
тАО05-25-2011 09:58 AM
I am working on a script and have the first part solved of numerical input validation. Below the code validates that the input is a numerical value between 100 and 1000. If not, it errors out. Now I need to be able to read values separated by a comma. For example, instead of my input being let's say 105, I'd like for the input validated to be more like 105,106,500. How can I achieve this masters of scripting?
Current code for custom function:
get_info_check ()
{
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
#INPUT VALIDATION HERE!!!
if [[ "$INFO_CHK" != +([0-9]) ]]
then
echo
echo "ERROR: You must enter a valid number!!"
get_info_check
elif [ "$INFO_CHK" -ge 100 -a "$INFO_CHK" -le 999 ]
then
echo "GOOD JOB!!!"
else
echo
echo "ERROR: Valid range is 100-999!!"
get_info_check
fi
}
Solved! Go to Solution.
- Tags:
- csv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 11:07 AM
тАО05-25-2011 11:07 AM
Re: Input Validation of comma separated values
New script with changes:
-----------------------CUT HERE-----------
get_info_check ()
{
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
echo $INFO_CHK | awk -v FS=',' '{
split($0,mystring,",")
for ( i=1;i<=NF;i++ ) {
print mystring[i];
}
}' | while read INPUT;do
#INPUT VALIDATION HERE!!!
if [[ "$INPUT" != +([0-9]) ]]
then
echo "input: ${INPUT}: not valid number"
elif [ "$INPUT" -ge 100 -a "$INPUT" -le 999 ]
then
echo "input: ${INPUT}: GOOD JOB!!!"
else
echo "input: ${INPUT}: ERROR: Valid range is 100-999!!"
echo
fi
done
}
get_info_check
-----------------CUT HERE--------------
Example run:
Which INFO check would you like to run?
The valid range is 100-999.
Enter INFO number : 100,kbe,200,kb,300,400,600
input: 100: GOOD JOB!!!
input: kbe: not valid number
input: 200: GOOD JOB!!!
input: kb: not valid number
input: 300: GOOD JOB!!!
input: 400: GOOD JOB!!!
input: 600: GOOD JOB!!!
This will take any number of comma separated entries. It will choke on null entries, leading or trailing comma.
Hth,
-klb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 11:24 AM
тАО05-25-2011 11:24 AM
Re: Input Validation of comma separated values
You might use something like this:
# cat ./mycheck
get_info_check ()
{
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
#INPUT VALIDATION HERE!!!
if [[ "$INFO_CHK" != +([0-9]) ]]
then
echo
echo "ERROR: You must enter a valid number!!"
get_info_check
elif [ "$INFO_CHK" -ge 100 -a "$INFO_CHK" -le 999 ]
then
echo "GOOD JOB!!!"
else
echo
echo "ERROR: Valid range is 100-999!!"
get_info_check
fi
}
...run as:
# ./mycheck 100 1000 666 123456
'100' is valid
ERROR: '1000' must be 100-999
'666' is valid
ERROR: '123456' must be 100-999
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 11:24 AM
тАО05-25-2011 11:24 AM
Re: Input Validation of comma separated values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 11:53 AM
тАО05-25-2011 11:53 AM
Re: Input Validation of comma separated values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 12:09 PM
тАО05-25-2011 12:09 PM
Solutionthis should do:
-----------------------CUT------
get_info_check ()
{
ERRORS=0
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
echo $INFO_CHK | awk -v FS=',' '{
split($0,mystring,",")
for ( i=1;i<=NF;i++ ) {
print mystring[i];
}
}' | while read INPUT;do
#INPUT VALIDATION HERE!!!
if [[ "$INPUT" != +([0-9]) ]]
then
ERRORS=$((ERRORS+1))
elif [ "$INPUT" -lt 100 -o "$INPUT" -gt 999 ]
then
ERRORS=$((ERRORS+1))
fi
done
if [ $ERRORS -ne 0 ];then
echo "input had $ERRORS errors"
else
echo "GOOD JOB!!!"
echo "send input to next stage: $INFO_CHK"
fi
}
get_info_check
---------------CUT
example run:
1)
Which INFO check would you like to run?
The valid range is 100-999.
Enter INFO number : 100,200,300
GOOD JOB!!!
send input to next stage: 100,200,300
2)
Which INFO check would you like to run?
The valid range is 100-999.
Enter INFO number : 100,klb,200,666,1,2,3,johndoe
input had 5 errors
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 12:47 PM
тАО05-25-2011 12:47 PM
Re: Input Validation of comma separated values
My apologies! I re-posted *your* script with *my* output! Oops. I *meant* to offer:
# cat ./mycheck
#!/bin/sh
get_info_check ()
{
typeset INFO_CHK=$1
if [ $(expr "${INFO_CHK}" : '[0-9]*') -ne $(expr "${INFO_CHK}" : '.*') ]
then
echo "ERROR: '${INFO_CHK}' be all digits"
fi
if [ "$INFO_CHK" -ge 100 -a "$INFO_CHK" -le 999 ]; then
echo "'${INFO_CHK}' is valid"
else
echo "ERROR: '${INFO_CHK}' must be 100-999"
fi
}
NUMS=$(echo "$@"|sed -e 's/,/ /g')
for NUM in ${NUMS}
do
get_info_check ${NUM}
done
exit 0
...run as:
# ./mycheck 100 1000 666 123456
'100' is valid
ERROR: '1000' must be 100-999
'666' is valid
ERROR: '123456' must be 100-999
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2011 01:30 PM
тАО05-25-2011 01:30 PM
Re: Input Validation of comma separated values
I meant to give you 10 points....