How to embed a built-in (heredoc, maybe?) Python script in a stdin / stdout bash stream pipeline

I recently did a lot of work in python and would like to use its features instead of shell / bash builtins / shell scripting.

So, for the shell pipeline as follows:

echo -e "Line One\nLine Two\nLine Three" | (cat<<-HERE | python import sys print 'stdout hi' for line in sys.stdin.readlines(): print ('stdout hi on line: %s\n' %line) HERE ) | tee -a tee.out 

All that is printed is "stdout hi"

What needs to be fixed here?

thanks!

+3
source share
1 answer

It would be better if you explained what your goal is in this design. Maybe this can be simplified?

The problem with this script is that echo goes into the stdin encapsulating shell initiated by the note (...) . But inside the shell, stdin is redefined as sent to , so it reads the script from stdin, which now comes from .

So you are trying to do something like this:

 echo -e "Line One\nLine Two\nLine Three" | python <(cat <<HERE import sys print "stdout hi" for line in sys.stdin: print line.rstrip() print "stdout hi" HERE ) 

Conclusion:

 stdout hi Line One Line Two Line Three stdout hi 

Now the script is read from /dev/fd/<filehandle> , so stdin can be used by the echo pipe.

SOLUTION # 2

There is another solution. The script can be sent to stdin as here-is-document, but then the input channel should be redirected to another file descriptor. To do this, the script uses a fdopen(3) similar function. I am not familiar with , so I show a example:

 exec 10< <(echo -e "Line One\nLine Two\nLine Three") perl <<'XXX' print "stdout hi\n"; open($hin, "<&=", 10) or die; while (<$hin>) { print $_; } print "stdout hi\n"; XXX 

Here echo redirected to the file descriptor 10, which opens inside the script.

But the echo part can be removed (-1 fork ) using another heredoc :

 exec 10<<XXX Line One Line Two Line Three XXX 

MULTILINE SCIPRT

Or simply enter a multile script using the -c option:

 echo -e "Line One\nLine Two\nLine Three"|python -c 'import sys print "Stdout hi" for line in sys.stdin: print line.rstrip() print "Stdout hi"' 
+5
source

Source: https://habr.com/ru/post/1208376/


All Articles