LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Piping into a Case Statement embedded in a while loop (https://www.linuxquestions.org/questions/programming-9/piping-into-a-case-statement-embedded-in-a-while-loop-652940/)

telecom_is_me 07-01-2008 08:51 PM

Piping into a Case Statement embedded in a while loop
 
I'm attempting to pipe in a data stream to a shell script with a case statement inside of a while loop. The idea behind this is that I want to be able to test the values that come through on the stream so that I have an output of there values.

For instance If my data stream looks something like this:

Code:

c2
c2
c2
c2
a1
c2

I want to have an output stream that looks like

Code:

system is up
system is up
system is up
system is up
system is down
system is up

I was trying to script it something like:

Code:

#!/bin/bash

while [ 1 -le 1 ]
        do
                case in
                        c2) echo "system is up";;
                        a1) echo "system is down";;
                        *)  echo "invalid input";;
                esac
        done

However I have no idea what to put in between the "case in" statement so that it will read the input from a pipe "|".

Perhaps there is a way to do this in perl using the Switch and Case commands but I haven't been able to figure that out either.

Any Help is appreciated.

ntubski 07-01-2008 09:33 PM

Code:

#!/bin/bash

while read VALUE
do
    case "$VALUE" in
      c2) echo "system is up";;
      a1) echo "system is down";;
      *)  echo "invalid input";;
    esac
done


matthewg42 07-01-2008 10:32 PM

incidentally, if you want an infinite look, you can use the true command, like this:
Code:

while true; do
  echo "Pull the wool over your own eyes"
done


telecom_is_me 07-01-2008 10:48 PM

Quote:

Originally Posted by matthewg42 (Post 3200948)
incidentally, if you want an infinite look, you can use the true command, like this:
Code:

while true; do
  echo "Pull the wool over your own eyes"
done


Thanks guys/gals I split the difference and got:

Code:

#!/bin/bash

while true; do                                          # Infinate Loop
        while read VALUE; do                            # Loop to Read In the Input
                case "$VALUE" in                        # Case Statement to Check the Input
                        c2) echo "system is up";;      # If the input equals c2
                        a1) echo "system is down";;    # If the input equals a1
                        *)  echo "invalid input";;      # If the input doesn't equal any of the previous
                esac                                    # End the case statement
        done                                            # End the Read In While Loop
done

Which works out well.

ntubski 07-02-2008 10:08 AM

I don't think you want an infinite loop in this case. read will only return false when the input closes, so your "splitting the difference" will go into a busy loop (using 100% cpu) after the input stops.

Mr. C. 07-02-2008 04:45 PM

Furthermore, the inner while loop is superfluous.


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