LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Is my bash file stupid, can it be simplified? (https://www.linuxquestions.org/questions/linux-general-1/is-my-bash-file-stupid-can-it-be-simplified-4175475856/)

stozi 09-04-2013 08:22 AM

Is my bash file stupid, can it be simplified?
 
I made a couple pipe menus for Openbox over the last couple of days, which was fun as I knew nothing about bash previously. One displayed the output of df but it seems a limit to menu width undermined this. This one, on the other hand, works like a dandy for me. I don't understand entirely how it works, so for the sake of my own bash/awk education, I ask you, can it be simplified without using bc or acpi or something else I might not have installed?

Code:

#!/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>"


TenTenths 09-04-2013 09:27 AM

All looks pretty sensible to me. Might just want to change the first line to:

Code:

#!/bin/bash
To ensure that bash shell is used rather than whatever /bin/sh is linked to.

H_TeXMeX_H 09-04-2013 09:33 AM

I also recommend using $() instead of back ticks ``, as they are deprecated and will be removed at some point in the future.


All times are GMT -5. The time now is 03:00 PM.