suppressing quotes from command line

  • Thread starter Thread starter Csaba Gabor
  • Start date Start date
C

Csaba Gabor

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
 
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
 
Csaba Gabor said:
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
 
William said:
in message
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/micr...mpt.admin/browse_frm/thread/54b6ed2581856854/
 
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.
 
Back
Top