So I don't know much about bash scripting. I am just a beginner. I made this script which checks the SMART status of my root volume and if it is failing then it immediately throws a pop up alert if my drive is failing. Here's the code:

A=$( diskutil info disk0 | grep SMART )

if [ "$A" != "   SMART Status:             Verified" ]

osascript -e 'tell application "Finder" to activate' -e 'tell 
application "Finder" to display dialog "Your Drive is failing, 
Please backup all your important files now" buttons 
{"OK"} with icon stop'

I know this is so simple. Do you have any suggestions to improve it?


1 Answer 1


Some suggestions:

  • A direct string comparison test may be flaky especially if the string contains multiple space characters. I can think of three alternatives:

    • Use regex matching: this can be achieved by the =~ operator inside [[ ]]: if [[ ! "$A" =~ Verified ]].
    • Use awk:

      A=$(diskutil info disk0 | awk '/SMART Status:/{print $NF}')
      if [ "$A" != 'Verified' ]
      # ...
    • Check exit status instead of output:

      if sudo smartctl -H -q silent /dev/disk0
      # ...
  • Use more descriptive names: smart_status_text is more self-evident than A.

  • Properly format your code: indent the commands inside the if block and don't leave a blank line after then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.