Disable interpreting/stripping of quotes for command-line arguments?

Disable interpreting/stripping of quotes for command-line arguments?

Post by Joel Cor » Fri, 07 Feb 2003 06:53:14



I am trying to write a Unix Shell script that will act as a wrapper
for a binary. It needs to do x, y, then call the binary with the EXACT
arguments that the script was executed with. The format of the
arguments cannot be changed (i.e.- escaping characters) because this
is hard-coded into many scripts, and it would be very time-consuming
to edit every script and add any escaping. The program is as follows:

#! /bin/sh

# do x
# ...

# do y
# ...

# load the binary and pass all arguments
/path/to/binary/my.binary $*

The wrapper is called like this:
/path/to/my.script --set-defaults=/some/path/my.conf -e'this text is
executed in the binary'

but what is actually getting executed (passed to the binary) is:
/path/to/my.script --set-defaults=/some/path/my.conf -ethis text is
executed in the binary

The shell is stripping the quotes from the argument before the script
can do any processing on it, such as doing a pattern match and
substitution to insert escape characters. Is there a way to disable
interpretation of arguments? I tried using the noglob option:
#! /bin/sh -f
but still had the same results. Any help would be greatly appreciated,
I've been trying to find a way to do this all day.
(The system is SunOS version 8)

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Ian Springe » Fri, 07 Feb 2003 09:15:12


Quote:> I am trying to write a Unix Shell script that will act as a wrapper
> for a binary. It needs to do x, y, then call the binary with the EXACT
> arguments that the script was executed with. The format of the
> arguments cannot be changed (i.e.- escaping characters) because this
> is hard-coded into many scripts, and it would be very time-consuming
> to edit every script and add any escaping. The program is as follows:

> #! /bin/sh

> # do x
> # ...

> # do y
> # ...

> # load the binary and pass all arguments
> /path/to/binary/my.binary $*

> The wrapper is called like this:
> /path/to/my.script --set-defaults=/some/path/my.conf -e'this text is
> executed in the binary'

> but what is actually getting executed (passed to the binary) is:
> /path/to/my.script --set-defaults=/some/path/my.conf -ethis text is
> executed in the binary

> The shell is stripping the quotes from the argument before the script
> can do any processing on it, such as doing a pattern match and
> substitution to insert escape characters. Is there a way to disable
> interpretation of arguments? I tried using the noglob option:
> #! /bin/sh -f
> but still had the same results. Any help would be greatly appreciated,
> I've been trying to find a way to do this all day.
> (The system is SunOS version 8)



 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Barry Margoli » Fri, 07 Feb 2003 10:23:47



Only the latter is correct.  "$*" will put all the original arguments into
a single parameter.

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Joel Cor » Sat, 08 Feb 2003 01:56:27






> Only the latter is correct.  "$*" will put all the original arguments into
> a single parameter.

I tried it with:
# load the binary and pass all arguments
/path/to/binary/my.binary "$*"

and
"/path/to/binary/my.binary $*"

but in both cases the same result, it stripped the single quotes:
-e'statement' becomes -estatement
It's doing this straight from the command line, before the script handles it.

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Barry Margoli » Sat, 08 Feb 2003 02:02:51









>> Only the latter is correct.  "$*" will put all the original arguments into
>> a single parameter.

>I tried it with:
># load the binary and pass all arguments
>/path/to/binary/my.binary "$*"

>and
>"/path/to/binary/my.binary $*"

>but in both cases the same result, it stripped the single quotes:
>-e'statement' becomes -estatement
>It's doing this straight from the command line, before the script handles it.


Note that the binary will never actually see the quotes themselves.  What's
important is that the argument continues to be a single token.

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Tim Cargi » Sat, 08 Feb 2003 06:17:42



> I am trying to write a Unix Shell script that will act as a wrapper
> for a binary.

[ ... ]

Quote:> The wrapper is called like this:
> /path/to/my.script --set-defaults=/some/path/my.conf -e'this text is
> executed in the binary'

> but what is actually getting executed (passed to the binary) is:
> /path/to/my.script --set-defaults=/some/path/my.conf -ethis text is
> executed in the binary

[ ... ]

Quote:

> The shell is stripping the quotes from the argument before the script
> can do any processing on it, ...


you could have used this to 're-package' the arguments into
a var with quotes, then an eval my.binary $var.  I does
shift off your arguments so ... a caveat :

unset var

