Excuting shell commands within C program

Excuting shell commands within C program

Post by movq.. » Thu, 12 Sep 1991 13:42:00



Hi.  I have a question about runing system calls within a C program.
I understand that library function "system(string)" takes only
constant strings.  Is there an alternative function where I can
use variables as its argument?

Thanks in advance.

 
 
 

Excuting shell commands within C program

Post by neal.l.leitn » Fri, 13 Sep 1991 03:38:46



Quote:> Hi.  I have a question about runing system calls within a C program.
> I understand that library function "system(string)" takes only
> constant strings.  Is there an alternative function where I can
> use variables as its argument?

> Thanks in advance.

I am not sure if I understand you correctly, but this will work:

.
.
.
        strcpy(sys_var,"cat x");
        system(sys_var);
.
.
.

 
 
 

Excuting shell commands within C program

Post by Jonathan I. Kame » Fri, 13 Sep 1991 06:36:30


|> Hi.  I have a question about runing system calls within a C program.
|> I understand that library function "system(string)" takes only
|> constant strings.  Is there an alternative function where I can
|> use variables as its argument?

system(3) takes any string you want to pass into it, constant or otherwise.
Perhaps what you really mean to say is that system(3) only takes one string,
rather than a list of arguments.  If you want to pass in the latter, perhaps
you want to use one of the exec*(2) functions.  See the man page for execl(2)
(possibly execl(3)) on your system for more information.

--
Jonathan Kamens                               USnail:
MIT Project Athena                              11 Ashford Terrace

Office: 617-253-8085                          Home: 617-782-0710

 
 
 

Excuting shell commands within C program

Post by Doug Gw » Fri, 13 Sep 1991 12:34:10



>Hi.  I have a question about runing system calls within a C program.
>I understand that library function "system(string)" takes only
>constant strings.  Is there an alternative function where I can
>use variables as its argument?

Your question evidences some misunderstanding, but I can't figure out
what.  The system() library function uses a Bourne shell to interpret
its argument as a shell command and execute it.  It is up to you to
create the argument to system().  It need not be a string literal,
how could it be so constrained?  Or perhaps you have seen the
declaration "int system(const char*);" and misunderstood the role
played by the "const" type qualifier?  It has nothing to do with
"constant strings", whatever you may have meant by that term.