Search code examples
shellvariablesparameterizedsshpass

export parameterized variables in shellscript using sshpass


I have a use case similar to the below code. Need to export parameterized variables/identifier to remote shell script from a shell script. I used the below code but I cannot export the value. Please suggest how to do it.

A.sh (script 1)

#!/bin/bash
sshpass -p asdf ssh rock@host.com<<'ENDSSH'
export directory="$1"
sh /../B.sh
ENDSSH

B.sh (script 2)

#!/bin/bash
echo directory=$directory
mkdir $directory

#Execution
sh A.sh '/data/2017-7-7/' 

#output
directory=

I get the value in the remote shell script when I hard code the value.

export directory='/data/2017-7-7/' 

I want to export parameterized variable, please suggest how to implement this.Thanks


Solution

  • Just remove quotes from the first ENDSSH:

    sshpass -p asdf ssh rock@host.com << ENDSSH
    export directory="$1"
    sh /../B.sh
    ENDSSH
    

    According to the bash manual:

    Here Documents

    [...]

    The format of here-documents is:

    [n]<<[-]word
        here-document
    delimiter
    

    No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

    [...]