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

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.

echo -n "Hours remaining: " 
&& echo "$(cat /proc/acpi/battery/BAT0/state 
      | grep 'remaining capacity' 
      | awk '{print $3}') /  
   $(cat /proc/acpi/battery/BAT0/state 
      | grep 'present rate' 
      | awk '{print $3}')" 
   | bc -l

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.

One Comment on “Everything is a file A.K.A. Battery state on Linux”

  1. […] 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 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s