Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run every script in a directory except itself?

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?

like image 236
Teflon Ted Avatar asked Oct 04 '09 21:10

Teflon Ted


People also ask

How do I move everything except one directory in Linux?

The first is -maxdepth 1 which prohibits reiteration to other folders. The second is -mindepth 1 which prevents the source folder from being included in the results to be transferred. The third option is the -not -name unwanted_filename option that uses the unwanted filename as an index to list the remaining content.

How do I move everything except one file in Linux?

A quick way would be to modify the tux filename so that your move command will not match. don't use ".", as that will only move files that have a "." in the name. Just "*" is enough.

What is $@ in script?

$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do I run a script in a folder?

We can run all scripts in a directory or path using "run-parts" command. The run-parts command is used to run scripts or programs in a directory or path. One disadvantage with run-parts command is it won't execute all scripts. It will work only if your scripts have the correct names.


2 Answers

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

like image 115
bbg Avatar answered Nov 01 '22 12:11

bbg


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
like image 44
Teflon Ted Avatar answered Nov 01 '22 13:11

Teflon Ted