change if-else loop to while-do


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers change if-else loop to while-do
# 1  
Old 08-31-2009
change if-else loop to while-do

Hello friends,

i wrote a script which includes a couple of if-else loops but i have to change it to while-do loop as it is not allowed to use "break" in if-else loop in bash.

Code:
#!/bin/bash -x
NUM=`find . -name ufsdump_output1.txt | xargs egrep "End-of-tape detected"`
if [ "$NUM" = "" ]; then  echo "OK!"
elif [ "$NUM" = "End-of-tape detected" ]; then echo "NOT OK!"  exit 2
fi

i coudlnt conclude the script so i'd like to have your support pls:

Code:
while [ "$NUM" = "" ];
do echo "OK!"
else ......  ##what "elif" part corresponds to? in while-do loop?##

thanks by advanced
# 2  
Old 08-31-2009
There is not an else or elif for a while do loop, but you can simply do:
Code:
while [ "$NUM" = "" ];do
   echo "OK!"
done
if [ "$NUM" = "End-of-tape detected" ]; then
  echo "NOT OK!"
  exit 2
fi


Last edited by TonyFullerMalv; 08-31-2009 at 11:19 AM.. Reason: exit 2 on its own line.
# 3  
Old 08-31-2009
yes its a solution but i have a couple of these loops and the problem is when "NOT OK" condition happens i should end the script by using "break" without considering the next parts of script, however i cant use "break" as "shell" does not allow using "break" instruction in if-else loops Smilie Thats why i tried to convert my loops to while-do. hmm i should think about another solution mate, thx
# 4  
Old 08-31-2009
"Plan B" Smilie

Code:
while true
do
  case "$NUM" in
    "")
      echo "OK"
    ;;
    "End-of-tape detected")
      echo "KO"
      break 
    ;;
  esac
done

(Reference)
# 5  
Old 09-01-2009
Dr. i tried the script, first part of it is like (other parts same) :

Code:
NUMA=`find . -name ufsdump_output1.txt | xargs egrep "End-of-tape detected"`
while true do
  case "$NUMA" in
    "")
      echo "OK"
    ;;
    "End-of-tape detected")
      echo "KO"
      break
    echo "FOR TEST"
    ;;
  esac
done

and i have txt files for script to use :
Code:
-rw-r--r--   1 root     root         373 Aug 31 11:50 ufsdump_output1.txt
-rw-r--r--   1 root     root         943 Aug 31 11:51 ufsdump_output2.txt
-rw-r--r--   1 root     root         373 Aug 31 11:52 ufsdump_output3.txt

but when i run script it prints endless "OK" to stdout; screen?? which part i should modify, it seems it does not consider the other loops.
# 6  
Old 09-01-2009
Quote:
Originally Posted by EAGL€
Code:
while true do

Ad hack, er, hoc ... Smilie

Code:
while [ $NUMA ] do

# 7  
Old 09-01-2009
Code:
#!/bin/bash

for n in {1..3}
 do
#   eval NUM$n=$(find . -name ufsdump_output${n}.txt | xargs egrep -c "End-of-tape detected")
   eval NUM$n=$(egrep -Rc "End-of-tape detected" .)
   case NUM$n
    in
      0) echo "TAPE HAS STILL SPACE FOR BACKUP"
         exit #or what you have to do
      ;;
      [1-9]|[1-9][0-9]*) echo "ATTENTION! EOT REACHED, CHANGE CARTRIDGE!"
                        exit 2
      ;;
   esac
done

HTH
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change argument in a for loop

In a "for i in *FD.CPY do" loop, I need to change .CPY to .layout so the executed command would be reclay blahFD.CPY >blahFD.layout What do I need to do to modify a copy of i to use after the > symbol? TIA (5 Replies)
Discussion started by: wbport
5 Replies

2. UNIX Desktop Questions & Answers

Change name of files to their paths -- find loop

Dear All, I have many sub-folders but each of them have a file with same name but different data. I want to either move or copy them into a new folder but they need to have the path of where they are coming as part of their name... I have managed to find the files but dont know how to change... (2 Replies)
Discussion started by: A-V
2 Replies

3. Shell Programming and Scripting

Doing a loop to change a value in a file

Hi, I have an N number of files in a directory. I like to write a shell script that would make identical plots for each one of these files. The files have names such as: t00001.dat t00002.dat t00003.dat t00004.dat t00005.dat . . . t00040.dat i.e. the... (4 Replies)
Discussion started by: lost.identity
4 Replies

4. Shell Programming and Scripting

Change a field value in a loop

Hi guys, i have an executable file that contains several records and fields. One of the records has a variable filed that must be changed each time i want to execute the file. Would it be possible that i can use a loop to change the value of that field? Suppose that the field address is: Record... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

5. Shell Programming and Scripting

Variable value dosent change after the loop changes it

i am new to shell scripting. this is similar to the code which i was writing for some other work. i want the variable 'x' to have the value which it will finally have at the end of the loop ( the number of directories ). but the value of 'x' only changes inside the loop and it remains '0' out-side... (3 Replies)
Discussion started by: chathura666
3 Replies

6. Shell Programming and Scripting

Variable name change in a loop

I need to do something like this:for I in var1 var2 var3 ; do $I = "Something calculated inside the loop" doneObviously it doesn't work...but is it possible doing that in other ways? Thanks in advance. (6 Replies)
Discussion started by: canduc17
6 Replies

7. Shell Programming and Scripting

loop through file to change some data

Hi, I have a file with the following data -0.00000 0.00000 0.00000 F F F 0.00000 0.00000 0.80000 F F F 0.50000 0.50000 0.60000 F F F 0.50000 0.50000 0.20000 F F F -0.00000 0.00000 0.40000 F F F I would like to change the last 3 lines from F F F to T T T. I tried looping each line but don't... (5 Replies)
Discussion started by: princessotes
5 Replies

8. Shell Programming and Scripting

'while' loop does not change local variables?!

(I think this question desearves separate thread..) I have a problem with 'while' I am trying to set variables by 'while' and it is fine inside, but after completting the loop all changes are lost: > bb="kkkk - 111\nlllll - 22222\nbbbb - 4444" > echo "$bb" kkkk - 111 lllll - 22222 bbbb -... (3 Replies)
Discussion started by: alex_5161
3 Replies

9. Shell Programming and Scripting

help with a 'while read' loop to change the names of files

Hi, I am new to scripting, so any help on this would be much appreciated. I am trying to rename a bunch of files, taking the names sequentially from a list read in another file... # ls oldnames file_1 file_2 file_3 # cat names red yellow green I want the files to take on the... (6 Replies)
Discussion started by: starsky
6 Replies
Login or Register to Ask a Question