1755699 Members
2966 Online
108837 Solutions
New Discussion юеВ

help in script

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

help in script

Guys,

I have posted below a section of script that checks changes in cron ....

The script checks for added or removed schedules and reports to the sysadmin however I cannot configure without it getting messy to run the check agains current crons then the check against removed crons:

ls $LIVE_CRON |grep -v "^d" |grep -v ".tar" |grep -v ".sav" |sort -n > $LIVELIST
ls $WORKCRON |grep -v "^d" |grep -v ".tar" |grep -v ".sav" |sort -n > $KNOWNLIST

DIFFCHK=`diff $LIVELIST $KNOWNLIST |tail +2 |wc -l`

if [ $DIFFCHK -gt 0 ] ; then

for a in `diff $LIVELIST $KNOWNLIST |tail +2|sed 's/ /:/g'`
do

DIFF=`echo $a |awk -F: '{print $1}'`
SCHED=`echo $a |awk -F: '{print $2}'`

if [ "$DIFF" = ">" ] ; then

echo "Cron schedules removed ...... \n$SCHED\n" >> $OUTFILE
rm -f $WORKCRON/$SCHED
cat $LIVELIST > $KNOWNLIST
echo "\n" >> $OUTFILE

elif [ "$DIFF" = "<" ] ; then

echo "Cron schedules that have been added ......\n$SCHED\n" >> $OUTFILE
cp $LIVE_CRON/$SCHED $WORKCRON
cat $LIVELIST > $KNOWNLIST
echo "\n" >> $OUTFILE

fi

done

fi

basically if I remove a cron then the first bit works but then doesn't run the second 'if' statement.

any idea's please?

Thanks
hello
5 REPLIES 5
Peter Godron
Honored Contributor
Solution

Re: help in script

Hi,
So
if [ $DIFFCHK -gt 0 ]; then
works, but the if statement
if [ "$DIFF" = ">" ]; then
does not work ?

Put a debug statement displaying the variable variables values for $a, $DIFF, $SCHED with in the loop, but before the failing if statement.


lawrenzo_1
Super Advisor

Re: help in script

nope

if [ $DIFFCHK -gt 0 ] ; then
etc
etc

if [ "$DIFF" = ">" ] ; then

echo "Cron schedules removed ...... \n$SCHED\n" >> $OUTFILE
rm -f $WORKCRON/$SCHED
cat $LIVELIST > $KNOWNLIST
echo "\n" >> $OUTFILE

works but wont contiue to elif ...

I am pretty sure the elif is the wrong statement so what else can I use?

many thanks
hello
John Kittel
Trusted Contributor

Re: help in script

... not sure I understand, but

what if you just change the elif to an if. For example:

if [ "$DIFF" = ">" ] ; then

echo "Cron schedules removed ...... \n$SCHED\n" >> $OUTFILE
rm -f $WORKCRON/$SCHED
cat $LIVELIST > $KNOWNLIST
echo "\n" >> $OUTFILE

fi

if [ "$DIFF" = "<" ] ; then

echo "Cron schedules that have been added ......\n$SCHED\n" >> $OUTFILE
cp $LIVE_CRON/$SCHED $WORKCRON
cat $LIVELIST > $KNOWNLIST
echo "\n" >> $OUTFILE

fi
OldSchool
Honored Contributor

Re: help in script

Nope...the if/elif logic illustrated should be ok.

note that you don't need the "tail" when creating the original list, as you are only processing those lines where "DIFF" is "<" or ">", the remainder of the diff output is read and tossed out.

the "cat"s are going to happen multiple times, which is probably an error.

the "<" and ">" are going to be interspersed in the output of the diff, it looks like (from the header echos) that you expect to get all of the adds and all of the removes grouped together.

I'd add set +x to your script, along w/ Peter's suggestion.
lawrenzo_1
Super Advisor

Re: help in script

got it, debugged anworked it out ...

Thanks for the suggestions chaps
hello