Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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

![]() |
![]() |
![]() |

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