LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-01-2008, 08:51 PM   #1
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Rep: Reputation: 15
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.
 
Old 07-01-2008, 09:33 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,386

Rep: Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808
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
 
Old 07-01-2008, 10:32 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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
 
Old 07-01-2008, 10:48 PM   #4
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 07-02-2008, 10:08 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,386

Rep: Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808Reputation: 808
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.
 
Old 07-02-2008, 04:45 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Furthermore, the inner while loop is superfluous.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Statement craigjward Programming 6 12-12-2007 11:33 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
case statement baks Programming 2 03-15-2007 01:19 PM
problematic case statement alaios Programming 4 08-24-2005 09:05 AM
bash'ed by case statement??? 3inone Programming 2 04-29-2004 04:52 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration