passing a string with quotes as parameter

  • Thread starter Thread starter Michael Moser
  • Start date Start date
M

Michael Moser

Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
double-quotes to a program?

I am writing a batch-file and I need to call one program with a
parameter that contains a '"' (a douple quote). I am going circles
trying to persuade CMD.EXE to swallow this.

Michael :-(
 
Ah - and I forgot: it also contains blanks, which why I have to quote it
in the first place, i.e. I need something like:

prgmname 'argument with "quotes" and blanks'

But - alas - using ' or ` as quotes does not work. I faintly remember,
that one could escape a " within a quoted string using "" or ^" , but
none of these appear to work, either...

Michael
 
On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:

Tell us more about this program.
 
foxidrive said:
On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:

Tell us more about this program.

A good point. Perhaps it is not CMD that is having difficulty swallowing
this.

I believe that cmd.exe will pass whatever is given on the commandline. The
following demonstrates this on Windows XP:

@echo off

call:sub param
call:sub "param"
call:sub "par"ram"
call:sub "par""ram"
goto:eof

:sub
echo/[%1]
goto:eof

The values displayed in the brackets by the echo command are all exactly
character for character what appears in the respective call:sub lines.

/Al
 
The program gets verbatim what is on the command line. It is code in the program that breaks parameters up. Different languages and environment do it differently. EG In JS (but not WSH) one need to manually break it up. Ditto for VB (except VB tremoves the program part of the command line leaving only the parameters). In WSH parameters are nicely in an array for you.
 
It tries to make sense of the command line but if not passes it verbatim to CreateProcess. This is why cmd can execute programs renamed to text files as CP doesn't care about extensions (except to add .exe to files without an extension) but opens the file to see how to execute (as there are many types of executable files).

lpCommandLine
[in, out] Pointer to a null-terminated string that specifies the command line to execute.
Windows NT/2000/XP: The Unicode version of this function, CreateProcessW, will fail if this parameter is a const string.

The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.

If both lpApplicationName and lpCommandLine are non-NULL, *lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. C runtime processes can use the argc and argv arguments. Note that it is a common practice to repeat the module name as the first token in the command line.

If lpApplicationName is NULL, the first white-space – delimited token of the command line specifies the module name. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin (see the explanation for the lpApplicationName parameter). If the file name does not contain an extension, .exe is appended. Therefore, if the file name extension is ..com, this parameter must include the .com extension. If the file name ends in a period (.) with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file in the following sequence:

1.. The directory from which the application loaded.
2.. The current directory for the parent process.
3.. Windows 95/98/Me: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.

4.. Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
5.. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6.. The directories that are listed in the PATH environment variable.
The system adds a null character to the command line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.


--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
Al Dunbar said:
foxidrive said:
On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:

Tell us more about this program.

A good point. Perhaps it is not CMD that is having difficulty swallowing
this.

I believe that cmd.exe will pass whatever is given on the commandline. The
following demonstrates this on Windows XP:

@echo off

call:sub param
call:sub "param"
call:sub "par"ram"
call:sub "par""ram"
goto:eof

:sub
echo/[%1]
goto:eof

The values displayed in the brackets by the echo command are all exactly
character for character what appears in the respective call:sub lines.

/Al
 
No secret: it's SED (the stream editor - part of the cygwin utils
(http://www.cygwin.com). I am trying to add a argument to an element in
an XML file:

I.e. I want to change a an element <toc> to <toc href="some_url_here">
so I was trying to specify:
sed "s/<toc>/<toc href="some_url_here">/"

It's the quotes that need to surround the URL that cause the trouble.
And since there is a space after "toc" in the second string this needs
to be quoted (otherwise I would only need to escape the redirection
symbols "<" and ">").

Michael
 
David Candy said:
The program gets verbatim what is on the command line. It is code in the
program that breaks parameters up. Different languages and environment do
it differently. EG In JS (but not WSH) one need to manually break it up.
Ditto for VB (except VB tremoves the program part of the command line
leaving only the parameters). In WSH parameters are nicely in an array
for you.


What you posted above is basically true, but there is what I consider to be a
bug in CommandLineToArgvW. This API function will remove the surrounding double
quotes. A while back I wrote a quick C++ executable to demonstrate this:


http://www.optimumx.com/download/CommandLineTest.exe


C:\>CommandLineTest.exe "123" "4 5 6"


Display the entire command line using GetCommandLineA():

CommandLineTest.exe "123" "4 5 6"


Display the entire command line using GetCommandLineW():

CommandLineTest.exe "123" "4 5 6"


Display each parameter in the command line using argc/argv from main():

0=[CommandLineTest.exe]
1=[123]
2=[4 5 6]


Display each parameter in the command line using CommandLineToArgvW():

0=[CommandLineTest.exe]
1=[123]
2=[4 5 6]
 
Michael Moser said:
No secret: it's SED (the stream editor - part of the cygwin utils
(http://www.cygwin.com). I am trying to add a argument to an element in an
XML file:

I.e. I want to change a an element <toc> to <toc href="some_url_here"> so
I was trying to specify:
sed "s/<toc>/<toc href="some_url_here">/"

It's the quotes that need to surround the URL that cause the trouble. And
since there is a space after "toc" in the second string this needs to be
quoted (otherwise I would only need to escape the redirection symbols "<"
and ">").

Michael

Hmmm... I use HHSED, and I don't claim to be an expert on all the world's
SED versions.

I'd use

SED s/\x3ctoc\x3e/\x3ctoc href=\x22some_url_here\x22\x3c/

myself. Or possibly i'd put the
s/<toc>/<toc href="some_url_here">/
into a file and use SED -f filename

Perhaps Eric Pement's www.unixguide.net/unix/sedoneliner.shtml or
www.cornerstonemag.com/sed might be of aid in your quest...

HTH

....Bill
 
You need to escape the quotes with a slash like so

"param1" "param2 \"some subparam of param 2\""
 
Bingo! You are my hero! Thanks a lot!

Michael

Joe Richards said:
You need to escape the quotes with a slash like so

"param1" "param2 \"some subparam of param 2\""
 
Back
Top