Getopts - space in argument (OPTARG)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getopts - space in argument (OPTARG)
# 1  
Old 05-07-2014
Getopts - space in argument (OPTARG)

Hi,

I want to capture space as well from the argument
eg:
Code:
script.ksh -m "Message from xyz" -e "email@xyz.com"
script.ksh -m 'Message from xyz' -e 'email@xyz.com'

I am parsing using getopts, but for option "m" OPTARG is returning only "Message".


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 05-07-2014 at 10:27 AM..
# 2  
Old 05-07-2014
try with
Code:
 
script.ksh -mMessage\ from\ xyz -e'email@xyz.com'

# 3  
Old 05-07-2014
I tried with escape character as well but within quotes eg.

Code:
script.ksh -m "Message\ from\ xyz" -e'email@xyz.com'

Do I need to try without quotes ? I do not have access to system now. I have to check it tomorrow.
# 4  
Old 05-07-2014
Humm, I suspect something else is going on if quotes fail. getopts in ksh is a somewhat complex beast compared to other getopts. Which version of ksh are you using?

Any of the following should work:
Code:
script.ksh -m "string with white space"
script.ksh -m 'string with white space'
script.ksh -m string\ with\ white\ space
script.ksh -m string' with'\ "whi"te" "''space

# 5  
Old 05-07-2014
From what i see, i'd assume its more how the argument is set to the variable.

Eg:
Code:
while getopts "e:m:": name
do 	case $name in
	m)	var_M="$OPTARG"	;;
	e)	var_E="$OPTARG"	;;
	esac
done

If you leave out the quotes around $OPTARG, it'll catch only the first word of a string.

Hope this helps

Last edited by sea; 05-07-2014 at 12:11 PM.. Reason: code reduced to essential
This User Gave Thanks to sea For This Post:
# 6  
Old 05-07-2014
Here's a script I wrote a long time ago as I was having the same issues.
I'll modify it as necessary when I have getopts problems.
Code:
aflag=
bflag=
while getopts :ab:t: opt
do
  case $opt in
    a) aflag=1;;
    b) bflag=1
       bval="$OPTARG";;
    t) tflag=1
       tval="$OPTARG";;
    ?) printf "Usage: %s: [-a] [-b value] args\n" $0
       exit 2;;
  esac
done
if [ ! -z "$aflag" ]; then
  printf "Option -a specified\n"
fi
if [ ! -z "$bflag" ]; then
  printf 'Option -b "%s" specified\n' "$bval"
fi
if [ ! -z "$tflag" ]; then
  printf 'Option -t "%s" specified\n' "$tval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"

Here's the output.
Code:
>getopts.sh -b "this is my opt" -t hello\ all
Option -b "this is my opt" specified
Option -t "hello all" specified
Remaining arguments are:

HTH
# 7  
Old 05-08-2014
I think I see the problem, but need a fix.

I am calling a shell script structured as below, though I am placing the arguments in quotes, looks quotes are getting lost through the $* being passed to the function. Can you please suggest a remedy.
Code:
parse_cmd () {

   getopts functionality

}


main () {
  parse_cmd $*
}
main $*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies

2. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

3. Shell Programming and Scripting

Xargs error: insufficient space for argument

I'm trying to crudely hack my way through some data processing. I have file.txt with around 17,000 lines like this: ACYPI002690-PA.aa.afa.afa.trim_phyml_tree_fullnames_fullhomolog.txt 3 72 71 ACYPI002690-PA.aa.afa.afa.trim_phyml_tree_fullnames_fullhomolog.txt 97 111 71... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

4. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

5. Shell Programming and Scripting

OPTARG is not taking SPACE

Hi I have below code in one of my shell script: if ; then fail $USAGE; fi while getopts hz:r:t:dz: o do case "$o" in h) echo $USAGE ; exit 0;; r) export REQ_ID="$OPTARG";; t) TIMESPAN="$OPTARG";; d) detail="true";; ) ... (0 Replies)
Discussion started by: mohsin.quazi
0 Replies

6. Shell Programming and Scripting

getopts : two values to a single argument ?

Hi Gurus I am trying to figure out (with not much success) how to pass two values to a single getopts argument ... for example ./script -a Tuesday sausagesThe $OPTARG variable seems to only get populated with the first argument. What im looking to do is to process the first argument (i.e.make... (6 Replies)
Discussion started by: rethink
6 Replies

7. Shell Programming and Scripting

getopts ... only allow a single instance of an argument?

Hi there, if i have a simple getopts like below ...how can i make it so that if somebody enters more than one -g argument for example, it will error with a " you cannot enter more than one -g" or something like that.? I want to only allow one instance of a -g or a -h etc .. while getopts... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

8. Shell Programming and Scripting

getopts and a list argument

Hi, I'm using bash and ksh93 compatible derivatives. In a recent getopts experience, I found myself spending far too much time on this little problem. I hope someone can help... So here's the deal. I want to build have a command line interface that accepts either zero, one, or... (4 Replies)
Discussion started by: duderonomy
4 Replies

9. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. Shell Programming and Scripting

replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument so, program will be ran ./programname hi hello hi usa now hello hello so, inside of program,... (7 Replies)
Discussion started by: convenientstore
7 Replies
Login or Register to Ask a Question