0

(Question SOLVED and edited for helping someone else that need it or have the same mistake, now with the code in the post for better readability)

I am trying to create a script that does something if my disk arrays fail. When I run the command it have to return the number 4 (grep counted 4 times the word OK, one time for each disk).

Then I want to check if there where 4 or less OKs and do something.

Original script:

#!/bin/bash
$okdevices = hpssacli controller slot=4 physicaldrive all show | grep -c "OK"
if [ $okdevices -lt 4 ]
then
    echo "disk missing"
fi

This returns that expect an unary operator.

Corrected thanks to comments and answer:

#!/bin/bash
okdevices="$(hpssacli controller slot=4 physicaldrive all show | grep -c OK)"
if [ $okdevices -lt 4 ]; then
    echo "missing disk"
fi

Now works fine.

Thank you very much!

  • 1
    You're missing backticks around the command, in order to execute it; at the moment, you're setting $okdevices to be a string of commands, rather than the output of those commands. Please don't use images to show text; instead, cut-and-paste the text into your question, which makes it both more readable, and searchable. – MadHatter supports Monica May 9 '16 at 13:53
  • 1
    Post the content of the scripts here please, it's way easier to fix it. – sysfiend May 9 '16 at 13:55
  • Thanks for advice about the backticks. I edited the question for posting the script here instead putting a link to the image to show it. – yb_esc May 10 '16 at 7:09
0

Wrong command output assign to variable, you are evaluating the string "hpssacli controller slot=4 physicaldrive all show | grep -c OK" with "4"

This should work:

#!/bin/bash
okdevices="$(hpssacli controller slot=4 physicaldrive all show | grep -c OK)"
if [ $okdevices -lt 4 ]; then
    echo "missing disk"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy