shell 脚本中使用 ssh 的未定义变量
Undefined variable in shell script by using ssh
我将 shell 脚本用于 运行 R 程序,如下所示:
host_list="server@com" Directory="/home/program/" ssh -f "$host_list" 'cd $Directory && nohup Rscript L_1.R> L_1_sh.txt'
但它总是说
Directory: Undefined variable.
我不确定,但也许你可以试试这些:
- 在bash单引号
''
不替换变量Manual
- 尝试使用
${Directory}
或更改变量名(可能是
保留)
SSH 不会传播您的所有环境变量。您只是在本地客户端 ssh
程序的环境中设置,而不是在服务器端。作为 hack,只需将它放在 ssh
是 运行 的远程命令中,而不是预命令环境设置。
host_list="server@com" ssh -f "$host_list" 'Directory="/home/program/"; cd "$Directory" && nohup ...'
这是一个更简单的命令版本,您可以在不依赖特定程序设置的情况下对其进行测试。
ssh localhost Dir="/etc"; echo Dir is "$Dir"; cd "$Dir" && pwd && ps
我将 shell 脚本用于 运行 R 程序,如下所示:
host_list="server@com" Directory="/home/program/" ssh -f "$host_list" 'cd $Directory && nohup Rscript L_1.R> L_1_sh.txt'
但它总是说
Directory: Undefined variable.
我不确定,但也许你可以试试这些:
- 在bash单引号
''
不替换变量Manual - 尝试使用
${Directory}
或更改变量名(可能是 保留)
SSH 不会传播您的所有环境变量。您只是在本地客户端 ssh
程序的环境中设置,而不是在服务器端。作为 hack,只需将它放在 ssh
是 运行 的远程命令中,而不是预命令环境设置。
host_list="server@com" ssh -f "$host_list" 'Directory="/home/program/"; cd "$Directory" && nohup ...'
这是一个更简单的命令版本,您可以在不依赖特定程序设置的情况下对其进行测试。
ssh localhost Dir="/etc"; echo Dir is "$Dir"; cd "$Dir" && pwd && ps