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?
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 regex matching: this can be achieved by the
Use more descriptive names:
smart_status_text
is more self-evident thanA
.- Properly format your code: indent the commands inside the
if
block and don't leave a blank line afterthen
.
Explore related questions
See similar questions with these tags.