]> git.pld-linux.org Git - projects/rc-scripts.git/blob - run-parts
e67c5033064c5616a72f08185d337800a143e7b4
[projects/rc-scripts.git] / run-parts
1 #!/bin/sh
2
3 # run-parts - concept taken from Debian
4 #
5 # modified for PLD Linux by Pawel Wilk <siefca@pld-linux.org>
6 #
7 # NOTE:
8 #       1.) run-parts is now able to get arguments!
9 #       2.) relative pathname of the invoked directory can be
10 #           obtained by reading RUNPARTS_DIR env. variable
11 #       3.) absolute pathname of the invoked directory can be
12 #           obtained by reading RUNPARTS_ADIR env. variable
13 #
14
15 # keep going when something fails
16 set +e
17
18 if [ "$1" = "-u" ]; then
19         workasuser=yes
20         shift
21 fi
22
23 if [ "$1" = "--test" ]; then
24         test=yes
25         shift
26 fi
27
28 if [ "$1" = "--" ]; then
29         shift
30 fi
31
32 # std checks
33 if [ $# -lt 1 ]; then
34         echo "Usage: run-parts [-u] [--test] <dir> <args...>"
35         exit 1
36 fi
37
38 if [ ! -d $1 ]; then
39         echo "Is not a directory: $1"
40         echo "Usage: run-parts [-u] [--test] <dir> <args...>"
41         exit 1
42 fi
43
44 # assign passed dir name
45 RUNPARTS_DIR=$1
46
47 # assign absolute dir name
48 olddir=$(pwd)
49 cd $RUNPARTS_DIR
50 RUNPARTS_ADIR=$(pwd)
51 cd $olddir
52 unset olddir
53
54 # export directories for our descendants
55 export RUNPARTS_ADIR RUNPARTS_DIR
56
57 # shift args
58 shift
59
60 # Ignore *~ and *, scripts
61 for i in $RUNPARTS_DIR/*[!~,] ; do
62         [ -d "$i" ] && continue
63         # Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
64         [ "${i%.rpmsave}" != "${i}" ] && continue
65         [ "${i%.rpmorig}" != "${i}" ] && continue
66         [ "${i%.rpmnew}" != "${i}" ] && continue
67         [ "${i%.swp}" != "${i}" ] && continue
68         [ "${i%,v}" != "${i}" ] && continue
69
70         if [ -x "$i" ]; then
71                 runprog="$i $@"
72                 if [ "$test" = yes ]; then
73                         echo "$runprog"
74                         continue
75                 fi
76                 if [ "$workasuser" = "yes" ]; then
77                         runuser="$(/bin/ls -l "$i" | awk ' { print $3 } ' 2> /dev/null)"
78                         [ -z "$runuser" ] && echo "Warning: Can't find owner for [$i] file. Not running." && continue
79                         runprog="/bin/su $runuser -s /bin/sh -c $runprog"
80                 fi
81                 $runprog 2>&1 | awk -v "progname=$i" \
82                         'progname {
83                                 print progname ":\n"
84                                 progname="";
85                                 }
86                         { print; }'
87         fi
88 done
89
90 exit 0
91
92 # This must be last line !
93 # vi:syntax=sh
This page took 0.027894 seconds and 2 git commands to generate.