C++/php/python Language Execution Shell commands

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.