Launching bash script

General discussions about LaunchBar
Post Reply
iosdude
Posts: 3
Joined: Fri Jul 12, 2013 11:24 pm

Launching bash script

Post by iosdude » Fri Jul 12, 2013 11:32 pm

Hi,

I've seen several threads asking about running shell scripts and have tried all of the suggestions, to no avail.

I have a shell script, foo.sh, in my home folder (~/foo.sh).

Here is the output from ls (so we know that it is executable):

Code: Select all

-rwxr-xr-x   1 Me  staff   296 Jul 10 15:48 foo.sh*

The first line of the script is:

Code: Select all

#!/usr/bin/env sh

When I try to run the script using LB, it opens in Xcode - the script itself doesn't execute.

What am I missing?

Thanks!
io

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Launching bash script

Post by sjk » Mon Jul 15, 2013 5:50 am

Try removing the .sh filename extension from the script. Xcode is the default app for opening files with a .sh extension and LB is processing your ~/foo.sh script that way instead of honoring its shebang.

In a test here, when using LB to run a shell script named ~/bin/om-find (with a #!/bin/sh shebang and no extension) the expected output was displayed in a newly-opened Terminal window. Adding the .sh extension causes it to be opened with Xcode, like with the script on your system.

At least on my systems, Terminal is the default app for executable files without extensions. In LB, a small Terminal icon before the filename identifies them. Scripts that'll open with Xcode have generic icons, with a right pointing triangle indicating that Right-Arrow subitem navigation is possible (which, in this case, displays the script text).

You can also invoke File > Get Info (Command-I) on file/folder items in LB to open a Finder Info window and see the "Open with:" app that LB will use (when there is one).

iosdude
Posts: 3
Joined: Fri Jul 12, 2013 11:24 pm

Re: Launching bash script

Post by iosdude » Mon Jul 15, 2013 1:20 pm

sjk,

Thank you SO much - that solved my problem. I am aware that *.sh files are associated with Xcode (on my system anyway) but I expected LB to execute the script instead of opening it.

Of course, now that it is running the way I want a new Terminal window opens up and remains open after the shell script completes. Is there a way to have the Terminal window close after my shell script completes?

Thanks again for your help!!

-io

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Launching bash script

Post by sjk » Mon Jul 15, 2013 9:46 pm

iosdude wrote:Thank you SO much - that solved my problem.

Cool. Thanks for letting me know.

I am aware that *.sh files are associated with Xcode (on my system anyway) but I expected LB to execute the script instead of opening it.

Maybe related to LB indexing using Launch Services? The only control over that is in Options for the Applications indexing rule; norbert's post describes how to disable it:

Re: Index folders

I don't feel like testing if that changes LB's behavior with scripts/executables but am curious of the result if you do. :)

Note to ObjDev: "Use Mac OS X LaunchServices database" and "Prefetch application icons" options aren't documented in LB Help.

Btw, filename extensions are near the top of my list of my most undesirable things about OS X. I think their usage is negatively retrograde, prolonging an inelegant machine-centric data identification method that's overdue to be obsoleted. Makes me wince whenever they seem to have permanently infected someone's vocabulary.

Is there a way to have the Terminal window close after my shell script completes?

The easiest way I know to do it is to select the script in LB, press Space, and enter ;exit as the string parameter. Whether the window actually closes depends on the "When the shell exits:" option under the Shell tab of Terminal > Preferences > Settings for the Default profile (or whichever is used when a new window opens). For example, if it's "Don't close the window" the window will remain open with "[Process completed]" displayed. "Close if the shell exited cleanly" can be a preferable choice.

There's also this, under Running Unix Executables in LB Help:

Running in background

By default, LaunchBar runs Unix Executables and Shell Scripts in a newly opened Terminal window. You can optionally run them in background (without any window) by opening them with Option-Return.

iosdude
Posts: 3
Joined: Fri Jul 12, 2013 11:24 pm

Re: Launching bash script

Post by iosdude » Tue Jul 16, 2013 2:51 pm

Thanks sjk!!! This is exactly what I needed to know!

-io

sjk
Wizard
Wizard
Posts: 315
Joined: Thu Sep 13, 2007 9:46 pm
Location: Eugene
Contact:

Re: Launching bash script

Post by sjk » Wed Jul 17, 2013 5:40 am

Excellent.

Post Reply