Operating System - HP-UX
1753265 Members
5479 Online
108792 Solutions
New Discussion

getopt and parameters with space or "

 
SwissKnife
Frequent Advisor

getopt and parameters with space or "

Hi,

 

My script needs to accept this

myscript -o test -a -t preRefresh -v "echo "Hello world""

I'm using getopt. 

The key -v gives this value: echo

I want echo "Hello world"

 

Any ideas ?

 

Kind regards,

Den

 

6 REPLIES 6
Steven Schweda
Honored Contributor

Re: getopt and parameters with space or "

> My script needs to accept this
>
> myscript -o test -a -t preRefresh -v "echo "Hello world""

   Good luck.  Your (interactive) shell is processing those quotation
marks before "myscript" ever sees them.

> I'm using getopt.

   Inside "myscript"?  Doesn't matter.

> The key -v gives this value: echo

   Actually, I'd expect >echo <, not >echo<, because that's what's
between your first pair of quotation marks.

> I want echo "Hello world"

   That's possible, but not by simply nesting quotation marks.  A
UNIX(-like) shell does not interpret nested quotation marks as nested.
For example:

mba$ echo fred
fred

   No surprise.

mba$ echo "fred"
fred

   That's "fred", and the shell eats the quotation marks.

mba$ echo "f"re"d"
fred

   That's "f", re, and "d", and the shell eats all the quotation marks.

   To get what you want, you could use backslashes to escape the normal
(shell) interpretation of (some) quotation marks:

mba$ echo "f\"re\"d"
f"re"d

   Or, you could use apostrophes instead of quotation marks around the
whole expression:

mba$ echo 'f"re"d'
f"re"d

   Of course, if your string includes apostrophes, then you have a
different set of escape/quotation problems to solve.

   The critical detail is recognizing what is processing whom, and how
it interprets what it gets, and what the result is.  In your case, your
interactive shell is interpreting those quotation marks before they ever
get to "myscript".  For more details, see the documentation for your
interactive shell.

   Note that on VMS, the DCL command-line interpreter uses a different
set of rules, and a kind of quotation nesting is possible there:

alp $ write sys$output "fred"
fred

alp $ write sys$output "f""re""d"
f"re"d

But (obviously) that's not a UNIX(-like) shell.

SwissKnife
Frequent Advisor

Re: getopt and parameters with space or "

Hi,

 

thanks for this answer.

Then ... what is the solution offered  ? How may I pass my parameter to be recognized as the value I want to identify with the -v key ?

 

Best regards, Den.

Steven Schweda
Honored Contributor

Re: getopt and parameters with space or "

> Then ... what is the solution offered ? [...]

[...] -v "echo \"Hello world\""
[...] -v 'echo "Hello world"'

   Apparently, that whole "Teach a man to fish" thing has limited
application.

SwissKnife
Frequent Advisor

Re: getopt and parameters with space or "

Hi,

 

Unfortunately it does not works!

 

I'm using this to parse:

 

while [ $# -gt 0 ];
do
case $1 in
-h)
HELP=YES
shift
;;
-i)
inDELPHIX_OBJECT=$2
shift 2
;;
-t)
inHOOK_TYPE=$2
shift 2
;;
-v)
inHOOK_COMMAND=$2
shift 2
;;
-a)
inHOOK_ACTION="ADD_HOOK"
shift
;;
-x)
inHOOK_ACTION="DELETE_HOOK"
shift
;;
-n)
inHOOK_POSITION=$2
shift 2
;;
--)
shift
break
;;
*)
ErreurArguments
;;
esac
done

 

Kind regards, Den.

Steven Schweda
Honored Contributor

Re: getopt and parameters with space or "

> Unfortunately it does not works!

   What, exactly, does not work?  Define/explain "not work".  As usual,
showing actual commands with their actual output can be more helpful
than vague descriptions of interpretations.

   I added a "#!/bin/sh" to the beginning of the code you posted, and
this at the end:

      echo " inHOOK_COMMAND: >${inHOOK_COMMAND}<"

   Then:

pro3$ ./pa.sh -v "echo \"Hello world\""
  inHOOK_COMMAND: >echo "Hello world"<

   That looks like what I'd expect.  Similarly:

pro3$ ./pa.sh -a -t preRefresh -v "echo \"Hello world\""
  inHOOK_COMMAND: >echo "Hello world"<

   I'll admit that your (partial) script works badly when one specifies
       -o test
on its command line, but that's pretty easy to explain.  (Especially
when I don't have an "ErreurArguments" function(?) in my script.  I also
added a "break" statement after "ErreurArguments" in my script.)

Dennis Handly
Acclaimed Contributor

Re: getopt and parameters with space or "

>I'd expect >echo <, not >echo<, because that's what's between your first pair of quotation marks.

 

That doesn't matter, it's what's after the quotation marks, if no space, you should expect:

>echo Hello<

 

> I also added a "break" statement after "ErreurArguments"

 

You would need to add a "break 2" or a shift then break, so you don't loop.