script file naming conventions & behaviour from gui

(I'm sure there is documentation out there for this topic somewhere, but searches are next-to useless because the words are all so common... ".command", script, etc...)

I'm accustomed to writing legit cli programs in xcode, but I occasionally just want to slap down a few lines of shell script and make it executable. I'm wondering what filename extension(s) to use and, (since I'm used to shell commands, but new to script files), what else there is to know. I've read that with, say, ...sh scripts, in addition to an appropriate hashbang in the script, '.sh' would be an appropriate file-name extension. I've seen and tried using 'foo.command' though and can then run the script by double-clicking it as well as typing its path from a shell. Is '.command' really an OS X thing? Are there other tricks I should know or things to be careful about? Also, is there any way to run (non-interactive) shell scripts from Finder without them launching terminal?

Any links or straight advice appreciated. Thanks in advance.

Mac: Too Many To List, Mac OS X (10.4.3), Pretty much one of everything Apple sells

Posted on Jun 12, 2006 5:40 AM

Reply
3 replies

Jun 12, 2006 1:30 PM in response to Joel D. Reid

I thnk .command is an OS X Finder thing, where that filename extension is mapped to Terminal.app (just like .doc is mapped to Word, Terminal "opens" the file, hence the script runs). For stuff you actually use from the command-line, extensions don't matter. Some people say "use .sh for bash-like shell scripts", while others say "things one runs directly shouldn't have language extensions".

Dual 1.25 GHz G4 (among others) Mac OS X (10.3.9) Fink (obviously)

Jun 13, 2006 5:26 AM in response to Joel D. Reid

One way to run non-interactive scripts from Finder without them launching Terminal is to run them from within an AppleScript using it's "do shell script" command. For example:

do shell script "/path/to/shell/script"

or

do shell script "/path/to/shell/script arg1 arg2 etc"

You could also fairly easily write a generic "droplet" AppleScript that would allow you to drag-and-drop an arbitrary shell script onto it rather than hard coding a specific path into the script.

If your shell script generates output on stdout it will be returned to the AppleScript. You can recieve the output in an AppleScript variable and then display it or manipulate it in some way... like this:

set scriptOutput to do shell script "/path/to/shell/script"
-- code to parse or display "scriptOutput"

If the shell script is long running or redirects it's output to files then I believe you can have AppleScript run the shell script as a "background process". This will allow the "do shell script" command to return immediately so the AppleScript can quit.

do shell script "/path/to/shell/script &"

In all these examples the shell script will be run without launching Terminal or opening a Terminal window.

The file extensions you use are not particularly important, but the execute flag must be turned on as another poster has stated in order for the shell script to run.

Steve

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

script file naming conventions & behaviour from gui

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.