簡體   English   中英

如何使用 os.system() 運行`> >(tee -a log)`?

[英]How to use os.system() to run `> >(tee -a log)`?

我在終端中運行以下命令。

sh -c "echo out; echo err 2>&1" >>(tee -a stdout.log) 2>>(tee -a stdout.log >&2)

output:

out  
err

os.system中使用os.system會報錯。

import os
cmd = """
sh -c "echo out; echo err 2>&1" > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)
"""
os.system(cmd)
sh: -c: line 1: syntax error near unexpected token `>'
sh: -c: line 1: `sh -c "echo out" > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)'

>(...)是特定於 bash 的語法。 制作bash -c而不是sh -c

此外,您應該將整個命令用引號引起來,因為-c需要一個參數。

cmd = """
bash -c 'echo out > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)'
"""

要像原始示例一樣測試對 stdout 和 stderr 的寫入,請嘗試使用花括號:

cmd = """
bash -c '{ echo out; echo err 2>&1; } > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)'
"""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM