Im trying to write a script to invoke nagios. In order to do this I grep some words that comes from output of some backup scripts. When there is "End-of-tape detected" in directed output logs it should give alarm. First I would like to know if there is any better way to write comparision loops. Second if the alarm condition occurs in a loop there is no need to run the next loops so how can i break loops and end the script right way?
find . -name ufsdump_output1.txt | xargs egrep "End-of-tape detected" | wc -l > word_count1.txt
###i used wc-l to detect existance of the words "End-of-tape detected" , i know i could do it with awk but coudlnt be sure###
NUM1 = cat word_count1.txt
if [ $NUM1 = 0 ]; then
echo "TAPE HAS STILL SPACE FOR BACKUP" exit 0
elif [ $NUM1 != 0 ]; then
echo "ATTENTION! EOT REACHED, CHANGE CARTRIDGE!" exit 2
fi
find . -name ufsdump_output2.txt | xargs egrep "End-of-tape detected" | wc -l > word_count2.txt
NUM2 = cat word_count2.txt
if [ $NUM2 = 0 ]; then
echo "TAPE HAS STILL SPACE FOR BACKUP" exit 0
elif [ $NUM2 != 0 ]; then
echo "ATTENTION! EOT REACHED, CHANGE CARTRIDGE!" exit 2
fi
find . -name ufsdump_output3.txt | xargs egrep "End-of-tape detected" | wc -l > word_count3.txt
NUM1 = cat word_count3.txt
if [ $NUM3 = 0 ]; then
echo "TAPE HAS STILL SPACE FOR BACKUP" exit 0
elif [ $NUM3 != 0 ]; then
echo "ATTENTION! EOT REACHED, CHANGE CARTRIDGE!" exit 2
fi
I'd like to find out your thoughts,
thanks by advanced
Nope, i tried the commands and Manuel was right unfortunately so bash only allows break to be used in while and for-do loops not if-else loops. Im gonna change the loops as while.
result was
./break.sh: line 9: break: only meaningful in a `for', `while', or `until' loop
I don't get why you need to break. Once the condition is evaluated the script goes on
might this be helpful
#!/bin/bash
for n in {1..3}; do
eval NUM$n=$(egrep -R -c "End-of-tape detected" .)
case NUM$n; in
0) echo "TAPE HAS STILL SPACE FOR BACKUP"
exit
;;
[1-9]|[1-9]0-9]*) echo "ATTENTION! EOT REACHED, CHANGE CARTRIDGE!"
exit 2
;;
esac
done
Instead of 'exit'ing, you can assign a new variable, which you could use to define other actions
#!/usr/bin/bash
MAXIDX=3
IDX=1
DONE=1
while [ ${IDX} -le ${MAXIDX} -a ${DONE} -eq 1 ]
do
if [ `find . -name ufsdump_output${IDX}.txt -exec grep -il "End-of-tape detected" {} \; | wc -l` -gt 0 ]
then
DONE=0
echo "ATTENTION! EOT REACHED FOR INDEX ${IDX}, CHANGE CARTRIDGE!"
fi
((IDX=${IDX}+1))
done
---------- Post updated at 10:07 PM ---------- Previous update was at 10:03 PM ----------
The usage of "break" in a loop is a sign of weakness, like saying "I am not able to come up with a decent solution." A loop is supposed to have a proper end condition.