473,668 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing Variables To System()

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=sub mit http://URL";
cout << "command = " << cmd << endl;
cout << "command = " << cmd.c_str() << endl;
system(cmd.c_st r());
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
6 3287
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=sub mit http://URL";
cout << "command = " << cmd << endl;
cout << "command = " << cmd.c_str() << endl;
system(cmd.c_st r());
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
JLK
Leor Zolman <le**@bdsoft.co m> 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=sub mit http://URL";
cout << "command = " << cmd << endl;
cout << "command = " << cmd.c_str() << endl;
system(cmd.c_st r());
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&pre ss=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

"JLK" <jo******@gmail .com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
Leor Zolman <le**@bdsoft.co m> 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=sub mit http://URL";
cout << "command = " << cmd << endl;
cout << "command = " << cmd.c_str() << endl;
system(cmd.c_st r());
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&pre ss=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
JLK
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<2k******* *****@uni-berlin.de>...
"JLK" <jo******@gmail .com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
Leor Zolman <le**@bdsoft.co m> 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=sub mit http://URL";
> cout << "command = " << cmd << endl;
> cout << "command = " << cmd.c_str() << endl;
> system(cmd.c_st r());
>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&pre ss=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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14929
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
11
3473
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code and zipid are both passed to the lookup form/dialog by reference. I...
8
4408
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code is passed to the lookup form/dialog by reference. I then load a...
6
3248
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
1
6888
by: Eric | last post by:
Hello, I am trying to come up with the best way to pass large amounts of data from page to page, namely a data table. The user needs to enter data into a form in one page and confirm it on another. Session variables seem like the easiest, but I'm afraid of crashing the server after deploying with a few hundred users as opposed to just a few developers/testers. Any ideas?
8
2104
by: JJ | last post by:
Hi, What's the preferred way to pass variables around to different pages now? Or if my reading servers me right they are retained in memory for the life of the app, correct? How do I access these variables if in a different page than the one variable was created in? Thanks, JJ
3
1917
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford www.IntraRELY.com
12
2678
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
2
4430
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8371
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2782
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.