Advertisement
Guest User

k-smart 1.3

a guest
Feb 13th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. Main() {
  4.     export PATH=/bin:/usr/bin:/opt/local/bin:/usr/local/bin
  5.     PathToSmartCtl="$(GetSmartCtl)"
  6.     PathToKeys="${0%/*}"/keys
  7.  
  8.     CreateShellAlias
  9.  
  10.     sleep 1
  11.     IsShiftPressed=$("${PathToKeys}" shift)
  12.     IsOptionPressed=$("${PathToKeys}" option)
  13.  
  14.     case ${IsShiftPressed} in
  15.         1)
  16.             # shift is pressed, we are in test mode!
  17.             # if option is pressed as well we're
  18.             # performing a long test otherwise short
  19.             if [ "X${IsOptionPressed}" = "X1" ]; then
  20.                 TestMode=long
  21.             else
  22.                 TestMode=short         
  23.             fi
  24.             GetListOfVolumes | while read ; do
  25.                 # check whether device is SMART capable
  26.                 "${PathToSmartCtl}" -a ${REPLY} >/dev/null 2>&1
  27.                 if [ $? -eq 0 ]; then
  28.                     echo -e "${AbstandSchraddel}Previous test results for ${REPLY}:\n"
  29.                     "${PathToSmartCtl}" -l selftest ${REPLY}
  30.                     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"
  31.                     "${PathToSmartCtl}" -t ${TestMode} ${REPLY}
  32.                     AbstandSchraddel="\n\n\n"
  33.                 fi
  34.             done
  35.             ;;
  36.         *)
  37.             # we simply display SMART status for all devices
  38.             GetListOfVolumes | while read ; do
  39.                 echo -e "${AbstandSchraddel}Examining ${REPLY}\n"
  40.                 "${PathToSmartCtl}" -a ${REPLY}
  41.                 AbstandSchraddel="\n\n\n"
  42.             done
  43.             ;;
  44.     esac
  45. } # Main
  46.  
  47. CreateShellAlias() {
  48.     if [ -f "${HOME}/.profile" ]; then
  49.         MyTmpFile="$(mktemp /tmp/${0##*/}.XXXXXX || (echo -e "Not able to create temporary file below /tmp/. Skip creating shell alias." | logger ; return))"
  50.         trap "rm -f \"${MyTmpFile}\"; exit 0" 0 1 2 3 15
  51.         grep -v "^alias smartctl" <"${HOME}/.profile" >"${MyTmpFile}"
  52.         echo -e "alias smartctl=\"${PathToSmartCtl}\"" >>"${MyTmpFile}"
  53.         cat "${MyTmpFile}" >"${HOME}/.profile"
  54.     else
  55.         echo -e "alias smartctl=\"${PathToSmartCtl}\"" >>"${HOME}/.profile"
  56.     fi
  57. } # CreateShellAlias
  58.  
  59. GetListOfVolumes() {
  60.     /usr/sbin/diskutil list | grep "^/dev/" | while read ; do
  61.         /usr/sbin/diskutil corestorage info ${REPLY} >/dev/null 2>&1 || echo ${REPLY}
  62.     done
  63. } # GetListOfVolumes
  64.  
  65. GetSmartCtl() {
  66.     mdfind "kMDItemCFBundleIdentifier == 'de.kaiser-edv.k-smart'" | grep "\.app$" | while read ; do
  67.         SmartCtlVersion=$("${REPLY}"/Contents/Resources/smartctl | awk -F" " '/^smartctl / {print $2}')
  68.         echo -e "${SmartCtlVersion}\t${REPLY}/Contents/Resources/smartctl"
  69.     done | sort -r | head -n1 | cut -f2
  70. } # GetSmartCtl
  71.  
  72. Main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement