LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   need run-parts run in reverse. (http://www.linuxquestions.org/questions/linux-general-1/need-run-parts-run-in-reverse-517424/)

jukemann 01-08-2007 10:51 AM

need run-parts run in reverse.
 
I understand that run-parts run scripts in a directory one after another in abc order. I'm not very good with bash but I need a script to run in reverse order instead.

This is the script for run-parts any help would be greatly appreciated.
Quote:

# cat /usr/bin/run-parts
#!/bin/bash

# run-parts - concept taken from Debian

# keep going when something fails
set +e

if [ $# -lt 1 ]; then
echo "Usage: run-parts <dir>"
exit 1
fi

if [ ! -d $1 ]; then
echo "Not a directory: $1"
exit 1
fi

# Ignore *~ and *, scripts
for i in $1/*[^~,] ; do
[ -d $i ] && continue
# Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
[ "${i%.rpmsave}" != "${i}" ] && continue
[ "${i%.rpmorig}" != "${i}" ] && continue
[ "${i%.rpmnew}" != "${i}" ] && continue
[ "${i%.swp}" != "${i}" ] && continue
[ "${i%,v}" != "${i}" ] && continue

if [ -x $i ]; then
$i 2>&1 | awk -v "progname=$i" \
'progname {
print progname ":\n"
progname="";
}
{ print; }'
fi
done

exit 0

unSpawn 01-08-2007 11:08 AM

Easier instead would be to rename your current jobs in reverse order 0 to 9, a to z. Added advantage is it doesn't break compat with run-parts.

PTrenholme 01-08-2007 11:21 AM

Try changing the line for i in $1/*[^~,] ; do to

for i in `echo $1/*[^~,] | sort -dr` ; do

or

for i in `ls -1r | grep -r '[^~,]$'` ; do

See man sort and man ls for more details about those commands.

You might also want to look at the find command as an easier way to do what you want.

And, of course, the gawk language, since the whole of the script you displayed could be accomplished in a gawk program, with even more flexability.

Notes:
1) I've not tried either of these, although I think that they'll work.
2) Note the use of the back-quotes to tell bash to use the command output.

PTrenholme 01-08-2007 10:31 PM

Quote:

Originally Posted by unSpawn
Easier instead would be to rename your current jobs in reverse order 0 to 9, a to z. Added advantage is it doesn't break compat with run-parts.

Or use the method used by some distributions to specify the order in which system services should be started. That is, put the scripts in a directory (or leave them scattered all over the place), and create another directory with symbolic links to the scripts to be run using names (usually starting with a number are a single letter and a number with leading zeros) that, when ordered, specify the proper sequence.

For example, on Fedora the service startup scripts are kept in /etc/init.d/, and the links to those scripts -- for each run level -- are kept in /etc/rc.d/rc[0-6].d. Fedora actually uses two different single letter prefixes -- "K" for "kernel" services (loaded before the kernel is started) and "S" for "system" services (loaded after the kernel is started), but that's probably an irrelevant detail.

PTrenholme 01-10-2007 10:41 AM

Another thought: Create a list of the scripts you wish to run, and then run them in the order in which they're listed in the file.

For example, replace the line for i in $1/*[^~,];do with
Code:

if [ $# -gt 1 -a -r "$2" ]; then
  list=$2;
  rm="no";
else
  list=`mktemp -t list.XXXX`;
  rm="yes";
  echo $1/*[^~,] > $list;
fi
for i in `cat $list`; do
.
.
.
done
[ "$rm" = "yes" ] && rm $list;

The thing with Linux is, there's always more than one way to solve a problem. :cool: And it's fun thinking up new ways.:study:


All times are GMT -5. The time now is 11:19 PM.