Breaking if-else loop and variety of comparisions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking if-else loop and variety of comparisions
# 1  
Old 08-31-2009
Breaking if-else loop and variety of comparisions

Hello Friends,

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?

Code:
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

Last edited by EAGL€; 08-31-2009 at 04:38 AM.. Reason: i deleted a redundant variable in script.
# 2  
Old 08-31-2009
Hello EAGL€,

Quote:
Originally Posted by EAGL€
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?
Just the instruction break

Code:
NUM1 = cat word_count1.txt

Should be
Code:
NUM1=$(cat word_count.txt)

And I am sure there is a way to optimise your code. Might be worthwhile if you have many large files to process.
# 3  
Old 08-31-2009
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
Code:
./break.sh: line 9: break: only meaningful in a `for', `while', or `until' loop

thanks anyhow..
# 4  
Old 08-31-2009
Of course. break only works on loops and if..else is a conditional structure that does not loop.
# 5  
Old 08-31-2009
I don't get why you need to break. Once the condition is evaluated the script goes on

might this be helpful
Code:
#!/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
# 6  
Old 08-31-2009
Quote:
Originally Posted by EAGL€
Hello Friends,

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?

Code:
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
Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to parse a variety of formats

The below perl script parses a variety of formats. If I use the numeric text file as input the script works correctly. However using the alpha text file as input there is a black output file. The portion in bold splits the field to parse f or NC_000023.10:g.153297761C>A into a variable $common but... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

Ssh to remote server loop is breaking

hi All, cat login.list server1 userid1 server2 userid2 server3 userid3 ---------------------------------------- #SSHSCRIPT.ksh FILE=login.list while read vah vah_id do ssh $vah -l $vah_id "pwd" done < "$FILE" ----------------------------------------- When i... (2 Replies)
Discussion started by: raghur77
2 Replies

4. Shell Programming and Scripting

breaking for loop

Dear Friends, Here I need your guidance once again. I have for loop which check all files in a folder for a particular string. If the string is found in a file it returns value other than 0 else returns 0 value in variable t2. At times the string which we are looking for is in first file... (1 Reply)
Discussion started by: anushree.a
1 Replies

5. Shell Programming and Scripting

Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop. Let me explain with example here: I... (6 Replies)
Discussion started by: vpv0002
6 Replies

6. Shell Programming and Scripting

Breaking Loop by using another script

Hi friends, I have 2 scripts. 1) Master_Script.sh and 2) Sub_script.sh We run Master_script.sh manually where as sub_script.sh keeps generating output in every 2 minutes (through crontab). The output generated by sub_script.sh can be 0 or 1. As I told you, sub-script.sh keeps generating o/p... (7 Replies)
Discussion started by: anushree.a
7 Replies

7. Linux

breaking out of while loop

Hi , I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other... (3 Replies)
Discussion started by: capri_drm
3 Replies

8. Shell Programming and Scripting

rsh breaking me out of loop

Hey all I have two scripts, one script containing the guts of my code. The other simply loops through a list, calling the other script on each iteration. Problem is when I add the line `/usr/bin/rsh -l root $HOSTNAME ""` to my main script, the loop never seems to exectute any more... (1 Reply)
Discussion started by: mark007
1 Replies

9. Windows & DOS: Issues & Discussions

print image files to variety printer models

Hi, I am currently working on a windows platform (2000 and XP) and was wondering if there are today solutions for the task I have. I need to print image files onto a variety of inkjet printer models, most epson non-postscript. Some of the models I know but new models are added almost every... (0 Replies)
Discussion started by: jokofix007
0 Replies

10. Shell Programming and Scripting

Floating point numeric comparisions in bash

Hi, I am trying to compare 2 floating point numbers 0.8 and 0.15 using bash and get the largest of the two. Can anyone advise. (2 Replies)
Discussion started by: borncrazy
2 Replies
Login or Register to Ask a Question