views:

115

answers:

2

I have a folder full of *.command files on my OS X workstation.

(For those that don't know, *.command files are just shell scripts that launch and run in a dedicated Terminal window).

I've dragged this folder onto my Dock to use a "stack" so I can access and launch these scripts conveniently via a couple clicks.

I want to add a new "run-all.command" script to the stack that runs all the *.command files in the same stack with the obvious exception of itself.

My Bash chops are too rusty to recall how you get a list of the *.command files, iterate them, skip the file that's running, and execute each (in this case I'd be using the "open" command so each *.command opens in its own dedicated Terminal window).

Can somebody please help me out?

+2  A: 

How about something like this:

#! /bin/bash                                                                    

for x in ./*
do
        if [ "$x" != "$0" ]
        then
            open $x
        fi
done

where $0 automatically holds the name of the script that's running

bbg
I don't know OS/X very well, but what's the "open" for?
reinierpost
There are a couple issues here. ./* will include ./ on the front of all entries, while `$0` expands to the full name the script was called as, which might well be an absolute path. Your best bet is probably to wildcard simply with `*`, and use `$(basename $0)` instead of `$0` (store it in a variable first).
Jefromi
You don't need to store it in a variable and you can continue to specify the current directory. So, without using a variable, you'd have `if [ "$(basename $x)" != "cpmd~" ]`. You might want to do `for x in ./*.command` to be even more specific and use `open "$x"` to protect spaces if they are in a filename.
Dennis Williamson
@reinierpost the "open" command essentially tells OS X to "run this as if it were launched from the dock" so you can start applications from the command line, and in this case those applications are shell scripts that launch through new Terminal windows.
Teflon Ted
+3  A: 

Using @bbg's original script as a starting point and incorporating the comments from @Jefromi and @Dennis Williamson, and working out some more directory prefix issues, I arrived at this working version:

#!/bin/bash
for x in "$(dirname $0)"/*.command
do
  if [ "$(basename $x)" != "$(basename $0)" ]
  then
    open "$x"
  fi
done
Teflon Ted