case statement help

May 26, 2011 at 22:30:01
Specs: Windows XP
filename: mo.dat

MONITOR "ALERT_INFO"
DESCRIPTION "Triggered when informational Netware alert occured"
MAXTHRESHOLD 95
SEVERITY Normal


MONITOR "ALERT_MAJOR"
DESCRIPTION "Triggered when major Netware alert occured"
SEVERITY Major

****************************
i need the o/p above in below format

"ALERT_INFO","Triggered when informational Netware alert occured", 95, Normal
"ALERT_MAJOR","Triggered when major Netware alert occured", NA, Major


i have the code, but it doesn;t work the way i want.
****************************
#!/bin/bash

{
printf "MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY\n"
while read key value
do
case $key in
"MONITOR") monitor=$value ;;
"DESCRIPTION") desc=$value ;;
"MAXTHRESHOLD") max=$value ;;
"SEVERITY") printf "$monitor,$desc, $max, $value\n" ;;
esac
done < mo.dat
} > fin


See More: case statement help

Report •

#1
May 27, 2011 at 09:01:50
Since the "max" variable is not set in the second "monitor" block, it retains the original value. One way is to initialize the variables after the printf statement. I would also initialize them to something before starting the loop:

#!/bin/bash

{
# initialization block
max="NA"

printf "MONITOR,DESCRIPTION,MAXTHRESHOLD,SEVERITY\n"
while read key value
do

   case $key in
      "MONITOR") monitor=$value ;;
      "DESCRIPTION") desc=$value ;;
      "MAXTHRESHOLD") max=$value ;;
      "SEVERITY") printf "$monitor,$desc, $max, $value\n"
          max="NA"
       ;;
   esac
done < mo.dat
} > fin


Report •
Related Solutions


Ask Question