简体   繁体   English

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

[英]How to substitute shell variable into screen's "stuff" command in Bash?

I have script that needs to resume Virtualbox machines, and would like to use machine name as a variable so I have:我有需要恢复 Virtualbox 机器的脚本,并且想使用机器名称作为变量,所以我有:

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

but variable is not visible in this command.但变量在此命令中不可见。 So I only see following command in screen window所以我只在屏幕 window 中看到以下命令

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

So I'm not sure if variable needs to be defined in that screen first or how to carry it inside single quotes.所以我不确定是否需要先在该屏幕中定义变量,或者如何将它放在单引号内。

screen's stuff understands \n so you can use double quotes for the command: screen 的stuff理解\n所以你可以在命令中使用双引号:

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

The following also works:以下也有效:

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

There is no parameter expansion taking place inside $'.... ' , but you can always catenate the pieces into a single string: $'.... '中没有参数扩展,但您始终可以将这些片段连接成一个字符串:

"VBoxManage controlvm ${VMN} resume --type headless"$'\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM