使用 sshpass 在 shellscript 中导出参数化变量
export parameterized variables in shellscript using sshpass
我有一个类似于下面代码的用例。需要将参数化 variables/identifier 从 shell 脚本导出到远程 shell 脚本。我使用了以下代码,但无法导出该值。请建议如何操作。
A.sh(脚本 1)
#!/bin/bash
sshpass -p asdf ssh rock@host.com<<'ENDSSH'
export directory=""
sh /../B.sh
ENDSSH
B.sh(脚本 2)
#!/bin/bash
echo directory=$directory
mkdir $directory
#Execution
sh A.sh '/data/2017-7-7/'
#output
directory=
当我对值进行硬编码时,我在远程 shell 脚本中获取了该值。
export directory='/data/2017-7-7/'
我想导出参数化变量,请建议如何实现this.Thanks
只需删除第一个引号 ENDSSH
:
sshpass -p asdf ssh rock@host.com << ENDSSH
export directory=""
sh /../B.sh
ENDSSH
根据 bash 手册:
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 `
.
[...]
我有一个类似于下面代码的用例。需要将参数化 variables/identifier 从 shell 脚本导出到远程 shell 脚本。我使用了以下代码,但无法导出该值。请建议如何操作。
A.sh(脚本 1)
#!/bin/bash
sshpass -p asdf ssh rock@host.com<<'ENDSSH'
export directory=""
sh /../B.sh
ENDSSH
B.sh(脚本 2)
#!/bin/bash
echo directory=$directory
mkdir $directory
#Execution
sh A.sh '/data/2017-7-7/'
#output
directory=
当我对值进行硬编码时,我在远程 shell 脚本中获取了该值。
export directory='/data/2017-7-7/'
我想导出参数化变量,请建议如何实现this.Thanks
只需删除第一个引号 ENDSSH
:
sshpass -p asdf ssh rock@host.com << ENDSSH
export directory=""
sh /../B.sh
ENDSSH
根据 bash 手册:
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`
.[...]