How do you make C/C++ or Java execute Linux console commands?

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Basically I want to create a string in C++ and then excecute it in Bash. Bonus points if you can also tell me how to run Windows dos commands.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Execute command.com with the string as a parameter.
For Windows, you may need to enclose the string with quotes.

Use the Windows 32 bit API:
CreateProcess


or the older API:
WinExec
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
The system() command will execute commands using /bin/sh.

For example:

#include <stdlib.h>
system("ps");

or if you need to use bash:

system( "/bin/bash -c ps" );

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Using system(3) in any program that accepts user input for the string in any fashion is a security nightmare.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
If the program run by system(3) changes it's behavior from environment variables, user config file, etc then it can possibly be subverted to do bad things. Also if you build the command passed to system(3) from any type of user input the user could craft a command string to do bad things. If the program is setuid root things can get a lot worse.

Functions like system(3), exec(3), etc should be used very sparingly.