while [ $# -gt 0 ];do
   if [ -z "$var" ];then
       var="\"$1\""
   else
       var="$var \"$1\""
   fi
   shift
done

eval my.binary $var

HTKH

Tim

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Joel Cor » Sun, 09 Feb 2003 00:01:22










> >> Only the latter is correct.  "$*" will put all the original arguments into
> >> a single parameter.

> >I tried it with:
> ># load the binary and pass all arguments
> >/path/to/binary/my.binary "$*"

> >and
> >"/path/to/binary/my.binary $*"

> >but in both cases the same result, it stripped the single quotes:
> >-e'statement' becomes -estatement
> >It's doing this straight from the command line, before the script handles it.


> Note that the binary will never actually see the quotes themselves.  What's
> important is that the argument continues to be a single token.


was passing all the arguments as a single argument, not that the
quotes were being stripped. Thanks for the help!
 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Barry Margoli » Sun, 09 Feb 2003 02:28:07





>was passing all the arguments as a single argument, not that the
>quotes were being stripped. Thanks for the help!


Furthermore, if any of them contain whitespace, they'll be expanded into
multiple arguments.

"$*" should pass all the original arguments as a single argument.


won't expand any of them into multiple arguments.

Demonstration:

script1:

#!/bin/sh
echo 'Sending $*'
./script2 $*
echo 'Sending "$*"'
./script2 "$*"


script2:
#!/bin/sh
echo "Number of arguments: $#"
while [ $# -gt 0 ]
do
  echo "|$1|"
  shift
done
echo ''

tools:~#145% ./script1 arg1 "arg2 arg3" arg4
Sending $*
Number of arguments: 4
|arg1|
|arg2|
|arg3|
|arg4|

Sending "$*"
Number of arguments: 1
|arg1 arg2 arg3 arg4|


Number of arguments: 3
|arg1|
|arg2 arg3|
|arg4|

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Kevin Colli » Sun, 09 Feb 2003 03:47:18




> > I am trying to write a Unix Shell script that will act as a wrapper
> > for a binary.

> [ ... ]

> > The wrapper is called like this:
> > /path/to/my.script --set-defaults=/some/path/my.conf -e'this text is
> > executed in the binary'

> > but what is actually getting executed (passed to the binary) is:
> > /path/to/my.script --set-defaults=/some/path/my.conf -ethis text is
> > executed in the binary

> [ ... ]

> > The shell is stripping the quotes from the argument before the script
> > can do any processing on it, ...


> you could have used this to 're-package' the arguments into
> a var with quotes, then an eval my.binary $var.  I does
> shift off your arguments so ... a caveat :

> unset var

> while [ $# -gt 0 ];do
>    if [ -z "$var" ];then
>        var="\"$1\""
>    else
>        var="$var \"$1\""
>    fi
>    shift
> done

> eval my.binary $var

Tim,

    that is not as easy as it sounds. What happens when the wrapper is
called like this?

/path/to/my.script --set-defaults=/some/path/my.conf -e 'contains "
chars'

You'll end up with "extra" double-quotes and that would be a bad
thing. Same basic argument for using single-quotes. Barry's answer is
the right solution.

Kevin

 
 
 

Disable interpreting/stripping of quotes for command-line arguments?

Post by Tim Cargi » Sun, 09 Feb 2003 10:55:33





> > > I am trying to write a Unix Shell script that will act as a wrapper
> > > for a binary.

> > [ ... ]

> > > The wrapper is called like this:
> > > /path/to/my.script --set-defaults=/some/path/my.conf -e'this text is
> > > executed in the binary'

[ ... ]

Quote:> Tim,

>     that is not as easy as it sounds. What happens when the wrapper is
> called like this?

> /path/to/my.script --set-defaults=/some/path/my.conf -e 'contains "
> chars'

All bets are off - it's a different problem!
Change the problem params and any solution can be broken, no?

Although that could have been my second caveat.

Quote:

> You'll end up with "extra" double-quotes and that would be a bad
> thing.
> Same basic argument for using single-quotes.

Was tested with single quotes and worked fine.

Quote:> Barry's answer is the right solution.

Uh, oh.  There's that 'right solution' term again ...

I said 'Barry nailed it', didn't I?

Quote:

> Kevin

Appreciate the feedback,

Tim

 
 
 

1. Quoting Last Command Line Argument

Suppose I have a script that passes all its arguments on to another
program (say, cat). I'd like to have it enclose the last argument in
quotes, so that if there's three arguments on the command line, it will
send:

cat $1 $2 "$3"

If there's four:

cat $1 $2 $3 "$4"

etc. I'm not really sure how to do this if I don't know the number of
arguments beforehand. I was playing around with using a while loop to
"build" the command line as it goes, but quickly was bogged down in
confusion. Am I on the right track? If anyone can help me past this, I
would be grateful.

-------------------------------------------------------------------------------
Kim Scarborough                                      http://www.unknown.nu/kim/
-------------------------------------------------------------------------------
"Trying is the first step towards failure."
                                                                - Homer Simpson
-------------------------------------------------------------------------------

2. psnarf: promiscuous tcp session logger

3. Reading quoted command line arguments

4. Two spools to one printer = morphing reports!

5. How to obtain the EXACT command line arguments including quotes?

6. X drivers for Trident AGP

7. Passing command-line arguments

8. Wireless Ethernet on portable

9. hiding command-line arguments

10. How can I pass a command line argument to an aliased command?

11. How to process individual command-line arguments

12. Limitation on size of command-line arguments

13. command-line arguments in bash