View Full Version : suppressing quotes from command line


Csaba Gabor
09-04-2005, 05:31 PM
How can I do: echo messyExpression | myProg.exe -switches
when messyExpression needs quotes?

There are several posts that discuss stripping quotes, but those deal
with with the string already being a variable, and I am on the command
line. I want to call a program that only works interactively (that's
what the -a indicates below). Fortunately, I only need to pass it one
line of input so I can do this one liner from the Cmd Prompt on my Win
XP Pro system:

echo "<?php print('foo');exit(); ?>" | php.exe -a

Unfortunately, this prints:
"foo
instead of:
foo

Somehow, I would like to have echo not pass in those double quotes
cause that first one gets printed. I tried removing the double quotes
and instead escaped the < and >:
echo ^<?php print('foo');exit(); ?^> | php.exe -a

In this case I get the CMD prompt telling me:
The syntax of the command is incorrect.
and then php.exe does run, but it doesn't receive the input.
Similarly escaping all the other non word characters doesn't seem to
make a difference.

I hope someone will be able to set me straight.
Thanks,
Csaba Gabor from Vienna

William Allen
09-04-2005, 06:16 PM
"Csaba Gabor" wrote in message
....snip
> echo ^<?php print('foo');exit(); ?^> | php.exe -a
>
> In this case I get the CMD prompt telling me:
> The syntax of the command is incorrect.
....snip

Try double-escaping the redirection operators, thus:

echo ^^^<?php print('foo');exit(); ?^^^> | yourProg.exe

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/

Stefan Kanthak
09-04-2005, 08:20 PM
"Csaba Gabor" <Csaba@z6.com> wrote:

> How can I do: echo messyExpression | myProg.exe -switches
> when messyExpression needs quotes?
>
> There are several posts that discuss stripping quotes, but those deal
> with with the string already being a variable, and I am on the command
> line. I want to call a program that only works interactively (that's
> what the -a indicates below). Fortunately, I only need to pass it one
> line of input so I can do this one liner from the Cmd Prompt on my Win
> XP Pro system:
>
> echo "<?php print('foo');exit(); ?>" | php.exe -a
>
> Unfortunately, this prints:
> "foo
> instead of:
> foo

CMD.EXE ain't a POSIX shell, it does not eat " and '.

> Somehow, I would like to have echo not pass in those double quotes
> cause that first one gets printed. I tried removing the double quotes
> and instead escaped the < and >:
> echo ^<?php print('foo');exit(); ?^> | php.exe -a
>
> In this case I get the CMD prompt telling me:
> The syntax of the command is incorrect.

Omit the "| php.exe -a"; now you'll see that echo outputs exactly what
you want. It's the continuation with the pipe that breaks something
within CMD.EXE.

> and then php.exe does run, but it doesn't receive the input.
> Similarly escaping all the other non word characters doesn't seem to
> make a difference.
>
> I hope someone will be able to set me straight.

set TEMPFILE=%TEMP%\%RANDOM%
echo ^<?php print('foo');exit(); ?^> >%TEMPFILE%
type %TEMPFILE% | php.exe -a
del %TEMPFILE%

should do the job; not straight but it works.

BTW: do you use a native Win32 PHP.EXE or does it run under Cygwin, MSYS,
SFU or another "POSIX subsystem"? If not native: use the shell supplied
with your "POSIX subsystem".

Stefan

Csaba Gabor
09-04-2005, 08:42 PM
William Allen wrote:
> "Csaba Gabor" wrote in message
> > echo ^<?php print('foo');exit(); ?^> | php.exe -a
> >
> Try double-escaping the redirection operators, thus:
>
> echo ^^^<?php print('foo');exit(); ?^^^> | yourProg.exe

Majorly cool, thanks very much William. Had to puzzle for a while over
what's going on. In my new understanding, the parser goes along
looking for string markers (") and redirection characters (such as < >
|). It does not care about that echo (yet) at the beginning of the
string. Thus, this reduces the ^^^< to ^< and similarly we get ^>
after which the | is encountered.

NOW the parser says lets deal with the command on the left, which is
why the string that is finally fed to echo is what I had above. Nice
learning lesson.

Thanks again,
Csaba Gabor

By the way, this also solves:
http://groups.google.com/group/microsoft.public.win2000.cmdprompt.admin/browse_frm/thread/54b6ed2581856854/

William Allen
09-04-2005, 09:03 PM
"Csaba Gabor" wrote in message
> William Allen wrote:
> > "Csaba Gabor" wrote in message
> > > echo ^<?php print('foo');exit(); ?^> | php.exe -a
> > >
> > Try double-escaping the redirection operators, thus:
> >
> > echo ^^^<?php print('foo');exit(); ?^^^> | yourProg.exe
>
> Majorly cool, thanks very much William. Had to puzzle for a while over
> what's going on. In my new understanding, the parser goes along
> looking for string markers (") and redirection characters (such as < >
> |). It does not care about that echo (yet) at the beginning of the
> string. Thus, this reduces the ^^^< to ^< and similarly we get ^>
> after which the | is encountered.
>
> NOW the parser says lets deal with the command on the left, which is
> why the string that is finally fed to echo is what I had above. Nice
> learning lesson.

Thanks for confirming operation.

Your analysis is correct. When escaped commands are being
passed through several stages such as pipes or SETting into
variables, it's usually worthwhile trying one or more extra levels
of escaping if you get syntax errors. You are right to observe
that the escaping of, say ^^^>, works in pairs, with the first ^^
reducing to ^ and the following ^> reducing to > which then form
a new pair ^> for the next stage of parsing/processing.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/