Nicolás Brailovsky


A modern blog

Everything is a file A.K.A. Battery state on Linux

author Posted by: nico on date May 14th, 2009 | filed Filed under: Linux, Programming

I present to you the latest release from Wheel Reinventions Inc. – Already Invented dept. – a script to check the remaining battery time from Linux console.

  1. echo -n "Hours remaining: " \
  2. && echo "$(cat /proc/acpi/battery/BAT0/state \
  3.       | grep ‘remaining capacity’ \
  4.       | awk ‘{print $3}’) /  \
  5.    $(cat /proc/acpi/battery/BAT0/state \
  6.       | grep ‘present rate’ \
  7.       | awk ‘{print $3}’)" \
  8.    | bc -l
car_battery

Yes, I was bored, so? I’m planning to release a new bogosort implementation after this one, what do you think?

Jokes aside, this nifty script shows how flexible can be the model “everything is a file” which Unix-y systems implement. With a couple of pipes (and an almost magical incantation for those uninitiated in the console cult) you can do lots of stuff with very little work

This concept could be associated with the way polymorfism works in a programming language, but that’s an entry for another day.

tagOne Response to “Everything is a file A.K.A. Battery state on Linux”

  1. Nicolás Brailovsky » Blog Archive » DIY gnome applets Said,

    [...] widgets. Need to check your laptop’s battery? No need to search for a widget anymore, just cat /proc/acpi/battery/BAT0/state. Need to check the weather? Just wget your favorite forecast page and parse it with grep, sed an [...]

     Add A Comment

trackback Trackback URI | rsscomment Comments RSS