是否可以在远程 ssh 命令中使用变量? [英] is it possible to use variables in remote ssh command?

查看:132
本文介绍了是否可以在远程 ssh 命令中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在远程机器上按顺序执行几个命令,后面的一些命令依赖于前面的命令.在最简单的例子中,我得到了这个:

I'd like to execute several commands in sequence on a remote machine, and some of the later commands depend on earlier ones. In the simplest possible example I get this:

ssh my_server "echo this is my_server; abc=2;"
this is my_server
abc=2: Command not found.
ssh my_server "echo this is my_server; abc=2; echo abc is $abc"
abc: undefined variable

对于一些背景信息,我真正想做的是拼凑一个路径并启动一个java应用程序:

For a bit of background info, what I actually want to do is piece together a path and launch a java application:

ssh my_server 'nohup sh -c "( ( echo this is my_server; jabref_exe=`which jabref`; jabref_dir=`dirname $jabref_exe`; java -jar $jabref_dir/../jabref.jar` $1 &/dev/null ) & )"' &
jabref_dir: Undefined variable.

这样,每当 jabref 在服务器上更新为新版本时,我就不必手动更新 jar 文件的路径.jabref 可执行文件不接受参数,但使用 java -jar 启动它可以,这就是为什么我必须稍微调整路径.

That way, whenever jabref gets updated to a new version on the server, I won't have to manually update the path to the jar file. The jabref executable doesn't take arguments, but launching it with java -jar does, which is why I have to juggle the path a bit.

目前我有一个单独的脚本文件中的命令列表并调用

At the moment I have the list of commands in a separate script file and call

ssh my_server 'nohup sh -c "( ( my_script.sh &/dev/null ) & )"' &

这是可行的,但由于 ssh 调用已经在一个脚本文件中,所以最好将所有内容放在一起.

which works, but since the ssh call is already inside one script file it would be nice to have everything together.

推荐答案

在 chepner 的启发下,我现在有了一个可行的解决方案,但仅当从 bash shell 或 bash 脚本调用时才有效.它不适用于 tcsh.

With some inspiration from chepner, I now have a solution that works, but only when called from a bash shell or bash script. It doesn't work from tcsh.

ssh my_server "bash -c 'echo this is \$HOSTNAME; abc=2; echo abc is \$abc;'"

基于此,下面的代码是一个在远程服务器上运行 jabref 的本地脚本(尽管默认情况下使用 X 转发和无密码身份验证,用户无法判断它是远程的):

Based on this, the code below is a local script which runs jabref on a remote server (although with X-forwarding by default and passwordless authentication the user can't tell it's remote):

#!/bin/bash
if [ -f "$1" ]
then
    fname_start=$(echo ${1:0:4})
    if [ "$fname_start" = "/tmp" ]
    then
        scp $1 my_server:$1
        ssh my_server "bash -c 'source load_module jdk; source load_module jabref; java_exe=\$(which java); jabref_exe=\$(which jabref); jabref_dir=\$(echo \${jabref_exe%/bin/jabref});eval \$(java -jar \$jabref_dir/jabref.jar $1)'" &
    else
        echo input argument must be a file in /tmp.
else
    echo this function requires 1 argument
fi

这是 1 行脚本 load_module,因为 modulecmd 设置了环境变量,如果没有找到脚本,我无法弄清楚如何做到这一点.

and this is the 1-line script load_module, since modulecmd sets environment variables and I couldn't figure out how to do that without sourcing a script.

eval `/path/to/modulecmd bash load $1`;

我还查看了受如何使用 SSH 在远程机器上运行 shell 脚本?http://tldp.org/LDP/abs/html/here-docs.html.好的部分是它甚至可以从 tcsh 工作.我从命令行开始工作,但不在脚本中.这可能很容易解决,但我现在有了一个解决方案,所以我很高兴:-)

I also looked at heredocs, inspired by How to use SSH to run a shell script on a remote machine? and http://tldp.org/LDP/abs/html/here-docs.html. The nice part is that it works even from tcsh. I got this working from the command line, but not inside a script. That's probably easy enough to fix, but I've got a solution now so I'm happy :-)

ssh my_server 'bash -s' << EOF
echo this is \$HOSTNAME; abc=2; echo abc is \$abc;
EOF

这篇关于是否可以在远程 ssh 命令中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