How do I dynamically pipe the commands using the string buffer?
#include<stdio.h>
#include<stdlib.h>
main()
{
int i;
char commandBuffer[3][10]={"ls -l","ll","top"};
for(i=0 ; i<1 ; i++)
{
system("> gksudo cd /home/phoenix | command[i]");
system("\n");
printf("%d\n",i);
}
printf("The end\n");
}
I have a program in which i want to dynamically run the c开发者_StackOverflowommands using system()
but the problem arising here is that the string contained in command[i]
is not being considered as a the input for piping... but this works fine if I manually enter each of the commands such as system("> gksudo cd /home/phoenix | ls -l");
system("> gksudo cd /home/phoenix | command[i]");
Here the "command[i]" is considered as a string literal. So the value of command[i]
is not substituted. you need something like:
char cmd_buff[MAX_BUF];
strcpy (cmd_buff, "> gksudo cd /home/phoenix |");
strcat (cmd_buff, command[i]);
Note here that the command[i]
is not inside double quotes. Please read about string constants and identifiers in C to understand this.
精彩评论