1

I have a script that keeps unsetting the value but I cant find why. In the case below the output is:

green Physical drive in slot 2I:1:1 (size : 146.8GB) status is OK
green Physical drive in slot 2I:1:2 (size : 146.8GB) status is OK
green Physical drive in slot 2I:1:3 (size : 146.8GB) status is OK
yellow Physical drive in slot 2I:1:4 (size : 146.8GB) status is BAD !!!
green Physical drive in slot 2I:1:5 (size : 146.8GB) status is OK

I need the value Yellow to trigger my monitor. But yellow is changed in the loop and I can't find what I'm doing wrong

$HPACUCLI ctrl all show config | grep drive | while read OUTPUT ; do

    TYPE=$(echo $OUTPUT | awk '{print $1}' | sed s/drive//)
    SLOT=$(echo $OUTPUT | awk '{print $2}')
    STATUS=$(echo $OUTPUT | awk '{print $NF}' | sed s/\)//)
    if [ "$STATUS" == "spare" ] ; then
        STATUS=$(echo $OUTPUT | cut -d',' -f4 | sed 's/ //g')
    fi

    if [ "$TYPE" == "physical" ] ; then
        SIZE=$(echo $OUTPUT | awk '{print $8 $9}' | sed s/\,//)

        if [ "$STATUS" != "OK" ] ; then
            YELLOW=1
            echo "Yellow" "$YELLOW"
            LINE="&yellow Physical drive in slot $SLOT (size : $SIZE) status is BAD !!!"

        elif [ "$STATUS" == "OK" ] ; then
            echo "Yellow" "$YELLOW"
            LINE="&green Physical drive in slot $SLOT (size : $SIZE) status is OK"
        else
            echo "Yellow" "$YELLOW"
            RED=1
            LINE="&red Unknow status (or stupid monitoring script) for physical drive in slot $SLOT \(size : $SIZE\) !!!"
        fi
    fi

    echo $LINE >> $MSG_FILE
done

echo "Yellow" "$YELLOW"

The output when running is:

Yellow
Yellow
Yellow
Yellow 1
Yellow 1
Yellow

Why is the last Yellow not 1 ??

Noam M
  • 441
  • 1
  • 7
  • 17
  • see also: [Why is using a shell loop to process text considered bad practice?](https://unix.stackexchange.com/q/169716/7696) - you would be much better off doing this in awk rather than a shell while loop around multiple invocations of awk and cut and sed and whatever else. – cas Aug 27 '19 at 11:38
  • See also question E4 in the [bash FAQ](https://tiswww.case.edu/php/chet/bash/FAQ) – Stéphane Chazelas Aug 27 '19 at 11:39
  • Also in [that other unofficial bash FAQ](https://mywiki.wooledge.org/BashFAQ/024) – Stéphane Chazelas Aug 27 '19 at 11:39
  • @Noam M (and the users who approved this edit), please don't edit closed posts if the edit does not resolve the reason why the post was closed in the first place. See https://unix.meta.stackexchange.com/q/5319/22812 – Anthony Geoghegan Aug 28 '19 at 09:12

0 Answers0