Max Yankov
Max Yankov

Reputation: 13297

Build system with bash script for Sublime Text 2

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.

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:

{
    "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:

/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?

Upvotes: 3

Views: 7329

Answers (4)

DataGreed
DataGreed

Reputation: 13899

It seems like you are on Mac. I had the same problem before I realized that the System path variable is wrong.

I fixed it the following way.

First, get the current PATH by pasting this in terminal (without first $):

$ 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)

Now edit the build system to include this path (the path variable you use should be actually 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.

Upvotes: 0

Greg Burghardt
Greg Burghardt

Reputation: 18783

I was able to create a general purpose Bash build system for Sublime Text 2:

  1. Create a new Build System
  2. 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"]
        }
    }
    
  3. Save it as bash.sublime-build

  4. 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 $1/build.sh ]; then
        cd $1
        build.sh
        cd -
    else
        echo "File doesn't exist: $1/build.sh"
    fi
    
    echo "Done."
    
    exit $?
    

    Save it as bash-build.sh in the same folder that you reference in the Sublime Build System

  5. Open a folder in Sublime (note: on Windows, build systems seem broken when Sublime is opened from the command line)

  6. Create a new text file and save it as build.sh into the root folder opened in Subilme

  7. In build.sh, go crazy:

    #!/bin/env bash
    
    # do some crazy, custom build
    
    exit $?
    

I've started using this in conjunction with Pandoc to generate HTML files from Markdown files.

Upvotes: 0

DiCaprio
DiCaprio

Reputation: 833

If you don't want to create a symlink use a variable like $project_path or $file_path

{
    "shell_cmd": "./do.sh",
    "shell": true,
    "working_dir": "$project_path"
}

reference for variables: sublimeText docs

Upvotes: 0

aanton
aanton

Reputation: 5572

I think that the problem can be solved if your custom script is added to the system path (to the BIN directory).

I am going to use a detailed example that i have used succesfully.

  1. 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 $1
    basename $1
    
  2. 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
    
  3. Check the script in the console in any directory

    cd /tmp
    test_basename.sh some-filename
    
  4. Restart Sublime Text

  5. 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"]
    }
    
  6. Open a file in Sublime Text and run the build system created.

Upvotes: 3

Related Questions