In programming, it is often necessary to use shell commands in the program to simplify the program, which is documented here.
1. C + + Execute shell command
1 #include <iostream>2 #include <String>3 #include <stdio.h>45int Exec_cmd (std::String cmd, std::String &RES) {6if (cmd.size () = =0) {//CMD is empty7Return-1;8}910Char buffer[1024] = {0};STD::string result ="";FILE *pin = Popen (Cmd.c_str (),"R");13if (!pin) {//Popen failed14Return-1;15}1617Res.clear ();18while (!Feof (PIN)) {19if (fgets (buffer,sizeof (buffer), pin)! =NULL) {Result + =Buffer21st}22}23res =Result25return Pclose (PIN);//-1:pclose failed; else SHELL ret26}2728IntMain () {STD::string cmd ="ls-ial"; std::string res; std::cout << "ret = " << exec_cmd (cmd, res) << Std::endl; Std::cout << res << Std::endl; return 0; (+}
2. PHP Execution Shell command
1 <? PHP2 $cmd = "wc-l./test.php"; EXEC ($code); $code. " \ n "; Print_r ($output); 7?>
3. Python executes shell commands
Import Commands3 status, Output = Commands.getstatusoutput ('ls-lt')print status Print output
From:http://www.cnblogs.com/xudong-bupt/p/6218140.html
C++/php/python Language Execution Shell commands