Operating System - Linux
1748167 Members
4061 Online
108758 Solutions
New Discussion юеВ

Re: The usage of function system() in UNIX

 
SOLVED
Go to solution
CA1490051
Frequent Advisor

The usage of function system() in UNIX

Hi,

I dont kn ow this is is the right place to put this query. But, it will help me a lot if i can get the answer from any one of you.

Question is the system() builin function returns the integer value. But, i want to store the result of the command executed
ex- system("pwd"); in a string variable.
How can i achieve this?

thanks in advance
Vikram
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: The usage of function system() in UNIX

Are you talking about system() in a C
program, or in a perl script, or where?
Dennis Handly
Acclaimed Contributor
Solution

Re: The usage of function system() in UNIX

If this is a libc function, you must redirect stdout (system("pwd > file"). Or you can call popen(3). Or you should call getcwd(2)

If this is awk, you should pass PWD in its command line.
CA1490051
Frequent Advisor

Re: The usage of function system() in UNIX

Yes, I am talking about normal C- lib function.

Not any scripts.

thanks and regards
Vikram
Dennis Handly
Acclaimed Contributor

Re: The usage of function system() in UNIX

>I am talking about normal C

Then you need to look into popen(3). That provides you with:
popen() creates a pipe between the calling program and a command to be executed by the POSIX shell

Then you just do fgets on the output.
A. Clay Stephenson
Acclaimed Contributor

Re: The usage of function system() in UNIX

... and when you use popen(), you should also capture the return value of pclose() because that will return the exit status of the popen()'ed command --- equivalent to the value returned by system().
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: The usage of function system() in UNIX

Check out the URL below for stuff relevant to your post:

http://www.opengroup.org/onlinepubs/009695399/functions/popen.html
dirk dierickx
Honored Contributor

Re: The usage of function system() in UNIX

in case this C program does very trivial stuff, i highly recommend writing this is perl...
CA1490051
Frequent Advisor

Re: The usage of function system() in UNIX

I have found the right solution for this by support of all of you.

thnaks and regards
Vikram