Bash:如何将每个命令的输出重定向到文件?

six*_*ude 5 bash alias

我经常像这样在我的源代码存储库上运行搜索:

ack -l "foo bar" .
Run Code Online (Sandbox Code Playgroud)

搜索可能需要 10-60 秒并产生如下输出:

path/to/file1       
path/to/file2
path/to/file3
Run Code Online (Sandbox Code Playgroud)

我经常使用这个别名来打开在 vim 中找到的所有文件:

alias go='gvim `fc -s` -p'
Run Code Online (Sandbox Code Playgroud)

这工作正常,但fc -s重新运行前一个命令,这可能需要几秒钟或几分钟。


将这样的内容附加到我以交互方式运行的每个命令中会非常有用:

| tee /tmp/lastCommand to all commands.  
Run Code Online (Sandbox Code Playgroud)

这样,如果输出显示自己有用,那么我可以对它做其他事情。

用法示例:

类型:

find . -type f 
Run Code Online (Sandbox Code Playgroud)

执行以下操作:

find . -type f | tee $lastOutLocation # Where lastOutputLocation=/tmp/lastOutput
Run Code Online (Sandbox Code Playgroud)

然后你可以使用这样的东西来过滤上一个命令:

lastOut | grep "SomeString" # Where lastOut is an alias to cat $lastOutputLocation
Run Code Online (Sandbox Code Playgroud)

有没有一种机制可以用来做这样的事情? 我不认为别名会暴露这种行为。

潜在的障碍:

  • 获得一些适用于复合命令的东西: echo 'baz' ; echo 'bar'
  • 弄清楚如何在运行之前修改用户键入的命令
  • 忽略诸如交互式输入之类的东西。不想捕获密码提示

可能的解决方案,我目前正在探索:

小智 -1

如果我理解您想将命令的输出重定向到文件,可以通过以下方式完成:

#command > output_file
Or also #command | output_file
Run Code Online (Sandbox Code Playgroud)

第一个更改输出通道,因此它将写入终端的所有内容都会写入output_file,第二个通过管道传输两个程序...