【问题标题】:How to substite string into remote screen command in bash?如何将字符串替换为 bash 中的远程屏幕命令?
【发布时间】:2023-01-23 17:52:08
【问题描述】:

我有需要恢复 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 screen


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      screen 的 stuff 理解 所以你可以在命令中使用双引号:

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

      以下也有效:

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

      【讨论】:

      • 谢谢@pynexj 我实际上并不准确 "$VMN" 应该用双引号引起来,所以我不确定如何使用它 sfuff 和双引号
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2020-09-02
      • 2016-03-19
      • 2013-06-27
      • 2016-11-12
      相关资源
      最近更新 更多