Advertisement
Guest User

k-smart 1.4

a guest
Feb 13th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.23 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. Main() {
  4.     export PATH=/bin:/usr/bin:/opt/local/bin:/usr/local/bin
  5.     MyTmpFile="$(mktemp /tmp/${0##*/}.XXXXXX || (echo -e "Not able to create temporary file below /tmp/. Exiting." | logger ; exit 1))"
  6.     trap "rm -f \"${MyTmpFile}\"; exit 0" 0 1 2 3 15
  7.    
  8.     # PathToSmartCtl="$(GetSmartCtl)"
  9.     PathToSmartCtl="${0%/*}"/smartctl
  10.     PathToKeys="${0%/*}"/keys
  11.  
  12.     CreateShellAlias
  13.  
  14.     sleep 1
  15.     IsShiftPressed=$("${PathToKeys}" shift)
  16.     IsOptionPressed=$("${PathToKeys}" option)
  17.  
  18.     case ${IsShiftPressed} in
  19.         1)
  20.             # shift is pressed, we are in test mode!
  21.             # if option is pressed as well we're
  22.             # performing a long test otherwise short
  23.             if [ "X${IsOptionPressed}" = "X1" ]; then
  24.                 TestMode=long
  25.             else
  26.                 TestMode=short         
  27.             fi
  28.             GetListOfVolumes | while read ; do
  29.                 # check whether device is SMART capable
  30.                 "${PathToSmartCtl}" -a ${REPLY} >/dev/null 2>&1
  31.                 if [ $? -eq 0 ]; then
  32.                     echo -e "${AbstandSchraddel}Previous test results for ${REPLY}:\n"
  33.                     "${PathToSmartCtl}" -l selftest ${REPLY}
  34.                     echo -e "\nNow performing a ${TestMode} test on ${REPLY} in the background. You can close this window and try again in several minutes. Please see below the smartctl output.\n"
  35.                     "${PathToSmartCtl}" -t ${TestMode} ${REPLY}
  36.                     AbstandSchraddel="\n\n\n"
  37.                 fi
  38.             done
  39.             ;;
  40.         *)
  41.             # we simply display SMART status for all devices
  42.             GetListOfVolumes | while read ; do
  43.                 echo -e "${AbstandSchraddel}Examining ${REPLY}\n"
  44.                 "${PathToSmartCtl}" -a ${REPLY} | tee "${MyTmpFile}"
  45.                 # check for warning
  46.                 ButtonPressed=""
  47.                 grep "WARNING" "${MyTmpFile}" >/dev/null 2>&1 && ButtonPressed="$(DisplayWarning "${MyTmpFile}")"
  48.                 if [ "X${ButtonPressed}" = "XMore Info" ]; then
  49.                     grep "^http" "${MyTmpFile}" | while read ; do
  50.                         open -b com.apple.Safari "${REPLY}"
  51.                     done
  52.                 fi
  53.                 AbstandSchraddel="\n\n\n"
  54.             done
  55.             ;;
  56.     esac
  57. } # Main
  58.  
  59. CreateShellAlias() {
  60.     if [ -f "${HOME}/.profile" ]; then
  61.         grep -v "^alias smartctl" <"${HOME}/.profile" >"${MyTmpFile}"
  62.         echo -e "alias smartctl=\"${PathToSmartCtl}\"" >>"${MyTmpFile}"
  63.         cat "${MyTmpFile}" >"${HOME}/.profile"
  64.     else
  65.         echo -e "alias smartctl=\"${PathToSmartCtl}\"" >>"${HOME}/.profile"
  66.     fi
  67. } # CreateShellAlias
  68.  
  69. GetListOfVolumes() {
  70.     /usr/sbin/diskutil list | grep "^/dev/" | while read ; do
  71.         /usr/sbin/diskutil corestorage info ${REPLY} >/dev/null 2>&1 || echo ${REPLY}
  72.     done
  73. } # GetListOfVolumes
  74.  
  75. GetSmartCtl() {
  76.     mdfind "kMDItemCFBundleIdentifier == 'de.kaiser-edv.k-smart'" | grep "\.app$" | while read ; do
  77.         SmartCtlVersion=$("${REPLY}"/Contents/Resources/smartctl | awk -F" " '/^smartctl / {print $2}')
  78.         echo -e "${SmartCtlVersion}\t${REPLY}/Contents/Resources/smartctl"
  79.     done | sort -r | head -n1 | cut -f2
  80. } # GetSmartCtl
  81.  
  82. DisplayWarning() {
  83.     DisplayString=$(grep "WARNING" "$1" | sed -e 's/\"/\\\"/g' -e 's/^==\>\ //')
  84.     grep "^http" "$1" >/dev/null 2>&1
  85.     if [ $? -eq 0 ]; then
  86.         Buttons='{"More Info", "OK"}'
  87.     else
  88.         Buttons='{"OK"}'
  89.     fi
  90.     osascript <<-EOF
  91.         tell application "System Events"
  92.             activate
  93.             set ButtonPressed to (button returned of (display dialog "${DisplayString}" buttons ${Buttons} default button 1 with icon caution giving up after 30))
  94.         end tell
  95.     EOF
  96. } # DisplayWarning
  97.  
  98.  
  99. Main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement