Execute multiple commands on remote machine

白昼怎懂夜的黑 提交于 2019-12-12 19:31:42

问题


In the following command i am trying to ssh commands and execute the multiple commands.How can i exit if any of the command fails i.e, if !command1 exit then if !command1 and !commnd 2 exit else execute command 3 on the remote machine.How can i achieve this

 ssh login.com "git clone --bare new@login2.com/repo/ /home/usr1/repo/ && \
                cd /home/usr1/repo/info/                               && \
                echo "hello world" > .a.txt"

回答1:


Just make sure your quotes don't conflict:

ssh login.com \
   'git clone --bare new@login2.com/repo/ /home/usr1/repo/ &&
    cd /home/usr1/repo/info/ &&
    echo "hello world" > .a.txt'

This assumes you wanted to leave a.txt on the server, otherwiseL

ssh login.com \
   'git clone --bare new@login2.com/repo/ /home/usr1/repo/ &&
    cd /home/usr1/repo/info/ &&
    echo "hello world"' > .a.txt



回答2:


What you have there will do it — the && chaining will mean the next command only runs if the previous succeeded.

$ true && false && echo "here"
$ true && true && echo "here"
here
$


来源:https://stackoverflow.com/questions/9817950/execute-multiple-commands-on-remote-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!