- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ksh: optional parameter ( possible use of getopts ...
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
12-27-2013 05:59 AM
12-27-2013 05:59 AM
hello,
i tried to use
getopts
for following use. but i can't solve my task. i want to have a combination of optional parameters and also i want to know if optional parameter "-i" is used.
- without parameters ( easy )
- only optional parameters ( easy )
- combination of optional parameters
example :
- script
- script -i -o
- script -i -o param1 param1
the problem of "getopts" is :
- using "-i" without parameters
- when i want to use more parameters i need "
some inputs :
ksh: using of getopts and checks of wrong inputs
#!/usr/bin/ksh SCRIPT=${0##*/} typeset use_option="FALSE" typeset use_option_i="FALSE" typeset use_option_help="FALSE" typeset options="" if [ $# -eq 0 ] then : else while [ $# -ge 1 ] do echo "\$1:$1 *:$*" case "${1}" in -h ) use_option_help="TRUE" use_option="TRUE" ;; -\? ) use_option_help="TRUE" use_option="TRUE" ;; -[a-zA-Z]* ) echo "${1}" | grep -q "\-i" [ $? -eq 0 ] && use_option_i="TRUE" if [ -z "${options}" ] then options="${1}" else options="${options} ${1}" fi use_option="TRUE" ;; * ) params=$* ; shift $# ;; esac if [ $# -gt 0 ] then shift fi done fi echo "use_option_i ${use_option_i}" echo "options: ${options} params: ${params}" exit 0
regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2013 04:00 PM - edited 12-27-2013 04:01 PM
12-27-2013 04:00 PM - edited 12-27-2013 04:01 PM
Re: ksh: optional parameter (possible use of getopts)
>but I can't solve my task. I want to have a combination of optional parameters and also I want to know if optional parameter "-i" is used.
I don't see why getopts(1) can't be used.
>- using "-i" without parameters
>- when I want to use more parameters i need
These aren't parms to "-i". These are just parms.
if [ $# -eq 0 ]; then
:
Why not just test for "-ne 0", so you don't need this.
>echo "\$1:$1 *:$*"
You shouldn't be using $* if you want to handle embedded blanks. Instead use "$@" and make sure in quotes.
-h ) use_option_help="TRUE"
use_option="TRUE"
;;
-\? ) use_option_help="TRUE"
use_option="TRUE"
;;
You should combine the two. Also don't put a space before the ")":
-h|-\?) use_option_help="TRUE"
use_option="TRUE" ;;
> -[a-zA-Z]* ) echo "${1}" | grep -q "\-i"
This "\" does nothing. The proper syntax is:
-[a-zA-Z]*) echo "${1}" | grep -q -e "-i"
Also, why check for "-i", just have a separate case for it.
> * ) params=$* ;
Here is where you want "$@":
*) params="$@"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 01:12 AM
12-30-2013 01:12 AM
Re: ksh: optional parameter (possible use of getopts)
>> I don't see why getopts(1) can't be used.
when i want to use this combinations, how i handle this with "getopts" ?
script -i -o
script -i -o param1 param1
the problem of "getopts" is :
- when i want to use more parameters i need " like script -i -o "param1 param2"
- getopts can only has "fixed" combinations , right ?
like : -o param1 param2
but when i don't use "-o " like -i param1 param2 it is possible to handle ?
but how about
-i -o param1 param2 or -o -i param1 param2
here my code for : -i -o "param1 param2 " but not possible for -i -o param1 param2 or when i use only param1 param2 (no opt. params ) then i have to use a code like above ?
typeset options="" if [ $# -ne 0 ] then while getopts hio: option do case "${option}" in i) echo "Getopts_Option: i" optarg_i="${OPTARG}" ; ;; o) echo "Getopts_Option: o" optarg_o="${OPTARG}" ; ;; h) echo "Getopts_Option: h" echo "usage $0 ... "; exit 2 ;; [?]) echo "Getopts_Option: h" echo "usage $0 ... "; exit 2 ;; esac # case "${option}" in done # OPTIND The index of the last option argument processed by the # getopts built-in command. shift $(($OPTIND -1)) if [ ${OPTIND} -le $# ] then echo "${0}: Non-option present at Position \"${OPTIND}\" : $( eval echo \$${OPTIND} )" echo "usage $0 ... " ; exit 2 fi fi
regards
- Tags:
- getopts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 02:10 AM - edited 12-30-2013 12:04 PM
12-30-2013 02:10 AM - edited 12-30-2013 12:04 PM
Re: ksh: optional parameter (possible use of getopts)
>I want to use more parameters I need, like script -i -o "param1 param2"
You treat it as: script -i -o
Then you detect invalid options and then process them special.
I'm assuming there is NO connection between the -i and -o and param1 and param2??
Or is there? What syntax to you want to allow and is there a connection between parms and options?
>- getopts can only has "fixed" combinations, right?
Yes. It allows an optional "-(letter)" or "-(letter) parm" but not both with that same letter.
>when I don't use "-o" like -i param1 param2 it is possible to handle?
Sure.
>-i -o param1 param2 or -o -i param1 param2
Sure.
>here my code for: -i -o "param1 param2" but not possible for -i -o param1 param2 or when I use only param1 param2 (no opt. parms) then i have to use a code like above?
> while getopts hio: option
This getopts allows in any order and optional:
-h, -i or "-o o_parm"
(It also allows duplicates.)
The following in case "i" isn't valid: optarg_i="${OPTARG}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 06:03 AM
12-30-2013 06:03 AM
Re: ksh: optional parameter (possible use of getopts)
hello,
>>I'm assuming there is NO connection between the -i and -o and param1 and param2??
>> Or is there?
there is no connection, "-i" "-o" can use in any order , but when i have to get the params "param1 param2" ,
i have to specify it in "getopts .... " , right ?
> What syntax to you want to allow and is there a connection between parms and options?
no connection, "-i" "-o" are optional.
regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 06:19 AM
12-30-2013 06:19 AM
Re: ksh: optional parameter (possible use of getopts)
hello,
here my revised code of my first thread . I'm very ashamed of my bad program style ! thank you for your input's Dennis. Maybe you can teach me ( us ) with a web seminar ?
regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 12:05 PM
12-30-2013 12:05 PM
Re: ksh: optional parameter (possible use of getopts)
>there is no connection, "-i" "-o" can use in any order , but when i have to get the params "param1 param2" ,
I have to specify it in "getopts ....", right?
No. You use getopts to parse the "-" options. And when you get to the end of those, you handle the rest without getopts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 12:38 PM - edited 12-30-2013 11:15 PM
12-30-2013 12:38 PM - edited 12-30-2013 11:15 PM
Solution>> No. You use getopts to parse the "-" options. And when you get to the end of those, you handle the rest without getopts.
now i got it , a combination between "getopts" and script handling like :
#!/usr/bin/ksh typeset options="" typeset optarg_i="TRUE" typeset optarg_o="TRUE" if [ $# -ne 0 ] then while getopts hio option do case "${option}" in i) echo "Getopts_Option: i" optarg_i="TRUE" ; ;; o) echo "Getopts_Option: o" optarg_o="TRUE" ; ;; h) echo "Getopts_Option: h" echo "usage $0 ... "; exit 2 ;; [?]) echo "Getopts_Option: h" echo "usage $0 ... "; exit 2 ;; esac # case "${option}" in done # OPTIND The index of the last option argument processed by the # getopts built-in command. shift $(($OPTIND -1)) echo $# if [ ${OPTIND} -lt $# ] then echo "${0}: Non-option present at Position \"${OPTIND}\" : $( eval echo \$${OPTIND} )" echo "usage $0 ... " ; exit 2 fi echo $# echo $* param="$@" fi exit 0
now it is ok ? and i put at the end only "echo" commands for debugging
regards
- Tags:
- getopts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 01:32 PM
12-30-2013 01:32 PM
Re: ksh: optional parameter (possible use of getopts)
>now it is ok?
Well this "if" isn't right, since you have changed $# by that shift:
if [ ${OPTIND} -lt $# ]; then
>param="$@"
And unless you use "set -A" for an array, you are collecting all of the parms into one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2013 11:26 PM
12-30-2013 11:26 PM
Re: ksh: optional parameter (possible use of getopts)
>> Well this "if" isn't right, since you have changed $# by that shift:
>> if [ ${OPTIND} -lt $# ]; then
so it is better to store $# in a variable at the beginning like cnt_args=$# and change
statement to if [ ${OPTIND} -lt ${cnt_args} ]; then
does it make sense ? how i check valid getopts args ?
regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2013 12:55 PM
12-31-2013 12:55 PM
Re: ksh: optional parameter (possible use of getopts)
>so it is better to store $# in a variable at the beginning like cnt_args=$#
Yes. But what is that check trying to do? What illegal input is it trying to catch?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2014 04:33 AM
01-07-2014 04:33 AM
Re: ksh: optional parameter (possible use of getopts)
> Yes. But what is that check trying to do? What illegal input is it trying to catch?
in this case it make no sense.