Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using shell commands in my c program

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,
I am programming under Ubuntu, using c. I would like to use shell commands, like "ls", and print the result in the stdout. Does anyone know how I can do? Thanks
 
I found out that this code would work:
Code:
char cmd[100];
sprintf(cmd, "ls");
system(cmd);

But I would like to be able to use also sudo commands without the shell stopping for the password request. Is there a way to send the password as well together with the command?

Thanks
 
You could type "man sudo" and check for yourself.
The only thing I see that might work is the -S option.
I don't have Linux booted up now to try this myself, but try something like this:
Code:
sudo -S yourCmd < yourPassword
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top