简体   繁体   中英

How to do command substitution, e.g. “echo hi | tee >( gzip > /tmp/1 ) > /tmp/2” in plumbum?

I need to figure out how to call pipe substitution in plumbum. Specifically, how to construct chains such as echo hi | tee >( gzip > /tmp/1 ) > /tmp/2 echo hi | tee >( gzip > /tmp/1 ) > /tmp/2 ? Or, to illustrate the illustrate the idea better, find / | tee >( grep hi > /tmp/grepped ) > /tmp/nongrepped find / | tee >( grep hi > /tmp/grepped ) > /tmp/nongrepped ? I need this kind of approach because the equivalent of find / that I'm going to use is very expensive to run and I can't save it on disk, so I need to apply two different filters in parallel. Is there a way to avoid mkfifo ?

Note: I'm aware that the question is similar to " How to pipe many bash commands from python? ". The difference, though, is that I'm asking specifically about plumbum and an attempt to write a plumbum-related answer there is flawed, as described in its comments.

To replicate the command in the title of the question ( echo hi | tee >( gzip > /tmp/1 ) > /tmp/2 ), you can do the following with plumbum

from plumbum.cmd import echo, tee, gzip

(echo["hi"] | tee["/tmp/2"] | gzip > "/tmp/1")()

This pipes the string "hi" to the tee function which writes it to "/tmp/2" and also copies it to stdout. Then, stdout is piped to gzip whose output is redirected to "/tmp/1".

To achieve something similar with your other command ( find / | tee >( grep hi > /tmp/grepped ) > /tmp/nongrepped )

you can do:

from plumbum.cmd import find, grep, tee

(find["/"] | tee["/tmp/nongrepped"] | grep["hi"] > "/tmp/grepped")()

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