[英]Bash - pipe to variable and file
在下面的簡化示例中,“ anything”從$ S“變量正確地回顯到” S.gz“文件中,但是,該變量從管道流中丟失了其值:
echo 'anything' | tee >(read S | gzip >S.gz)
zcat S.gz
echo '$S='"$S"
它呼應:
anything
$S=
預期的輸出是:
anything
$S=anything
換句話說,不幸的輸出是:
echo 'anything' | tee >(read S) | gzip >S.gz
zcat S.gz
echo '$S='"$S"
它呼應:
anything
$S=
有任何想法嗎?
read
必須在當前shell中執行; 您需要反轉管道。
read S < <(echo anything | tee >(gzip - > S.gz))
或者,在bash
4.2或更高版本中,使用lastpipe
選項。 (請注意,作業控制必須無效,以使lastpipe
生效。在非交互式shell中,默認情況下該功能處於關閉狀態,而在set +m
的交互式shell中,該功能可以處於關閉狀態。)
shopt -s lastpipe
echo anything | tee >(gzip - > S.gz) | read S
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.