Using Popen () to execute shell commands under Linux

Source: Internet
Author: User
Tags function definition

Simply say 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 child process by creating a pipe, calling fork (), and executes a shell to run the command to open a process. This pipe must be closed by the Pclose () function instead of the fclose () function. The Pclose () function closes the standard I/O stream, waits for the command execution to end, and then returns the Shell's terminating state. If the shell cannot be executed, the terminating state returned by Pclose () is the same as the shell has executed exit.

The type parameter can only be read or write, and the resulting return value (standard I/O stream) also has a read-only or write-only type corresponding to type. If type is "R" then the file pointer is connected to the standard output of command, and if type is "W" then the file pointer is connected to the standard input of command.

The command parameter is a pointer to a null-terminated shell command string. This line of command will be passed to bin/sh and use the-c flag and the shell will execute the command.

The return value of Popen () is a standard I/O stream that must be terminated by Pclose. As mentioned earlier, this flow is unidirectional (can only be used for reading or writing). Writing to this stream is equivalent to writing the standard input to the command, the standard output of the command is the same as the process of calling Popen (), and, conversely, reading the data from the stream is equivalent to reading the standard output of the command, the standard input of the command and the process of calling Popen ().

return value

If the call to fork () or pipe () fails, or if memory cannot be allocated, it returns NULL, otherwise the standard I/O stream is returned. Popen () did not set errno value for memory allocation failure. If an error occurs when the fork () or pipe () is called, errno is set to the appropriate error type. If the type parameter is not valid, errno will return einval.

Attach an example:

Execute shell command
Executes a shell command, the output is stored row by line in Resvec, and returns the number of rows int32_t myexec (const char *cmd, vector<string> &resvec) { Resvec.clear (); FILE *pp = popen (cmd, "R"); Establish Pipeline if (!pp) { return-1; }
Char tmp[1024]; Set an appropriate length to store each line of output while (fgets (TMP, sizeof (TMP), pp) = NULL) { if (Tmp[strlen (TMP)-1] = = ' \ n ') { tmp[str Len (TMP)-1] = ' + '; Remove newline character } resvec.push_back (TMP); } Pclose (PP); Close the pipe return resvec.size ();}

Using Popen () to execute shell commands under Linux

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.