Re: Passing Variables To System()

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 06/30/04


Date: Wed, 30 Jun 2004 09:39:15 +0100


"JLK" <jonkokko@gmail.com> wrote in message
news:f9001473.0406292355.3cb4f638@posting.google.com...
> Leor Zolman <leor@bdsoft.com> wrote in message
news:<ut34e0le99nuj7cg072tcdnk0etrn2md33@4ax.com>...
> > On 29 Jun 2004 17:49:59 -0700, jonkokko@gmail.com (JLK) wrote:
> >
> > >I'm having a bit of a time with the following code. I can script this
> > >real easy in Bash but I'm trying to practice my C++:
> > >
> > >*******************************************************
> > >#include <iostream>
> > >#include <string>
> > >using namespace std;
> > >int main () {
> > > string user = "fred";
> > > string cmd;
> > > cmd = "curl -d userid=" + user + "&press=submit http://URL";
> > > cout << "command = " << cmd << endl;
> > > cout << "command = " << cmd.c_str() << endl;
> > > system(cmd.c_str());
> > >return 0;
> > >}
> > >*******************************************************
> > >
> > >If you run it you'll notice that both of the couts will produce the
> > >full line ok. However, if I try to pass either one to system() it will
> > >fail as nothing past "curl -d userid=" gets appended. How can you
> > >pass variables such as parameters to system() ??
> >
> > Depending upon what operating system you're running under (and it sounds
> > like yours is some flavor of Unix), the '&' character is going to have
> > different meanings to the command processor/shell. I happen to be
testing
> > under Windows XP and running 4NT as my command processor; the '&' is an
> > "end of command" separator just like ';' is under the Unix shells. On
your
> > machine, wouldn't '&' mean "run the previous command in the background"
?
> > And the remainder would be interpreted as setting an environment
variable
> > named 'press' to the value 'submit', with the 'http' being then seen as
the
> > start of yet another command?
> >
> > I'd reocommend stragetic placement of some backslashes, for starters...
> > -leor
>
>
>
> You are correct as I'm running linux and the & symbol is a
> 'background' command. However, that is only true if the character
> stands alone at the end of a command (such as: script.sh &) If it gets
> correctly appended and runs as "curl -d userid=user&press=submit
> http://URL" then it would be ok. It appears my variable insert is not
> getting appended along with the other half of the script. The weird
> part is that if I hard code USER I can run everything OK. I'm only
> bombing out when I try to insert it as a variable.

No, something else is going on. Your cmd string is correct, as proved by the
previous to cout << statements. By the time your program gets to the system
call, it is completely irrelevant whether the cmd string was composed of
variables or not, its just a string.

What you are saying is that you get different results with two identical
strings depending on whether that string was original composed from a
variable or not. Frankly, that is impossible, something else is going wrong.

john



Relevant Pages

  • Re: New "base document" available
    ... How to go about retrieving command line parameters? ... the command line arguments (and environment variables) ... While an environment pointer is defined ... clp-name points to a string containing "P1" ...
    (comp.lang.cobol)
  • Re: New "base document" available
    ... implications of any approach to command line parameters. ... exactly as it is now") for environment variables is ... While an environment pointer is defined ... clp-name points to a string containing "P1" ...
    (comp.lang.cobol)
  • TIP #185: Null Handling
    ... nulls, and command modifications for manipulating them. ... Tcl deals with strings, the universal medium for representing data. ... is know and it is an empty string, but if a respondent forgets to give ...
    (comp.lang.tcl)
  • Re: Determine how Word was launched
    ... inspect the command line used to launch Word. ... Private Declare Function GetCommandLineA Lib "kernel32" As Long ... Public Function GetCommandLineAs String ... Dim lngCmdLinePtr As Long ...
    (microsoft.public.office.developer.vba)
  • Re: Class.getMethod in classs static initializer block
    ... I just wanted to add that I had some similar code for a "command line server" project. ... static private final boolean DEBUG = true; ... {public ReturnCode runCommand(String s) ... public ReturnCode parseLine(String line) ...
    (comp.lang.java.programmer)