How to execute all scripts in a path?

Suppose I have a script that receives a path as a parameter. How could I execute all the scripts located in that path?


Assuming by path you meant path to a directory, use run-parts. From man run-parts:

run-parts - run scripts or programs in a directory

At first you need to set execute permission on all the scripts you want to run. Normally run-parts will ignore the directories and also the files that are not executable resides in that directory.

Although before running you should check which files will be run by the --test option:

run-parts --test /path/to/directory

I should mention that run-parts has a strict naming convention for the scripts to be executed:

If neither the --lsbsysinit option nor the --regex option is given 
then the names must consist entirely of ASCII upper- and
lower-case letters, ASCII digits, ASCII underscores, and ASCII minus-hyphens.

Check man run-parts to get more idea.


Run-parts will work if your scripts have the correct names. If you don't want to deal with renaming your scripts to fit run-parts's complex naming scheme, you could do something as simple as

for file in ~/target/*; do $file 2>/dev/null; done

That will attempt to execute all files (and directories) found in ~/target. The 2>/dev/null redirects error messages so it won't complain when attempting to run directories or non executable files.

Alternatively, you can try the more sophisticated

for file in ~/target/*; do
    [ -f "$file" ] && [ -x "$file" ] && "$file"
done

This will check whether each of the results is a file ([ -f $file ]), is executable ([ -x $file ]) and only if both those tests are successful will it attempt to execute the file.