Bash assign output to variable inside here document

前端 未结 2 1215
独厮守ぢ
独厮守ぢ 2021-01-28 05:15

In a bash script, what I need is to ssh to a server and do some commands, what I got so far is like this:

outPut=\"ss\"
ssh user@$serverAddress <         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-28 05:30

    No, sorry that will not work.

    1. Either use command substitution value (to printf) inside the here-doc (lines bettween EOF):

      ssh user@$serverAddress << EOF
          printf 'output is %s\n' "$(ls -l /opt/logs)"
      EOF
      
    2. Or you capture the execution of command from inside ssh, and then use the value.

      outPut="$( 
          ssh user@$serverAddress << EOF
              cd /opt/logs
              ls -l
      EOF
      )"
      echo 'output is ' "$outPut"
      echo "${outPut}"
      

    Option 1 looks cleaner.

提交回复
热议问题