gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:41 27 4
gpt4 key购买 nike

使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com