0

Running MacOS High Sierra 10.13.5

Good day! Today I attempted to write a script in my .bash_profile to allow me to call Sublime Text with a single command, sublime.

Here is the script I produced with my limited knowledge of bash as well as some research:

sublime_open()     # Function which opens the first argument as a text file in Sublime.
{
    open -a /Applications/Sublime\ Text.app < $1.txt;
}

export -f sublime_open;

sublime_test()     # Function which will open first argument as a text file in Sublime,
{                  # creating one if it does not exist in the current directory.
    if [ -e "$1" ];
    then sublime_open "$1"
    else
    touch "$1".txt
    sublime_open("$1")
    fi
}

export -f sublime_test
alias sublime="sublime_test"
export sublime  


I separated the function into two pieces for the sake of readability.


Expected Behavior:

sublime foo opens an instance of Sublime to the file foo.txt, creating that file if it does not already exist.


Observed Behavior:

Bash returns:

line 29: syntax error: unexpected end of file

Note that line 29 is the line after the file's final line, which does not exist.


Attempts at solution:
So far I have searched for solutions here and here.
I have tried:

  • Copying over to a new bash profile and deleting the old one to remove "ghost" characters
  • Running dos2unix on my .bash_profile
  • Parsing the script myself looking for individual errors, the problem seems to occur in the sublime_open() portion.

Sorry if my script is elementary or uninformed, this was my first attempt at writing a script that behaves this way. I welcome help or tips outside of the scope of my issue.

Thanks in advance!

Edit: Fixed misused heredoc as per @jwodder's solution as well as improper function calls as per @codeforester's solution.

Edit 2: If you are looking for a more elegant solution to this same problem, have a look at this very helpful gist.

  • 1
    Shell functions are invoked as regular commands, like `func arg1 arg2 ...` and not `func(arg1, arg2, ...)`. – codeforester Jun 19 '18 at 14:15
  • 2
    Get your code checked at [shellcheck](https://shellcheck.net). – codeforester Jun 19 '18 at 14:16
  • 1
    And your heredoc doesn't have an end marker. – codeforester Jun 19 '18 at 14:16
  • Have you tried using "subl" command, which is available post SublimeText Installation on all Operating Systems (you may need to set the path for binary though, to run this command from any path/folder)? – Amit Verma Jun 19 '18 at 14:35
  • Note that semicolons and newlines are effectively the same thing in bash. There's no reason to put a semicolon at the end of every line, as the line ending itself already terminates each statement. – jwodder Jun 19 '18 at 14:36
  • 1
    @codeforester Just fixed my calls to match this syntax. Thanks for both the link and that tip! – Hunter S. DiCicco Jun 19 '18 at 14:43
  • @AmitVerma This was more a coffee break project to see if I could do it, I didn't even realize I had `subl` at my disposal too. Thanks! – Hunter S. DiCicco Jun 19 '18 at 14:45

2 Answers2

1

The problem is this line:

open -a /Applications/Sublime\ Text.app << $1.txt;

I assume that what you mean for this to do is to feed the contents of $1.txt to Sublime Text on standard input. However, that would be done with one <, not two. With two <, you get a heredoc, which means that bash will look for the next line containing just $1.txt and will use all lines in between as the input to Sublime Text. As the file ends before bash finds a line with just $1.txt, you get an "unexpected end of file" error.

jwodder
  • 54,758
  • 12
  • 108
  • 124
-1

Keep in mind that on macOS, an *.app is just a folder. Generally speaking, you need to specify the location to a binary (or script). In the case of Sublime Text you have to use the Sublime Text CLI tool:

/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

See the OS X Command Line documentation for details.

idleberg
  • 12,634
  • 7
  • 43
  • 70
  • The OP's code is using the Sublime Text `*.app` as the value of the `-a` option to `open`, which is how apps are supposed to be used on the command line. It also has nothing to do with the syntax error they're getting. – jwodder Jun 19 '18 at 14:41