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
Forums
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
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
тАО09-01-2007 09:06 PM
тАО09-01-2007 09:06 PM
on unix shell scripting (bash, ksh, sh)
does anyone know how can I use getopt in order to examine command line parameters?
I couldn't find any helpfull info on man pages or other places on the web...
thanks
Itai.
Solved! Go to Solution.
- Tags:
- getopt/getopts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2007 10:14 PM
тАО09-01-2007 10:14 PM
Re: getopt
Example:
while getopts d:n:f:r:l: opt
do
case "$opt" in
d) dflg=1
DELAY=$OPTARG;;
r) rflg=1
RUNS=$OPTARG;;
n) nflg=1
NUM=$OPTARG;;
l) lflg=1
LF=$OPTARG;;
f) nflg=1
FILTER=$OPTARG;;
?) out_help
exit ;;
esac
done 2>/dev/null
http://www.hpuxconsulting.com/mem.mon
This script is in it's late beta stages, a memory leak monitor for HP-UX, Solaris and Linux.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2007 10:48 PM
тАО09-01-2007 10:48 PM
Re: getopt
if I have two switches that are conflicts each other (like -l and -L on lvcreate command...) how can I check it with getopts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 01:09 AM
тАО09-02-2007 01:09 AM
Solution> if I have two switches that are conflicts each other (like -l and -L on lvcreate command...) how can I check it with getopts?
Notice in the example given that as each switch is seen that a variable is set denoting that. Hence, after 'getopt' runs, write tests like:
if [ "$l_flag" = 1 -a "$L_flag" = 1 ]; then
print -u2 "Error: '-l' and '-L' are mutually exclusive"
fi
...
Since errors should be directed to STDERR I used 'print -u2' to target that file descriptor (FD2).
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 04:59 AM
тАО09-02-2007 04:59 AM
Re: getopt
in addition to SEP and JRF:
If you have to deal with plain parameters as well, don't forget to reset the argument list.
while getopts :al:L:n op
do
case $op in
a|n) ${op}flag=y ;;
l) ${op}flag=$OPTIND
if [ "$Lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;;
L) ${op}flag=$OPTIND
if [ "$lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;;
?) print -u usage ;;
esac
done
# reset arglist
shift $((OPTIND-1))
typeset -i p=0
for i # all remaining parameters
do
print param$((p+=1))="'$i'"
done
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 05:37 AM
тАО09-02-2007 05:37 AM
Re: getopt
http://docs.hp.com/en/B2355-60130/getopts.1.html
http://docs.hp.com/en/B2355-60130/getopt.1.html
http://docs.hp.com/en/B2355-60130/getopt.3C.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 06:43 AM
тАО09-02-2007 06:43 AM
Re: getopt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 07:06 AM
тАО09-02-2007 07:06 AM
Re: getopt
Not directly. You would have to have -i with a parm that is "tai". Or you would have to leave out -i and process it as an illegal option.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 07:13 AM
тАО09-02-2007 07:13 AM
Re: getopt
> can I use getopts with switches that has more than one character? (like -itai)
No, 'getopt' and 'getopts' are single-character oriented. Perl offers a 'Getopt::Long' module where options are introduced with double dashes and can have long names, like '--help' or '--verbose'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 07:04 PM
тАО09-02-2007 07:04 PM
Re: getopt
http://www.shelldorado.com/goodcoding/cmdargs.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2007 09:23 PM
тАО09-02-2007 09:23 PM
Re: getopt
in (another) addition to JRF regarding the usage of multi-character-options:
If you supply somthing like '-ita' AND NONE of the options is defined to get a parameter, all of the options i,t,a are set.
Try with my (corrected) testscript:
#!/usr/bin/ksh
while getopts :al:L:n op
do
case $op in
a|n) eval ${op}flag=y ;;
l) eval ${op}flag=$OPTARG
if [ "$Lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;fi ;;
L) eval ${op}flag=$OPTARG
if [ "$lflag" ]; then print -u2 'mutually exclusive option -l|-L ...'; exit 2;fi ;;
?) print -u usage ;;
esac
done
# reset arglist
shift $((OPTIND-1))
typeset -i p=0
for i # all remaining parameters
do
print param$((p+=1))="'$i'"
done
print Flags:
set | fgrep flag=
Example:
opt.ksh -anL val
Flags:
Lflag=val
aflag=y
nflag=y
mfG Peter