存在多个“'”问题的 Bash 配置文件命令

存在多个“'”问题的 Bash 配置文件命令

再会!

我目前正在尝试使以下别名起作用。'git_fetch' 和 'git_tree' 是自定义别名,所以不必担心它们。

alias git_workspace='osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e 'end tell' &> /dev/null && osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e 'end tell' &> /dev/null'

(我添加了换行符以提高可读性)

alias git_workspace='osascript -e 'tell application "Terminal"' 
-e 'tell application "System Events" to tell process "Terminal" 
to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear 
&& git_fetch\" in selected tab of the front window" -e 'end tell' 
&> /dev/null 
&& osascript -e 'tell application "Terminal"' -e 'tell application "System Events" 
to tell process "Terminal" to keystroke "n" using command down' 
-e "do script with command \"cd `pwd`;clear && git_tree\" 
in selected tab of the front window" -e 'end tell' &> /dev/null'

但是我无法让它工作。重新加载 ~/.profile 会导致:

-bash: alias: application: not found
-bash: alias: Terminal -e tell: not found
-bash: alias: application: not found
-bash: alias: System Events: not found
-bash: alias: to: not found
-bash: alias: tell: not found
-bash: alias: process: not found
-bash: alias: Terminal: not found
-bash: alias: to: not found
-bash: alias: keystroke: not found
-bash: alias: n: not found
-bash: alias: using: not found
-bash: alias: command: not found
-bash: alias: down -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e end: not found
-bash: alias: tell &> /dev/null && osascript -e tell: not found
-bash: alias: application: not found
-bash: alias: Terminal -e tell: not found
-bash: alias: application: not found
-bash: alias: System Events: not found
-bash: alias: to: not found
-bash: alias: tell: not found
-bash: alias: process: not found
-bash: alias: Terminal: not found
-bash: alias: to: not found
-bash: alias: keystroke: not found
-bash: alias: n: not found
-bash: alias: using: not found
-bash: alias: command: not found
-bash: alias: down -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e end: not found
-bash: alias: tell &> /dev/null: not found

有人能帮助我吗?:)

编辑:我找到解决方案了!

alias git_workspace='osascript -e "tell application \"Terminal\"" -e "tell application \"System Events\" to tell process \"Terminal\" to keystroke \"n\" using command down" -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e "end tell" &> /dev/null && osascript -e "tell application \"Terminal\"" -e "tell application \"System Events\" to tell process \"Terminal\" to keystroke \"n\" using command down" -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e "end tell" &> /dev/null'

答案1

问题是别名的格式是这样的:

alias thing='commands'
alias thingy="commands"

换句话说,别名的命令必须用引号引起来。

你已经得到了这个:

alias git_workspace='osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e 'end tell' &> /dev/null && osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e 'end tell' &> /dev/null'

现在有一个大问题,在这个过长的命令中,你中间有太多的引号。所以你的别名实际上会被读成这样

alias git_workspace='osascript -e '

并且由于您在该位之后有所有这些尾随命令,它会破坏您的 bash_profile 文件。

我个人一直使用的最简单的解决方法是在单独的文件中创建脚本,基本上将以下内容粘贴到一个空文件中。

osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e 'end tell' &> /dev/null && osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e 'end tell' &> /dev/null

您可以将文件命名为 git_workspace.sh 或 git_workspace 或其他任何名称,名称由您决定,然后您需要授予文件执行权限

chmod +x git_workspace.sh

之后,通过运行脚本来测试它以确保它正常工作,如下所示

/path/to/git_workspace.sh

如果它按照你预期的方式执行,请转到你的 bash 配置文件并创建此别名

alias git_workspace='/path/to/git_workspace.sh'

然后它就可以正常工作了。当然,还有许多其他方法可以解决这个问题,但我发现这是最简单的方法,我在主目录中创建了一个 .scripts 目录,并将多年来创建的所有脚本都放在其中。这也使备份我的脚本变得容易,以防我想更换计算机或重新安装操作系统。

答案2

别名不是正确的解决方案 - 所需的嵌套转义将变得难以管理。我将使用名为希雷多克在一个函数中:

git_workspace () {
(
  osascript <<-'EOF'
        tell application "Terminal"' 
        tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
        do script with command "cd `pwd`;clear && git_fetch" in selected tab of the front window
        end tell
EOF

  if [ $? -eq 0 ]; then
        osascript <<-'EOF'
            tell application "Terminal"
            tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
            do script with command "cd `pwd`;clear && git_tree" in selected tab of the front window
            end tell
EOF
   fi
)  &>/dev/null
}
  • 将把osascriptheredoc 输入视为文件而不是字符串参数,因此-e不需要该参数。
  • <<EOFHeredoc 语句将输出与和标记之间所写的完全相同的文本EOF,但有以下注意事项:
    • 默认情况下 heredoc将要解释 \、$ 和 ` 字符。要禁用此功能,请将 Heredoc<<EOF标记括在引号中,<<'EOF'
    • 两个标记之间的所有内容都将被输出,包括前导空格。为了允许缩进(好处是可读代码而不是脚本功能),如果 heredoc 运算符具有破折号参数,即,bash 将从文本中删除所有前导制表符<<-'EOF'
    • 在任何情况下,结束的 heredoc 标记必须位于新行上,并且没有前导空格(制表符或其他)。

如果您使用这种方法,您可以完全按照本机发出的方式输入 osascript 命令,而不必解决任何与 bash 扩展/替换有关的问题。

我将两个 osascript 命令序列包装在一个子 shell 中,以便更轻松地重定向输出。

如果您想查看 osascript 的文本 / 参数是什么样子,请临时修改该函数,将其替换为osascriptcat删除重定向到 /dev/null。现在编写该函数时,输出如下所示:

tell application "Terminal"' 
tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
do script with command "cd `pwd`;clear && git_fetch" in selected tab of the front window
end tell
tell application "Terminal"
tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
do script with command "cd `pwd`;clear && git_tree" in selected tab of the front window
end tell

相关内容