Skip to content

Instantly share code, notes, and snippets.

@SwagDevOps
Created April 8, 2018 13:12
Show Gist options
  • Select an option

  • Save SwagDevOps/f78e3d8af953dd788e1c79750b9f1b26 to your computer and use it in GitHub Desktop.

Select an option

Save SwagDevOps/f78e3d8af953dd788e1c79750b9f1b26 to your computer and use it in GitHub Desktop.
Get all devices (hdd) temperatures at once.
#!/usr/bin/env sh
# Get all devices (hdd) temperatures at once.
#
# Sample of use:
#
# ```sh
# sudo hddtemp-report
# sudo hddtemp-report /tmp/hddtemp-report
# sudo hddtemp-report /tmp/hddtemp-report '%s: $alignr %s'
# ```
#
# Dependencies are ``hddtemp`` and ``sponge``
devices() {
df | grep -Eo '^/dev/[a-z]+' | \
while read device; do
test -d "$device" || echo "$device"
done
}
report() {
devices | \
while read device; do
hddtemp --unit="$HDDTEMP_UNIT" "${device}"
done
}
format() {
local format="%s: %s"
test -n "$1" && format="$1"
report 2> /dev/null | \
while read line; do\
local tvalue=$(echo "$line" | awk '{print $NF}')
local device=$(echo "$line" | \
awk '{print $1}' | perl -pe 's/:$//')
printf "${format}\n" "${device}" "${tvalue}"
done | sort
}
main() {
# globals --------------------------------------------------------
export LANGUAGE='en_US.UTF-8'
export HDDTEMP_UNIT=C
# params ---------------------------------------------------------
local output='/dev/stdout'
local format=''
test -n "$1" && output="$1"
test -n "$2" && format="$2"
test -n "$format" && {
format "$format" | sponge "$output"
exit $?
} || {
report 2> /dev/null | sort
exit $?
}
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment