简体   繁体   English

Bash 单行保存到文件然后使用内容

[英]Bash one-liner to save to file then use content

I'm trying to do something like this:我正在尝试做这样的事情:

  1. save command1 output to file将 command1 output 保存到文件
  2. feed same output to command2将相同的 output 输入到 command2

something like this像这样的东西

   command2 $(command1 > file.txt)

but that didn't work.但这没有用。

Use tee .使用tee This copies its standard input to a file and also its standard output.这会将其标准输入以及其标准 output 复制到文件中。 Then pipe the output to the second command.然后 pipe output 到第二条命令。

command1 | tee file.txt | command2

Your question used你的问题使用

command2 $(command1 > file.txt)

tee pipes are great, but if I read this right, command2 expects a filename? tee管很棒,但如果我没看错的话, command2需要一个文件名吗? If so, maybe you can use如果是这样,也许你可以使用

command2 <( command1 )

If you also need the file, then tee inside the construct -如果您需要该文件,请在结构内tee -

command2 <( command1 | tee file.txt )

If for some strange reason it absolutely requires a file NAME (can't really imagine why), then you could always try this as a last resort -如果由于某种奇怪的原因它绝对需要一个文件名(无法真正想象为什么),那么你总是可以尝试这个作为最后的手段 -

command2 "$( command1 > file.txt && echo file.txt )"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM