在本地环境 ssh 命令中分配一个 bash 脚本变量
assign a bash script variable in local environment ssh commands
我按照 this 教程在 bash 脚本中通过 ssh 执行了 运行 多个命令。但是现在我想通过 ssh 获取我正在 运行ning 的命令之一的输出,将其保存在变量中并在下一个命令中使用。
#!/bin/bash
ssh -t $VM_IP_ADDRESS bash -c "'
export NET_ID=nova network-list | egrep $SUBNET_NAME | get_field 1
nova boot $INSTANCE1_NAME --flavor m1.small --image $UBUNTU_IMAGE_NAME --key-name $KEY_PAIR_NAME --security-groups default --nic net-id=$NET_ID
'"
我想在我的本地环境中设置 NET_ID,然后在下一个命令中使用它。
现在,当我 运行 出现此错误时:
bash: line 17: export: `network-list': not a valid identifier
但我已经检查过了,这个命令在目标机器上给了我正确的答案。
nova network-list | egrep test_net | get_field 1
更新:
使用此命令,NET_ID 变量在目标机器环境中设置,但我想在我的本地机器中设置它。
export NET_ID=$(nova network-list | egrep $SUBNET_NAME | get_field 1)
您可能需要这个(本地计算机上的脚本 运行):
#!/bin/bash
user=<remote-username>
ip=<remote-ip>
SUBNET_NAME=<you-know-the-value>
export NET_ID=$(ssh $user@$ip "nova network-list | egrep $SUBNET_NAME | get_field 1")
# Now, do anything with the $NET_ID on your local machine.
警告:NET_ID
变量将仅在您的脚本和由您的脚本启动的任何子进程中可见。您无法将其导出到父环境(如果这是问题所在)。
更新:最终适用于 OP 的解决方案:
NET_ID=$(ssh -t $VM_IP_ADDRESS bash -c "' \
<commands>
nova network-list | egrep $SUBNET_NAME | get_field 1; \
<commands> '")
echo $NET_ID
我按照 this 教程在 bash 脚本中通过 ssh 执行了 运行 多个命令。但是现在我想通过 ssh 获取我正在 运行ning 的命令之一的输出,将其保存在变量中并在下一个命令中使用。
#!/bin/bash
ssh -t $VM_IP_ADDRESS bash -c "'
export NET_ID=nova network-list | egrep $SUBNET_NAME | get_field 1
nova boot $INSTANCE1_NAME --flavor m1.small --image $UBUNTU_IMAGE_NAME --key-name $KEY_PAIR_NAME --security-groups default --nic net-id=$NET_ID
'"
我想在我的本地环境中设置 NET_ID,然后在下一个命令中使用它。 现在,当我 运行 出现此错误时:
bash: line 17: export: `network-list': not a valid identifier
但我已经检查过了,这个命令在目标机器上给了我正确的答案。
nova network-list | egrep test_net | get_field 1
更新:
使用此命令,NET_ID 变量在目标机器环境中设置,但我想在我的本地机器中设置它。
export NET_ID=$(nova network-list | egrep $SUBNET_NAME | get_field 1)
您可能需要这个(本地计算机上的脚本 运行):
#!/bin/bash
user=<remote-username>
ip=<remote-ip>
SUBNET_NAME=<you-know-the-value>
export NET_ID=$(ssh $user@$ip "nova network-list | egrep $SUBNET_NAME | get_field 1")
# Now, do anything with the $NET_ID on your local machine.
警告:NET_ID
变量将仅在您的脚本和由您的脚本启动的任何子进程中可见。您无法将其导出到父环境(如果这是问题所在)。
更新:最终适用于 OP 的解决方案:
NET_ID=$(ssh -t $VM_IP_ADDRESS bash -c "' \
<commands>
nova network-list | egrep $SUBNET_NAME | get_field 1; \
<commands> '")
echo $NET_ID