远程执行的复杂bash命令中的变量值 [英] Variables values in remotely executed complex bash commands

查看:44
本文介绍了远程执行的复杂bash命令中的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在远程主机上运行复杂的命令列表.通过使用此类命令创建另一个脚本,然后调用我的其他脚本(无论是否将其复制到远程主机),我都可以轻松解决,但是我不想这样做.我的问题是我有一些对于整个脚本固定的变量,然后有for循环变量.我可以在远程主机上对两者之一进行正确评估,但是两者都不可以.我在类似问题上发现的所有内容都是关于在不同元素之间使用引号引起来的讨论.我没有尝试过固定(在ssh之前扩展)和循环变量(在扩展上)(对您很重要,@ Inian,因为您不能他妈的在阅读完整问题之前将其标记为重复的内容而不是重复的内容).

I want to run a complex list of commands on a remote host. I could solve easily by creating another script with such commands and then have my other script call it (with or without copying it to the remote host), but I do not want to. My issue is that I have some variables that are fixed for the whole script and then I have the for-loop variable. I can get one of the two to be properly evaluated on the remote host, but noth both. All I've found on similar issues is talks about using note quotes, " or ' around different elements. Nothing I've tried worked on both the fixed (to be expanded before ssh) and the loop variables (to be expanded remotely) at the same time (highlight for you, @Inian, since you can't fucking read the full question before marking it as duplicate of something it's not a duplicate of).

fixed_value="blue"

ssh remote_host << "EOF"
    value_list="VALUE1 VALUE2"
    for i in $value_list; do
        echo "executing $i"
        my_program -arg1 $i -arg2 "$fixed_value"
    done
EOF

我期望的是运行带有两个参数的my_program(远程主机上的二进制文件),一个是固定的,实际上是作为输入传递给本地脚本的,另一个是从固定值的列表中读取的,因此,在该示例中,我应该最终在远程主机上执行以下操作:

What I expect is to run my_program (which is a binary on the remote host) with two arguments, one is fixed and is actually passed as an input to the local script, the other is read from a list of fixed values, so that, in the example, I should end up executing on remote host the following:

my_program -arg1 VALUE1 -arg2 blue
my_program -arg1 VALUE2 -arg2 blue

我可能需要一种将值传递值"到远程主机中正在执行的脚本的方法,或者在发送命令之前强制对$ fixed_value进行求值(我曾尝试使用引号来执行此操作,但此方法不起作用),而不会阻止在远程主机上对$ i进行评估.因为我通过切换引号(或删除引号或更改引号)而得到的只是:

I probably need a way to 'pass values' to the script being executed in the remote host, or force evaluation of $fixed_value before the command is sent (which I tried to do by using quotes, but it didn't work), without preventing the evaluation of $i from being done on the remote host. Because all I get by switching around the quotes (or removing them, or changing them) is either:

my_program -arg1 -arg2 blue
my_program -arg1 -arg2 blue

my_program -arg1 VALUE1 -arg2
my_program -arg1 VALUE2 -arg2

推荐答案

重组后的

fixed_value="blue"
value_list=( VALUE1 VALUE2 )
for i in "${value_list[@]}"; do
    echo "executing $i"
    ssh remote_host "my_program -arg1 $i -arg2 $fixed_value"
done

如果您真的希望在一次调用中完成所有操作,则将其编写为脚本,在 scp 上写完,然后在 ssh 上调用它机器.

If you really want this to all be done in a single call, then write it as a script, scp it over, then ssh to call it on the target machine.

...或将其本地写入临时文件:

...or write it locally to a temp file :

$: cat a
echo '
fixed_value="blue"
value_list=( VALUE1 VALUE2 )
for i in "${value_list[@]}"; do
    echo "executing $i"
    echo "my_program -arg1 $i -arg2 $fixed_value"
done
'>/tmp/foo && . /tmp/foo && rm /tmp/foo

然后以

ssh remote_host < a

产生:

Pseudo-terminal will not be allocated because stdin is not a terminal.
executing VALUE1
my_program -arg1 VALUE1 -arg2 blue
executing VALUE2
my_program -arg1 VALUE2 -arg2 blue

这篇关于远程执行的复杂bash命令中的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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