3
\$\begingroup\$

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" ]
then
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'
fi

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

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 19, 2017 at 14:11
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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.
answered Dec 26, 2017 at 13:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.