在xonsh
shell 中,如何从管道接收到 python 表达式?find
使用命令作为管道提供者的示例:
find $WORKON_HOME -name pyvenv.cfg -print | for p in <stdin>: $(ls -dl @(p))
for p in <stdin>:
显然是伪代码。我必须用什么来代替它?
注意:在 bash 中,我会使用这样的构造:
... | while read p; do ... done
将输入通过管道传输到 Python 表达式的最简单方法是使用可调用 alias的函数,该函数恰好接受 stdin 类文件对象。例如,
def func(args, stdin=None):
for line in stdin:
ls -dl @(line.strip())
find $WORKON_HOME -name pyvenv.cfg -print | @(func)
当然你可以跳过@(func)
func aliases
,
aliases['myls'] = func
find $WORKON_HOME -name pyvenv.cfg -print | myls
或者,如果您只想遍历 的输出find
,您甚至不需要管道。
for line in !(find $WORKON_HOME -name pyvenv.cfg -print):
ls -dl @(line.strip())