Passing Variables To System()

J

JLK

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() ??
 
L

Leor Zolman

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
 
J

JLK

Leor Zolman said:
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.
 
J

John Harrison

JLK said:
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.[/QUOTE]

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
 
J

JLK

John Harrison said:
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

Well, I'm definately open to suggestions. I'm using c++ on a linux box
to compile with. I've posted to a couple of other forums as well and
am still coming up blank.
 
R

red floyd

JLK wrote:
[redacted]
Well, I'm definately open to suggestions. I'm using c++ on a linux box
to compile with. I've posted to a couple of other forums as well and
am still coming up blank.

It *IS* the ampersand. On *nix, system("cmd") invokes $SHELL -c "cmd".
The ampersand is interpreted by the shell as the background operator.
You need to quote the ampersand either with a backslash, or putting it
inside single quotes.
 
O

Owen Jacobson

JLK wrote:
[redacted]
Well, I'm definately open to suggestions. I'm using c++ on a linux box
to compile with. I've posted to a couple of other forums as well and
am still coming up blank.

It *IS* the ampersand. On *nix, system("cmd") invokes $SHELL -c "cmd".
The ampersand is interpreted by the shell as the background operator.
You need to quote the ampersand either with a backslash, or putting it
inside single quotes.

<off-topic bits>

And, in case you're firmly convinced that the & is only relevant at the
end of a command, consider:

[owen@eidolon owen]$ ls | egrep -i '^s' & echo LAST COMMAND IN PARALLEL;
[1] 6242
LAST COMMAND IN PARALLEL
[owen@eidolon owen]$ Sentret.gif
sigbeast.c
sigfile-bits.txt
SOBAKASU.MP3
spinbottle
Splashdown

[1]+ Done ls --color=tty | egrep -i '^s'

[GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)]

</otb>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top