Auslesen des Akku-Ladestatus unter Ubuntu

Das folgende Perl-Skript zeigt den prozentuellen Ladezustand einer Netbook-Baterie unter Ubuntu an:
  1. #!/usr/bin/perl
  2. $FILE;
  3. $fileInfo = "/proc/acpi/battery/BAT0/info";
  4. $fileState = "/proc/acpi/battery/BAT0/state";
  5. $designCapacity=0;
  6. $capacity=0;
  7.  
  8. #open info file
  9. open( FILE, "< $fileInfo" ) or die "Can't open $fileInfo : $!";
  10. while( <FILE> ) {
  11. if ( $_ =~ /^design capacity:s*(d*)s*mAh$/ ) {
  12. $designCapacity = $1;
  13. }
  14. }
  15. close( FILE );
  16.  
  17. #open state file
  18. open( FILE, "< $fileState" ) or die "Can't open $fileState : $!";
  19. while( <FILE> ) {
  20. if ( $_ =~ /^remaining capacity:s*(d*)s*mAh$/ ) {
  21. $capacity = $1;
  22. }
  23. }
  24. close( FILE );
  25.  
  26. #check data
  27. if (($capacity == 0) || ($designCapacity == 0)) {
  28. die "Fehler beim Lesen des Batteriestatus";
  29. }
  30.  
  31. #calculate percantge
  32. $batterieState = $capacity / $designCapacity * 100;
  33.  
  34. #print state
  35. printf("Batterie Status: %3.0f%n", $batterieState );