By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

Passing Variables To System()

P: n/a
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() ??
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 29 Jun 2004 17:49:59 -0700, jo******@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
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

P: n/a
JLK
Leor Zolman <le**@bdsoft.com> wrote in message news:<ut********************************@4ax.com>. ..
On 29 Jun 2004 17:49:59 -0700, jo******@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.
Jul 22 '05 #3

P: n/a

"JLK" <jo******@gmail.com> wrote in message
news:f9**************************@posting.google.c om...
Leor Zolman <le**@bdsoft.com> wrote in message

news:<ut********************************@4ax.com>. ..
On 29 Jun 2004 17:49:59 -0700, jo******@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
Jul 22 '05 #4

P: n/a
JLK
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2k************@uni-berlin.de>...
"JLK" <jo******@gmail.com> wrote in message
news:f9**************************@posting.google.c om...
Leor Zolman <le**@bdsoft.com> wrote in message

news:<ut********************************@4ax.com>. ..
On 29 Jun 2004 17:49:59 -0700, jo******@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


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.
Jul 22 '05 #5

P: n/a
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.
Jul 22 '05 #6

P: n/a
On Wed, 30 Jun 2004 20:52:28 +0000, red floyd wrote:
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>
--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.