Operating System - Linux
1753365 Members
5359 Online
108792 Solutions
New Discussion юеВ

Re: Can we execute more than one command with popen() ?

 
Masthan
Advisor

Can we execute more than one command with popen() ?

Hi All,
Can we execute more than one command by using popen() call?

I am gettign an error message When I try to execute the following command

popen("cd /sys/class/scsi_host/host0/device/../driver; pwd", "r").

I am seeing the following error message:

cd: /sys/class/scsi_host/host0/device/../driver: No such file or directory

actually the above command is succesfully executed @ shell prompt but when i try to execute the same command in .c file i am getting the above error.

Can any one debug this?

Your help is appreciated.

Regards
Masthan D
5 REPLIES 5
Mark Grant
Honored Contributor

Re: Can we execute more than one command with popen() ?

Any particular reason you don't just chdir() before your popen()?

However, the error you are getting suggests to me that the shell can't find your directory, not that popen() is failing.

popen() simply passes the string to "/bin/sh" and connects it's standard input and output accordingly. If the shell returns what you want then you should be able to read it through popen().

I just tried your "cd ...;pwd" string and it works fine.

Regards
Never preceed any demonstration with anything more predictive than "watch this"
Masthan
Advisor

Re: Can we execute more than one command with popen() ?

Hi Mark,

Thanks for quick reply.

Can you try the following command on your linux machine?

popen("cd /sys/class/scsi_host/host0/device/../driver; pwd", "r");

FYI: I am using SLES 10 machine.

Regards
Masthan
Mark Grant
Honored Contributor

Re: Can we execute more than one command with popen() ?

Cool, I get the same error as you when I use that directory.

I'll have a little play now!
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: Can we execute more than one command with popen() ?

You'll notice this isn't a "popen" problem.

See the difference in these two commands.

sh -c "cd /tmp"

and

sh -c "cd /sys/class/scsi_host/host0/device/../driver"

It should be remembered that /sys isn't a real filesystem as such as is merely an export from the kernel. This almost certainly has something to do with it but it certainly requires further investigation.
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: Can we execute more than one command with popen() ?

Sorry to start spamming here!

Actually I think it's a combination of symbolic links and the sysfs filesystem. If you chdir() before popen() it works but you find yourself in the target directory not the symbolic link as such.

IS there another way you can get what you are trying to achieve?
Never preceed any demonstration with anything more predictive than "watch this"