Is my bash file stupid, can it be simplified? -
i made couple pipe menus openbox on last couple of days, fun knew nothing bash previously. 1 displayed output of df seems limit menu width undermined this. one, on other hand, works dandy me. don't understand entirely how works, sake of own bash/awk education, ask you, can simplified without using bc or acpi or else might not have installed?
#!/bin/sh now=`cat /sys/class/power_supply/bat0/charge_now` full=`cat /sys/class/power_supply/bat0/charge_full` date=$(date '+%r %a %x') wifi=$(cat /proc/net/wireless | awk 'nr==3 {print $3}') batt=$(echo $now $full | awk '{ printf("%.2f\n", $1/$2 * 100) }') echo "<openbox_pipe_menu>" echo "<separator label=\"$date\"/>" echo "<separator />" echo "<item label=\"wifi: $wifi%\"/>" echo "<item label=\"batt: $batt%\"/>" echo "</openbox_pipe_menu>"
first if it's bash, use proper header:
#!/bin/bash
no need use cat
, use $(<)
:
now=$(</sys/class/power_supply/bat0/charge_now) full=$(</sys/class/power_supply/bat0/charge_full)
no need use cat again since awk can accept input or parse files itself:
wifi=$(awk 'nr==3 {print $3}' /proc/net/wireless)
quote variables between ""
properly:
batt=$(echo "$now $full" | awk '{ printf("%.2f\n", $1/$2 * 100) }')
basically that's can see that's apparent.
update
as suggested user000001, use here strings:
batt=$(awk '{ printf("%.2f\n", $1/$2 * 100) }' <<< "$now $full")
Comments
Post a Comment