Execute multiple commands on remote machine

2019-07-21 08:41发布

问题:

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
$