Use popen () in Linux to execute shell commands

Source: Internet
Author: User
Document directory
  • Function Definition
  • Return Value

To put it simply, the popen () function

Function Definition
#include <stdio.h>FILE * popen(const char *command , const char *type );int pclose(FILE *stream);
Function Description

The popen () function creates a pipeline, calls fork () to generate a sub-process, and runs a shell to run the command to start a process. This pipeline must be closed by the pclose () function, rather than the fclose () function. The pclose () function closes the standard I/O Stream, waits until the command execution ends, and returns the shell termination status. If the shell cannot be executed, the termination status returned by pclose () is the same as that returned by the shell.

The type parameter can only be read or written. The returned value (standard I/O Stream) also has a read-only or write-only type corresponding to the type. If the type is "r", the file pointer is connected to the standard output of the command; if the type is "W", the file pointer is connected to the standard input of the command.

The command parameter is a pointer to a shell command string ending with null. This line of command will be passed to bin/sh and the-C flag will be used, shell will execute this command.

The return value of popen () is a standard I/O Stream and must be terminated by pclose. As mentioned above, this stream is unidirectional (only used for reading or writing ). Writing content to this stream is equivalent to writing standard input to this command. The standard output of the command is the same as the process that calls popen (). In contrast, reading data from a stream is equivalent to reading the standard output of a command. The standard input of a command is the same as the process that calls popen.

Return Value

If fork () or pipe () fails to be called, or the memory cannot be allocated, null is returned. Otherwise, the standard I/O Stream is returned. Popen () does not set errno for memory allocation failure. If an error occurs when fork () or pipe () is called, errno is set to the corresponding error type. If the type parameter is invalid, errno returns einval.

 

Attached example:

// Execute shell command
// Execute a shell command, store the output results row by row in resvec, and return the number of rows int32_t myexec (const char * cmd, vector <string> & resvec) {resvec. clear (); file * PP = popen (CMD, "R"); // create an IF (! Pp) {return-1 ;}
Char TMP [1024]; // set an appropriate length to store the output of each row while (fgets (TMP, sizeof (TMP), pp )! = NULL) {If (TMP [strlen (TMP)-1] = '\ n') {TMP [strlen (TMP)-1] =' \ 0 '; // remove linefeed} resvec. push_back (TMP);} pclose (PP); // close the pipe return resvec. size ();}
Related Article

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.