Шалом.
Сабж. Может что-то можно оптимизировать (для скорости работы, удобства чтения и ПРАВИЛЬНОСТИ)?
#!/bin/sh
# CPUs
getstat_core_temp_separator="/"
getstat_core_temp_suffix="°C"
# Battery
getstat_battery_state_charging="заряжается"
getstat_battery_state_discharging="разряжается"
getstat_battery_state_charged="заряжена"
getstat_battery_state_missing="отсутсвует"
getstat_battery_state_unknown="ошибка"
getstat_sysctl="sysctl -n"
getstat_arg_n="$#"
getstat_core_temperature() {
getstat_core_n="`${getstat_sysctl} hw.ncpu`"
while [ ! "$getstat_core_n" -eq "0" ] ; do
getstat_core_n="$(( ${getstat_core_n} - 1 ))"
getstat_core_temp="`${getstat_sysctl} dev.cpu.${getstat_core_n}.temperature`"
getstat_core_temp="`printf "${getstat_core_temp}" | cut -d, -f1`"
printf "${getstat_core_temp}${getstat_core_temp_suffix}"
[ "$getstat_core_n" -ge "1" ] && printf "${getstat_core_temp_separator}"
done
}
getstat_memory_total() {
getstat_memory_page_size="`${getstat_sysctl} vm.stats.vm.v_page_size`"
getstat_memory_page_count="`${getstat_sysctl} vm.stats.vm.v_page_count`"
getstat_memory_total="$(( ${getstat_memory_page_count} * ${getstat_memory_page_size} / 1048576 ))"
printf "${getstat_memory_total}"
}
getstat_memory_used() {
getstat_memory_page_size="`${getstat_sysctl} vm.stats.vm.v_page_size`"
getstat_memory_page_wire="`${getstat_sysctl} vm.stats.vm.v_wire_count`"
getstat_memory_page_active="`${getstat_sysctl} vm.stats.vm.v_active_count`"
getstat_memory_used="$(( (${getstat_memory_page_wire} + ${getstat_memory_page_active}) * \
${getstat_memory_page_size} / 1048576 ))"
printf "${getstat_memory_used}"
}
getstat_memory_free() {
getstat_memory_free="$(( `getstat_memory_total` - `getstat_memory_used` ))"
printf "${getstat_memory_free}"
}
getstat_battery_state() {
getstat_battery_state="`${getstat_sysctl} hw.acpi.battery.state`"
if [ "$getstat_battery_state" -eq "2" ] ; then
getstat_battery_state="${getstat_battery_state_charging:=charging}"
elif [ "$getstat_battery_state" -eq "1" ] ; then
getstat_battery_state="${getstat_battery_state_discharging:=discharging}"
elif [ "$getstat_battery_state" -eq "0" ] ; then
getstat_battery_state="${getstat_battery_state_charged:=charged}"
elif [ "$getstat_battery_state" -eq "-1" ] ; then
getstat_battery_state="${getstat_battery_state_missing:=missing}"
else
getstat_battery_state="${getstat_battery_state_unknown:=unknown}"
fi
printf "${getstat_battery_state}"
}
getstat_battery_life() {
getstat_battery_life="`${getstat_sysctl} hw.acpi.battery.life`"
printf "${getstat_battery_life}"
}
while [ "$getstat_arg_n" -ne "0" ] ; do
getstat_arg_n="$(( ${getstat_arg_n} - 1 ))"
case ${1} in
-ct) getstat_out="${getstat_out} `getstat_core_temperature`" ;;
-mt) getstat_out="${getstat_out} `getstat_memory_total`" ;;
-mu) getstat_out="${getstat_out} `getstat_memory_used`" ;;
-mf) getstat_out="${getstat_out} `getstat_memory_free`" ;;
-bs) getstat_out="${getstat_out} `getstat_battery_state`" ;;
-bl) getstat_out="${getstat_out} `getstat_battery_life`" ;;
*) getstat_out="${getstat_out} `printf "${1}"`" ;;
esac
shift
done
echo ${getstat_out}
// Это не конец, будет больше отдавать.
Интересует улучшение конструкции обработки параметров в первую очередь.
Заранее благодарю.