C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing Variables To System()

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
JLK
Guest





PostPosted: Wed Jun 30, 2004 12:49 am    Post subject: Passing Variables To System() Reply with quote



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() ??
Back to top
Leor Zolman
Guest





PostPosted: Wed Jun 30, 2004 1:00 am    Post subject: Re: Passing Variables To System() Reply with quote



On 29 Jun 2004 17:49:59 -0700, [email]jonkokko (AT) gmail (DOT) com[/email] (JLK) wrote:

Quote:
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 #include 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

Back to top
JLK
Guest





PostPosted: Wed Jun 30, 2004 7:55 am    Post subject: Re: Passing Variables To System() Reply with quote



Leor Zolman <leor (AT) bdsoft (DOT) com> wrote

Quote:
On 29 Jun 2004 17:49:59 -0700, [email]jonkokko (AT) gmail (DOT) com[/email] (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 #include 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.

Back to top
John Harrison
Guest





PostPosted: Wed Jun 30, 2004 8:39 am    Post subject: Re: Passing Variables To System() Reply with quote


"JLK" <jonkokko (AT) gmail (DOT) com> wrote

Quote:
Leor Zolman <leor (AT) bdsoft (DOT) com> wrote

On 29 Jun 2004 17:49:59 -0700, [email]jonkokko (AT) gmail (DOT) com[/email] (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 #include 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



Back to top
JLK
Guest





PostPosted: Wed Jun 30, 2004 5:34 pm    Post subject: Re: Passing Variables To System() Reply with quote

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:
"JLK" <jonkokko (AT) gmail (DOT) com> wrote in message
news:f9001473.0406292355.3cb4f638 (AT) posting (DOT) google.com...
Leor Zolman <leor (AT) bdsoft (DOT) com> wrote in message
news:<ut34e0le99nuj7cg072tcdnk0etrn2md33 (AT) 4ax (DOT) com>...
On 29 Jun 2004 17:49:59 -0700, [email]jonkokko (AT) gmail (DOT) com[/email] (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 #include 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.

Back to top
red floyd
Guest





PostPosted: Wed Jun 30, 2004 8:52 pm    Post subject: Re: Passing Variables To System() Reply with quote

JLK wrote:
[redacted]
Quote:

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.

Back to top
Owen Jacobson
Guest





PostPosted: Thu Jul 01, 2004 2:49 am    Post subject: Re: Passing Variables To System() Reply with quote

On Wed, 30 Jun 2004 20:52:28 +0000, red floyd wrote:

Quote:
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.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.