0

尝试使用 bash 脚本将本地变量传递给远程 shell。这是我正在尝试的。这是 test.sh

#!/bin/bash

envi=$1

function samplefunction {
                        echo "Environment selected is $envi"
                        }

if [ "$envi" = "test" ]; then
   ssh user@remotehost <<EOF
   $(typeset -f samplefunction)
   samplefunction
EOF

else
 echo "Please pass correct parameter which is - test"
fi

当我尝试执行“./test.sh test”时,我得到的结果是“选择的环境是”。shell 无法将变量传递给远程系统。

4

1 回答 1

0

ssh不会(也不能)使这些变量在远程端可用。

您可以像嵌入函数一样嵌入定义:

ssh user@remotehost <<EOF
   $(declare -p envi)
   $(typeset -f samplefunction)
   samplefunction
EOF

您还可以复制所有已知变量。在这种情况下,它有助于消除有关设置只读值的错误:

ssh user@remotehost <<EOF
   {
     $(declare -p)
   } 2> /dev/null
   $(typeset -f samplefunction)
   samplefunction
EOF

如果您有一个经常要复制的特定变量,您可以选择通过添加到您的 ssh 配置来ssh自动发送它。SendEnv envi必须导出变量才能使其正常工作。

于 2018-05-08T03:48:47.610 回答