连接字符串,文件和程序输出猛砸(Concatenate strings, files and pro

2019-06-25 09:09发布

用例是,在我的情况,CSS文件连结,它就会被最小化了。 为了Concat的两个CSS文件:

cat 1.css 2.css > out.css

要添加在一个单一的位置一些文字,我可以做

cat 1.css <<SOMESTUFF 2.css > out.css
This will end in the middle.
SOMESTUFF

若要从另一个程序添加STDOUT:

sed 's/foo/bar/g' 3.css | cat 1.css - 2.css > out.css

到现在为止还挺好。 但我经常来的情况,在那里我需要几个字符串,文件,甚至是程序输出混合在一起,像版权头,由预处理文件sed(1)等。 我想在尽可能少的步骤和临时文件成为了可能一起将它们连接起来,而其选择顺序的自由。

总之,我正在寻找一种方式,在Bash中尽可能少的步骤可能这样做:

command [string|file|output]+ > concatenated
# note the plus ;-) --------^

(基本上,具有cat来处理多个STDINs就足够了,我想,像

<(echo "FOO") <(sed ...) <(echo "BAR") cat 1.css -echo1- -sed- 2.css -echo2-

但我看不到,我怎么能访问这些。)

Answer 1:

这工作:

cat 1.css <(echo "FOO") <(sed ...) 2.css <(echo "BAR")


Answer 2:

你可以在一个子shell,它会被重定向到一个文件中添加的所有命令:

(
    cat 1.css
    echo "FOO"
    sed ...
    echo BAR
    cat 2.css
) > output

您还可以附加到文件里>> 。 例如:

cat 1.css  >  output
echo "FOO" >> output
sed ...    >> output
echo "BAR" >> output 
cat 2.css  >> output

(这可能打开和关闭文件中重复)



Answer 3:

你可以做:

echo "$(command 1)" "$(command 2)" ... "$(command n)" > outputFile


文章来源: Concatenate strings, files and program output in Bash