linux - 在 osascript 中传递 bash 变量时出错

标签 linux macos bash ssh osascript

使用 osascript,我可以告诉终端应用程序在单独的终端窗口中打开 ssh 连接,但是,当我尝试在脚本中传递 bash 变量时遇到错误。

这个脚本工作得很好:

osascript -e 'tell app "Terminal"
    do script "ssh user@hostname -p port -L 9999:localhost:80 -i keyfile"
end tell'
...more commands...

但是运行这样的脚本...

read -p "Port: " prt
read -p "Localhost port: " lhprt
osascript -e 'tell app "Terminal"
            do script "ssh user@hostname -p \"$prt\" -L \"$lhprt\":localhost:80 -i keyfile"
        end tell'
...more commands...

它不传递变量,即在新终端窗口中传递的命令是

ssh user@hostname -p "$prt"

我该如何格式化它才能让我的 bash 变量通过?

最佳答案

只需反转引号类型 使用单引号会阻止 shell 扩展您的参数

编辑: 您也可以关闭变量周围的字符串。 IE。 '这是“字符串”中的“$变量”。'

关于linux - 在 osascript 中传递 bash 变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36075693/

相关文章:

Linux - Shell 脚本并行运行 curl 命令

java - 64 位和 8 GB Mac 上的 "java.lang.OutOfMemoryError: unable to create new native thread"!

linux - 自定义 bash 完成的提示

linux - shell脚本: when to test on the last command?

Bash 与 Zsh,git grep 在 bash for 循环中工作,但在 zsh for 循环中不起作用?

linux - 使用 ssh 启动远程进程并退出

linux - 在 Windows 10 上的 Docker 容器中运行 X-Windows 桌面应用程序

linux - 在源代码级别是否有任何 linux 内核模块的模块依赖定义?

macos - Mac OS X 相当于 VirtualQuery 或/proc/pid/maps?

c++ - “ future ”已在此处明确标记为已删除