简体   繁体   中英

How to create a command line pipe? (xcode mac os x)

How to create a command line pipe? (xcode mac os x) Hello I want to create a command line with xcode (mac os x) that had the quality of being used in pipe after "|" .

i know that by using xargs we can pass arguments stored in stdin into arguments.

I would like to understand how to create a pipable command line. Thank you for your answers

In objective-c or in C simply, to give an line command the virtue to be used ien pipeline as a result of the use of "|" after another command line, it is necessary to redirect "stdin" towards the entry of the command line as it was an argument ("argv"). From there by mastering a little programming in "C", one must arrive at its ends to make a command line create with xcode, usable after "|" .

For example, if we define the hand function that should receive the arguments to execute. In a rudimentary way we will write (in C):

         int main (int argc, char ** argv) 
    {
     char buf [512] = ""; 
    char buf1[512] = "";
int f;

and achieve some things

the first argument of the argument array, contains in any case the no of the command line that you are creating ex: argv [0] "echo" and the argument following the one you wish to use, 'here for echo argv [1] "the sun shines" of course if echo is able to receive an argument after "|" (pipe) for example: echo "the sun is shining" | echo, but echo do not use the pipe.

For our main function, is elementarily we will check if argv [1] contains an argument by

     if (argv [1] == NULL)
>     {
  there we arbitrarily take the guess that argv [1] is empty. as a reminder our command line is located after a pipe "|" if our argv [1] 

is empty, it will need to find an argument to continue using our command line, for this we accept the postulate that if the command line is placed after "|" pipe is that the output of the previous command is not empty from where what will follow

>  f = open ("/ dev / stdin", O_RDONLY);
>  read (f, buf, sizeof (buf)); 
  memcpy (buf1, buf, sizeof (buf));

now we have opened the stream "stdin" which necessarily contains the output of the previous command, we use 2 buffers buf [512] and buf1 [512], because we can not fill argv [1], now that we have our arguments in a buffer, one can realize the continuation of the execution of the command, as if it were about argv [1].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM