bash 将命令输出重定向到文件(现有命令)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28777306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect command output to file (existing command)
提问by ToBeReplaced
To write the stdout of a command (in this case, echo hi
) to a file, you can do:
要将命令的标准输出(在本例中为echo hi
)写入文件,您可以执行以下操作:
echo hi > outfile
I would like a command instead of a redirection or pipe so that I do not need to invoke a shell. This is eventually for use with Ansible, which calls python's subprocess.POpen
.
我想要一个命令而不是重定向或管道,这样我就不需要调用 shell。这最终用于 Ansible,它调用 python 的subprocess.POpen
.
I am looking for:
我在寻找:
stdout-to-file outfile echo hi
tee
makes copying stdout to a file easy enough, but it accepts stdin, not a separate command.
tee
使将标准输出复制到文件很容易,但它接受标准输入,而不是单独的命令。
Is there a common, portable command that does this? It's easy enough to write one, of course, but that's not the question. Ultimately, in Ansible, I want to do:
有没有一个通用的、可移植的命令来做到这一点?当然,写一个很容易,但这不是问题。最终,在 Ansible 中,我想做:
command: to-file /opt/binary_data base64 -d {{ base64_secret }}
Instead of:
代替:
shell: base64 -d {{ base64_secret }} > /opt/binary_data
Edit: Looking for a command available on RHEL 7, Fedora 21
编辑:寻找 RHEL 7、Fedora 21 上可用的命令
回答by Gourav Shah - Devops Trainer
What you are actually looking for, is a Ansible Module, which takes two parameters,
你真正要找的是一个 Ansible 模块,它有两个参数,
- The actual command
- A file to redirect the output of above command
- 实际命令
- 用于重定向上述命令输出的文件
In this case, you could use shell
module instead of command
module, which allows such redirections.
在这种情况下,您可以使用shell
module 而不是command
module,这允许此类重定向。
e.g.
例如
- shell: /usr/bin/your_command >> output.log
You could check the source and example documentation here.
This is the simplest. And I believe you are aware of this. I am just putting this across for anyone new to shell/command modules reading this thread.
这是最简单的。我相信你知道这一点。我只是把这个介绍给任何不熟悉 shell/命令模块的人阅读这个线程。
If you don't like to do it that way, you could still write a wrapper module, which accepts the "file name" as an argument and will run as,
如果您不喜欢那样做,您仍然可以编写一个包装器模块,它接受“文件名”作为参数并运行为,
- custommodule: output.log /usr/bin/your_command
All you may need to do is fork the repo, look into existing modules, and customize yours accordingly.
您可能需要做的就是 fork 存储库,查看现有模块,并相应地自定义您的模块。
回答by jhutar
Not sure if that is what you want, but in Ansible - Save registered variable to fileI have found what I have needed:
不确定这是否是您想要的,但在Ansible - 将注册的变量保存到文件中,我找到了我需要的内容:
- name: "Gather lsof"
command: lsof
register: lsof_command
- name: "Save lsof log"
local_command:
copy content="{{ lsof_command.stdout }}" dest="/root/lsof.log"
Or, in my specific case (might be useful for you as well) playbook was running on system A, but I needed the log from B and having it saved to localhost (because A system is hitting B and I want to log B's state):
或者,在我的特定情况下(可能对您也有用)剧本在系统 A 上运行,但我需要来自 B 的日志并将其保存到本地主机(因为 A 系统正在击中 B 并且我想记录 B 的状态) :
- name: "Gather lsof on B"
delegate_to: B
command: lsof
register: lsof_command
run_once: true
- name: "Save lsof log"
local_action:
copy content="{{ lsof_command.stdout }}" dest="/root/lsof.log"
run_once: true
IMO run_once: true
is required in my case, as I want the log gathered only once per playbook run (not say 10 times if playbook is running on 10 systems).
run_once: true
在我的情况下需要IMO ,因为我希望每次 playbook 运行只收集一次日志(如果 playbook 在 10 个系统上运行,则不说 10 次)。
Space for improvement would be to save stderr as well or possibly failing "Save lsof log" task when it is not empty.
改进的空间是保存 stderr 或者当它不为空时可能会失败的“保存 lsof 日志”任务。