What is the cleanest way to ssh and run multiple commands in Bash?

前端 未结 12 2314
礼貌的吻别
礼貌的吻别 2020-11-22 09:14

I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like:

ssh blah_server "ls; pwd;"
         


        
12条回答
  •  再見小時候
    2020-11-22 09:36

    The posted answers using multiline strings and multiple bash scripts did not work for me.

    • Long multiline strings are hard to maintain.
    • Separate bash scripts do not maintain local variables.

    Here is a functional way to ssh and run multiple commands while keeping local context.

    LOCAL_VARIABLE=test
    
    run_remote() {
        echo "$LOCAL_VARIABLE"
        ls some_folder; 
        ./someaction.sh 'some params'
        ./some_other_action 'other params'
    }
    
    ssh otherhost "$(set); run_remote"
    

提交回复
热议问题