2016-11-15 35 views
0

我在尋找這樣的一個反面: Trick an application into thinking its stdin is interactive, not a pipe招數應用到想這是一個管道,而不是互動

我想獲得stdout中的命令的輸出,但要認爲這是寫成管道。

通常的解決方案是| cat但是我有額外的要求,這是跨平臺(即sh,不bash),並且如果命令失敗返回一個有效的退出代碼。通常我會使用pipefail,但這在任何地方都不可用。

我試過stty的各種咒語,但沒有成功。

回答

1

你總是可以使用命名管道:

mkfifo tmp.pipe 

# Reader runs in background 
cat tmp.pipe & 

# Producer in foreground 
your_command > tmp.pipe 
command_rtn=$? 

rm tmp.pipe 

或者,如果您不需要實時輸出,輸出是相當小:

output=$(your_command) 
command_rtn=$? 
echo "${output}" 

或者你也可以寫退出狀態到文件做一些可怕的事情:

{ your_command; echo $? > rtn_file; } | cat 
command_rtn=$(cat rtn_file) 
相關問題