2

我正在创建一个 bash 别名,这样我就可以 cd 到给定的目录并运行一个打开 pwd 的命令。我的脚本运行良好,但是当我抓取 ${pwd} 时,它会抓取 bash_profile 文件的密码。如何获取调用终端窗口的密码?

alias opencoda="osascript -e 'tell application \"Coda\"' -e 'tell document 1' -e 'change local path \"${pwd}\"' -e 'end tell' -e 'end tell'"

解决方案我不确定为什么上面给出了 bash_profile 目录,而这个给出了终端目录,但尽管如此:

alias opencoda='osascript -e "tell application \"Coda\"" -e "tell document 1" -e "change local path \"${PWD}\"" -e "end tell" -e "end tell"'

我不得不改变引号..显然还需要在里面保留双引号。

我刚刚写的另一个有趣的 Coda bash 脚本:

从当前目录打开给定文件:

function coda() {  osascript -e "tell application \"Coda\"" -e "tell document 1" -e "open \"${PWD}/$@\"" -e "end tell" -e "end tell";}

例如)结尾 myfile.txt

4

2 回答 2

2

当您在双引号字符串中引用变量时,Bash 会立即替换该变量的值。您需要做的就是转义$,以便不会发生替换。这样,当您运行 时opencoda,Bash 将$PWD在命令中看到变量引用并在那时进行替换。

alias opencoda="... \${PWD} ..."

(顺便说一句,在我的电脑上只有$PWD[大写] 有效。)

于 2011-10-06T19:24:08.350 回答
0

我不太确定这是在做什么,所以这是一个相当疯狂的猜测,但我会尝试转义$变量中的\${pwd}。然后在第一次解析时 .bash_profile 将评估它${pwd}然后应该传递正确的变量。

于 2011-10-06T19:22:40.883 回答