如何将 shell 变量替换为 Bash 中屏幕的“stuff”命令?

问题描述 投票:0回答:2

我有需要恢复 Virtualbox 机器的脚本,并且想使用机器名称作为变量,所以我有:

VMN="VMtest"
screen -S MyScr -p 3 -X stuff $'VBoxManage controlvm "${VMN}" resume --type headless\n'

但是变量在此命令中不可见。所以我只在屏幕窗口中看到以下命令

[me@srv ~]$ VBoxManage controlvm resume --type headless

所以我不确定是否需要首先在该屏幕中定义变量或如何将其放在单引号内。

bash variables gnu-screen
2个回答
1
投票

screen 的

stuff
可以理解
\n
,因此您可以在命令中使用双引号:

VMN="VMtest"
screen -S MyScr -p 3 -X stuff "VBoxManage controlvm $VMN resume --type headless\n"

以下方法也适用:

# CTRL-J
... stuff "VBoxManage controlvm $VMN resume --type headless^J"
# \ooo (octal)
... stuff "VBoxManage controlvm $VMN resume --type headless\012"

1
投票

$' .... '
内部没有发生参数扩展,但您始终可以将这些片段连接成单个字符串:

"VBoxManage controlvm ${VMN} resume --type headless"$'\n'
© www.soinside.com 2019 - 2024. All rights reserved.