HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- getopts help
Operating System - HP-UX
1829453
Members
725
Online
109992
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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-07-2005 12:43 PM
07-07-2005 12:43 PM
Hi,
I am using the following script:
#!/usr/bin/ksh
program="${0:##*/}"
function usage {
echo "USAGE: $program [-d tardir -i instdir | -u]"
echo "d - message."
echo "i - message."
}
while getopts :d:i:u OPTION
do
case "$OPTION" in
d) tardir="$OPTARG";;
i) instdir="$OPTARG";;
u) usage;exit;;
:) print -u2 "$program: $OPTARG is missing argument!"
usage
exit 1;;
\?) print -u2 "$program: $OPTARG is an invalid option!"
usage
exit 1;;
esac
done
shift $(( $OPTIND - 1 ))
The issue is that I don't get any message when I just use:
"program"
I want it to give a message that arguments are needed to execute the program.
I get some message when I use an incorrect argument.
Please help.
Thanks,
Pat
I am using the following script:
#!/usr/bin/ksh
program="${0:##*/}"
function usage {
echo "USAGE: $program [-d tardir -i instdir | -u]"
echo "d - message."
echo "i - message."
}
while getopts :d:i:u OPTION
do
case "$OPTION" in
d) tardir="$OPTARG";;
i) instdir="$OPTARG";;
u) usage;exit;;
:) print -u2 "$program: $OPTARG is missing argument!"
usage
exit 1;;
\?) print -u2 "$program: $OPTARG is an invalid option!"
usage
exit 1;;
esac
done
shift $(( $OPTIND - 1 ))
The issue is that I don't get any message when I just use:
"program"
I want it to give a message that arguments are needed to execute the program.
I get some message when I use an incorrect argument.
Please help.
Thanks,
Pat
Solved! Go to Solution.
- Tags:
- getopt/getopts
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2005 04:13 PM
07-07-2005 04:13 PM
Re: getopts help
getopts doesn't test for required options.
you'll need to test for that yourself. something like this:
tardir=
instdir=
while getopts :d:i:u OPTION
do
.
.
.
done
if [ -z "$tardir" ] ;then
print -u2 "$program: tardir is unspecified"
usage
exit 2
fi
probably best to test for the existance of the directories also, [ ! -d "$tardir" ]
you'll need to test for that yourself. something like this:
tardir=
instdir=
while getopts :d:i:u OPTION
do
.
.
.
done
if [ -z "$tardir" ] ;then
print -u2 "$program: tardir is unspecified"
usage
exit 2
fi
probably best to test for the existance of the directories also, [ ! -d "$tardir" ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2005 04:20 PM
07-07-2005 04:20 PM
Solution
There are several ways to do this but most common is to simply test ${#} after the ${OPTIND} shift at the end of the getopts processing case statement.
e.g.
if [[ ${#} -lt 1 ]]
then
echo "Required arguments missing" >&2
exit 255
fi
When multiple arguments are required one technique is to initialize a flag variable to zero and then set this flag variable to non-zero in the getopts case statement. At the end of the case, you make sure that all of these corresponding flag variables are non-zero.
e.g.
if [[ ${#} -lt 1 ]]
then
echo "Required arguments missing" >&2
exit 255
fi
When multiple arguments are required one technique is to initialize a flag variable to zero and then set this flag variable to non-zero in the getopts case statement. At the end of the case, you make sure that all of these corresponding flag variables are non-zero.
If it ain't broke, I can fix that.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP