使用 bash 脚本为 Sublime Text 2 构建系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13968552/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build system with bash script for Sublime Text 2
提问by Max Yankov
I work with SQL queries in Sublime 2. I have a custom script that takes sql filename as an argument, uploads it to the server, downloads the query result and converts it to valid csv file. It works, and it's very useful.
我在 Sublime 2 中使用 SQL 查询。我有一个自定义脚本,它将 sql 文件名作为参数,将其上传到服务器,下载查询结果并将其转换为有效的 csv 文件。它有效,而且非常有用。
However, when I tried to set up a build system for Sublime Text 2 using this script, I get errors. Here's my sublime-build file:
但是,当我尝试使用此脚本为 Sublime Text 2 设置构建系统时,出现错误。这是我的崇高构建文件:
{
    "cmd" : ["exec_sql.sh", "$filename"],
    "selector" : "source.sql",
    "path" : "/Users/username/sql"
}
SQL files and exec_sql script are both located in /Users/username/sql/ folder. Here's what I get:
SQL 文件和 exec_sql 脚本都位于 /Users/username/sql/ 文件夹中。这是我得到的:
/Users/username/sql/exec_sql.sh: line 9: which: command not found
/Users/username/sql/exec_sql.sh: line 16: basename: command not found
/Users/username/sql/exec_sql.sh: line 19: [email protected]:/tmp/: No such file or directory
/Users/username/sql/exec_sql.sh: line 24: ssh: command not found
/Users/username/sql/exec_sql.sh: line 25: [email protected]:/tmp/.csv: No such file or directory
[Finished in 0.0s with exit code 127]
It seems that despite being a command-line script, sh file is being interpreted as something else. How do I fix it?
似乎尽管是命令行脚本,但 sh 文件被解释为其他东西。我如何解决它?
采纳答案by aanton
I think that the problem can be solved if your custom script is added to the system path (to the BIN directory).
我认为如果将您的自定义脚本添加到系统路径(到 BIN 目录),问题就可以解决。
I am going to use a detailed example that i have used succesfully.
我将使用我成功使用的详细示例。
- Be sure that your custom script is marked as executable and has got the shebang. A simple script that includes the basename command that fails in your example (i have called it test_basename.sh): - #!/bin/bash echo basename
- Make a symlink to include the script in the BIN directory so it can be executed in any folder. In my Ubuntu i do it with this command: - sudo ln -s /Users/username/sql/test_basename.sh /usr/bin/test_basename.sh
- Check the script in the console in any directory - cd /tmp test_basename.sh some-filename
- Restart Sublime Text 
- Create a build system that uses the simple script. I have used $file as an example. Other options are in the documentation. - { "cmd" : ["test_basename.sh", "$file"] }
- Open a file in Sublime Text and run the build system created. 
- 确保您的自定义脚本被标记为可执行文件并获得了shebang。包含在您的示例中失败的 basename 命令的简单脚本(我将其称为 test_basename.sh): - #!/bin/bash echo basename
- 制作符号链接以将脚本包含在 BIN 目录中,以便它可以在任何文件夹中执行。在我的 Ubuntu 中,我使用以下命令执行此操作: - sudo ln -s /Users/username/sql/test_basename.sh /usr/bin/test_basename.sh
- 在控制台中的任何目录中检查脚本 - cd /tmp test_basename.sh some-filename
- 重启 Sublime Text 
- 创建一个使用简单脚本的构建系统。我以 $file 为例。其他选项在文档中。 - { "cmd" : ["test_basename.sh", "$file"] }
- 在 Sublime Text 中打开一个文件并运行创建的构建系统。 
回答by DiCaprio
If you don't want to create a symlink use a variable like $project_pathor $file_path
如果您不想创建符号链接,请使用类似$project_path或$file_path
{
    "shell_cmd": "./do.sh",
    "shell": true,
    "working_dir": "$project_path"
}
reference for variables: sublimeText docs
变量参考:sublimeText 文档
回答by Greg Burghardt
I was able to create a general purpose Bash build system for Sublime Text 2:
我能够为 Sublime Text 2 创建一个通用的 Bash 构建系统:
- Create a new Build System
- Use the following code: - { "cmd": ["/path/to/bash-build.sh", "$folder"], "shell": true, "working_dir": "$folder", "windows": { "cmd": ["C:\path\to\bash-build.sh", "$folder"] } }
- Save it as - bash.sublime-build
- Create a new shell script using your editor of choice and paste the following code into it: - #!/bin/env bash echo "Start building..." if [ -f /build.sh ]; then cd build.sh cd - else echo "File doesn't exist: /build.sh" fi echo "Done." exit $?- Save it as - bash-build.shin the same folder that you reference in the Sublime Build System
- Open a folder in Sublime (note: on Windows, build systems seem broken when Sublime is opened from the command line) 
- Create a new text file and save it as - build.shinto the root folder opened in Subilme
- In - build.sh, go crazy:- #!/bin/env bash # do some crazy, custom build exit $?
- 创建一个新的构建系统
- 使用以下代码: - { "cmd": ["/path/to/bash-build.sh", "$folder"], "shell": true, "working_dir": "$folder", "windows": { "cmd": ["C:\path\to\bash-build.sh", "$folder"] } }
- 将其另存为 - bash.sublime-build
- 使用您选择的编辑器创建一个新的 shell 脚本并将以下代码粘贴到其中: - #!/bin/env bash echo "Start building..." if [ -f /build.sh ]; then cd build.sh cd - else echo "File doesn't exist: /build.sh" fi echo "Done." exit $?- 将其保存 - bash-build.sh在您在 Sublime Build System 中引用的同一文件夹中
- 在 Sublime 中打开一个文件夹(注意:在 Windows 上,从命令行打开 Sublime 时构建系统似乎已损坏) 
- 创建一个新的文本文件并将其保存 - build.sh到在 Subilme 中打开的根文件夹中
- 在 - build.sh,发疯:- #!/bin/env bash # do some crazy, custom build exit $?
I've started using this in conjunction with Pandocto generate HTML files from Markdown files.
我已经开始使用它与Pandoc结合从 Markdown 文件生成 HTML 文件。
回答by DataGreed
It seems like you are on Mac. I had the same problem before I realized that the System path variable is wrong.
好像你在 Mac 上。在我意识到系统路径变量错误之前,我遇到了同样的问题。
I fixed it the following way.
我通过以下方式修复了它。
First, get the current PATH by pasting this in terminal (without first $):
首先,通过将其粘贴到终端中来获取当前 PATH(没有第一个 $):
$ echo $PATH
Copy this path (it will look something like this: /opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin)
复制这条道路(它看起来是这样的:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin)
Now edit the build system to include this path (the pathvariable you use should be actually a working_dir):
现在编辑构建系统以包含此路径(path您使用的变量实际上应该是 a working_dir):
{
"cmd" : ["exec_sql.sh", "$filename"],
"selector" : "source.sql",
"working_dir" : "/Users/username/sql",
"path": "PASTE $PATH VALUE HERE"
}
}
And it should work now. At least it helped me.
它现在应该可以工作了。至少它帮助了我。

