在另一个命令的输出中给出的执行行
问题描述:
警告:这个问题不是关于git的使用,而是关于在Linux中使用管道的问题,这里给出git
命令作为示例。在另一个命令的输出中给出的执行行
具有git push
fatal: The current branch my_branch has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin my_branch
我想执行给定的命令这样的输出,即git push --set-upstream origin my_branch
。
通过做git push 2>&1 | tail -3
我得到git push --set-upstream origin my_branch
印在屏幕上。
问题:什么命令应该被添加到下一个管道,因此给定git push
将被执行。所以,我可以做git push 2>&1 | tail -3 | command_to_eval_given_string
答
只是管bash
本身:
git push ... | bash
这将从以前的管道发送标准输出来砸,那么将执行它。
$ echo "echo 'hello'" | bash
hello
$ echo "uptime" | bash
16:22:37 up 7:31, 3 users, load average: 0,03, 0,14, 0,23
答
而不是管道到另一个命令,你可以改为将管道包装在一个命令替换和eval
它。这将在您当前的shell会话中执行该命令。例如:
eval "$(printf 'some output 1\nsome output 2\necho execute me'| tail -1)";
## execute me
你想'尾巴-1',而不是'尾巴-3' ......这只是最后一行。 – gilez