Python fabric,为什么 grep 使用 [] 失败
Python fabric, why does grep fails using []
我想知道为什么在使用 [] 执行 grep 时出现致命错误,请在此处寻找逻辑。
工作:
In [37]: run("""ps aux | grep "grunt" """)
[worker2] run: ps aux | grep "grunt"
[worker2] out: root 21414 0.0 0.0 16476 5632 pts/0 Ss+ 03:10 0:00 /bin/bash -l -c ps aux | grep "grunt"
[worker2] out: root 21475 0.0 0.0 11752 884 pts/0 S+ 03:10 0:00 grep grunt
[worker2] out:
Out[37]: 'root 21414 0.0 0.0 16476 5632 pts/0 Ss+ 03:10 0:00 /bin/bash -l -c ps aux | grep "grunt" \r\nroot 21475 0.0 0.0 11752 884 pts/0 S+ 03:10 0:00 grep grunt'
不工作:
In [38]: run("""ps aux | grep "[g]runt" """)
[worker2] run: ps aux | grep "[g]runt"
Fatal error: run() received nonzero return code 1 while executing!
Requested: ps aux | grep "[g]runt"
Executed: /bin/bash -l -c "ps aux | grep \"[g]runt\" "
Aborting.
An exception has occurred, use %tb to see the full traceback.
SystemExit: run() received nonzero return code 1 while executing!
知道为什么第二节不起作用吗?
为了避免 grep returns 的出口 1,这样写:
grep "[g]runt" | tee
这是因为 grep 在使用方括号时没有按字面意思找到任何内容(当 g运行t 确实 运行 时,它应该可以工作,试试吧)
我想知道为什么在使用 [] 执行 grep 时出现致命错误,请在此处寻找逻辑。
工作:
In [37]: run("""ps aux | grep "grunt" """)
[worker2] run: ps aux | grep "grunt"
[worker2] out: root 21414 0.0 0.0 16476 5632 pts/0 Ss+ 03:10 0:00 /bin/bash -l -c ps aux | grep "grunt"
[worker2] out: root 21475 0.0 0.0 11752 884 pts/0 S+ 03:10 0:00 grep grunt
[worker2] out:
Out[37]: 'root 21414 0.0 0.0 16476 5632 pts/0 Ss+ 03:10 0:00 /bin/bash -l -c ps aux | grep "grunt" \r\nroot 21475 0.0 0.0 11752 884 pts/0 S+ 03:10 0:00 grep grunt'
不工作:
In [38]: run("""ps aux | grep "[g]runt" """)
[worker2] run: ps aux | grep "[g]runt"
Fatal error: run() received nonzero return code 1 while executing!
Requested: ps aux | grep "[g]runt"
Executed: /bin/bash -l -c "ps aux | grep \"[g]runt\" "
Aborting.
An exception has occurred, use %tb to see the full traceback.
SystemExit: run() received nonzero return code 1 while executing!
知道为什么第二节不起作用吗?
为了避免 grep returns 的出口 1,这样写:
grep "[g]runt" | tee
这是因为 grep 在使用方括号时没有按字面意思找到任何内容(当 g运行t 确实 运行 时,它应该可以工作,试试吧)