2016-08-18 98 views
1

警告:這個問題不是關於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

+0

你想'尾巴-1',而不是'尾巴-3' ......這只是最後一行。 – gilez

回答

2

只是管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 
1

而不是管道到另一個命令,你可以改爲將管道包裝在一個命令替換和eval它。這將在您當前的shell會話中執行該命令。例如:

eval "$(printf 'some output 1\nsome output 2\necho execute me'| tail -1)"; 
## execute me 
相關問題