Computing.Net > Forums > Programming > C++: Unix system call and variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++: Unix system call and variables

Reply to Message Icon

Name: kenp
Date: August 27, 2003 at 17:15:38 Pacific
OS: Solaris
CPU/Ram: unknown
Comment:

I'm trying to write a program that asks the user for for a couple parameters and then uses system("") to run a unix shell command. I can't seem to get these variables into the command string. For example if I ask the user for an file extention (s1) and file name (s2) I'd like to be able to do something like:
"ls *.s1 | grep s2"
I can never get the variables s1 or s2 to be replaced with their values so that the shell gets a command like:
"ls *.cpp | grep example"
What am I doing wrong?

-Ken



Sponsored Link
Ads by Google

Response Number 1
Name: jorgen
Date: August 27, 2003 at 18:08:13 Pacific
Reply:


if you get input from the command line:

char name[255];
char ext[255];
char cmd[512];
.
.
.
printf("enter program name-> ");
gets(name);
printf("enter extension-> ");
gets(ext);

sprintf(cmd,"ls %s|grep %s",ext,name);
system(cmd);
.
.
.
will do it.

If you get input from a form (as in a web page), you need to run a cgi script or executable to parse form input from stdin on the server, pass the strings to the variables as above and build the command-line as above.

Several good libs are available for that sort of thing (www.boutell.com/cgic/ ) and tons of perl scripts that handle input.

In either case, you're doing the same thing by the time you make the system call.

watch out for buffer overruns on the web (use strncpy() or the like to limit input).

jorgen


0

Response Number 2
Name: kenp
Date: August 28, 2003 at 09:43:01 Pacific
Reply:

I did just that and I get an error saying "Segmentation Fault (core dumped)" Know what that means?

-Ken


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++: Unix system call and variables

Advice wanted www.computing.net/answers/programming/advice-wanted/7047.html

Visual C++ System() pwd??? www.computing.net/answers/programming/visual-c-system-pwd/5934.html

Display a system call in C++ www.computing.net/answers/programming/display-a-system-call-in-c/10868.html