- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ksh: using of getopts and checks of wrong inputs
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
Discussions
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
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
07-10-2012 12:43 AM
07-10-2012 12:43 AM
hello,
i am using getopts and tried to prevent wrong inputs :
example :
while getopts ':c:f:m:eht' option
do
case "${option}" in
c) OPTION_C="${OPTARG}" ;;
f) OPTION_F="${OPTARG}" ;;
m) OPTION_M="${OPTARG}" ;;
s) OPTION_S="Y" ;;
e) OPTION_E="J" ;;
t) OPTION_T="J" ;;
h) echo show_Usage ; exit 2;;
[?]) echo show_Usage ; exit 2;;
esac
done
in the attachment is my getopts test script.
allowed should be :
./getopts_test.sh -f <value> -m <value> -c <value> -t
or
./getopts_test.sh -f <value> -m <value> -c <value> -e
but not allowed options "-t" and "-e" , only "-t" or "-e"
i tried to prevent this issue, but when you specify a value after "-e" or "-t" , then my script can't handle this :
OK
./getopts_test.sh -f Option_F -m 10240 -c Option_C -t
./getopts_test.sh -f Option_F -m 10240 -c Option_C -e
WRONG
./getopts_test.sh -f Option_F -m 10240 -c Option_C -e -t
./getopts_test.sh -f -m -c -t -e
ALLOWED but WRONG
./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E
possibilty of DEBUG
DEBUG=ON ./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E
regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2012 01:53 AM
07-10-2012 01:53 AM
Re: ksh: using of getopts and checks of wrong inputs
>ALLOWED but WRONG
>./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E
So this is the only problem in the script, it allows this?
The following doesn't seem to be consistent, why sleep? Why missing show_Usage for the first?
You also repeat "-e" in each error message.
if [ "${OPTION_E}" = "J" -a "${OPTION_T}" = "J" ]; then
echo "${SCRIPT}: Option \"-e\" and Option \"-e\" isn't allowed!"
exit 2
fi
if [ "${OPTION_E}" = "N" -a "${OPTION_T}" = "N" ]; then
echo "${SCRIPT}: Option \"-e\" or Option \"-e\" must be set!"
sleep 1
show_Usage
exit 2
fi
Also due to fuzziness of the English language, you might want to beef up what "and" and "or" really mean. They don't mean the same as "&&" or "||".
What you really mean for BOTH:
'Options -e and -t are mutually exclusive and one is required'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2012 04:27 AM
07-10-2012 04:27 AM
Re: ksh: using of getopts and checks of wrong inputs
So this is the only problem in the script, it allows this?
yes,this is the only problem !
What you really mean for BOTH:
'Options -e and -t are mutually exclusive and one is required'
Only one Option is allowed and should exists and not : .... "-e -t " or "-e value " or "-t value"
This is what i mean : 'Options -e and -t are mutually exclusive and one is required' sorry for my bad english explanation .
can "getopts" handle this ? i tried to handle it, with variables
regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2012 10:48 PM - edited 07-10-2012 10:49 PM
07-10-2012 10:48 PM - edited 07-10-2012 10:49 PM
Solution>yes,this is the only problem!
After your while loop you need something like:
if [ ${OPTIND} -le $# ]; then
echo "Non-option present at ${OPTIND}"
show_Usage; exit 2
fi
>Can "getopts" handle this? I tried to handle it, with variables
No, if options are required, optional, mutually exclusive or duplicates are not allowed, you must handle that yourself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2012 12:17 AM
07-11-2012 12:17 AM
Re: ksh: using of getopts and checks of wrong inputs
hello,
if [ ${OPTIND} -le $# ]; then echo "Non-option present at ${OPTIND}" show_Usage; exit 2 fi
handle wrong options like "-t Option_T", right ?
OK with your input ${OPTIND}
DEBUG=ON ./getopts_test.sh -f Option_F -m 10240 -c Option_C -t Option_T -e Option_E
Output: getopts_test.sh: Non-option present at 8
OK and i handle it in the programm
DEBUG=ON ./getopts_test.sh -f Option_F -m 10240 -c Option_C -t -e
Output: getopts_test.sh: Option "-e" and Option "-e" isn't allowed !
OK and i handle it in the programm
DEBUG=ON ./getopts_test.sh -f -m -c -t -e
Output: USAGE: getopts_test.sh -f Value_Option_F
-m Value_Option_M (Digit)
-c Value_Option_C { -e | -t}
Summary : ${OPTIND} handle wrong options and other checks i have to do with program steps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2012 02:36 AM
07-11-2012 02:36 AM
Re: ksh: using of getopts and checks of wrong inputs
>handle wrong options like "-t Option_T", right?
>${OPTIND} handles wrong options and other checks I have to do with program steps
Actually these are separate. There is a "-t" and some non-option "Option_T", even if the latter is not after the "-t".
At the end of getopts, ${OPTIND} points to where it stopped.