![]() |
James (Jim) G. Sack wrote this explanation of the program. The explanations are all covered in man bash or the bash reference manual.
# keep going when something fails
set +eNormally a shell script does not exit on a command error. The -e option forces exit on error executing any command not otherwise tested. So the +e option reverts to default just in case the -e was in effect when *this* shell was invoked. Hence the "keep going when something fails". Admittedly, the action of the "+" is easy to overlook in reading man bash.
if [ $# -lt 1 ]; then
echo "Usage: run-parts <dir>"
exit 1
fiif the argument-count (of args passed to this shell-script) is less than 1, then give a usage message and exit without doing anything else. The "fi" is the ending-keyword for the "if" command syntax. The semi-colon usage may be understood by giving the full syntax (ignoring elif/else variants) if condition; then command-list; fi plus explaining that semi-colons may be replaced with newines.
if [ ! -d $1 ]; then
echo "Not a directory: $1"
exit 1
fiif the first argument ($1) is not (!) a directory-name (-d), give an error message and exit.
for i in $1/* ; do
[ -d $i ] && continue
if [ -x $i ]; then
$i
fi
doneAll the "sanity checks" are done, now the real meat: For every file (call it i) in the specified (from arg 1) directory, if it's a subdirectory, do nothing (just continue on with the next filename), else (if it's executable), execute it. It may be of interest to note that the "for i in $1/*" gives fully-qualified filenames.
exit 0This is just good script-programming practice. Always ensure that some meaningful return-code is returned, whatever the exit-path. Since this is the "norma" (or "successful") return, the convention of returning 0 is appropriate. When this convention is observed, you can calls this script from within other commands and be sure of being able to test for success or failure - for this particular script, success is defined as having no syntax errors in the command args.