Tag Archive for Shell

battery

#!/usr/bin/perl

# battery.pl
#
# Perl script that displays to the terminal the charge on your Mac Intel battery (and takes
# a guess as to how much time is left on the charge).
#
# Version 1.0 (010308) - Hey, it works!
#         1.1 (010408) - Added debugging code; added IsCharging test; changed symbols for TimeLeft.
#         1.2 (011208) - "Fixed" some weirdness with the battery being at less than 100% capacity,
#                        but still reading "FullyCharged" or reading "FullyCharged" while also
#                        reading "IsCharging." Wacky.
#
# Author: P. Ham - <a href="mailto:pham@sdf.lonestar.org">pham@sdf.lonestar.org</a>
#
# Cryptic Notes: 65535 may just be a kludge number to mean "infinity." See <a href="http://en.wikipedia.org/wiki/65535_" >http://en.wikipedia.org/wiki/65535_</a>(number).

use strict;
use Data::Dumper;
my $IOREG = "/usr/sbin/ioreg";

my $output = `$IOREG -nAppleSmartBattery`;
my @output = split("
", $output);
my $batteryHash;

foreach (@output) {
if (/"(MaxCapacity)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(DesignCapacity)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(CurrentCapacity)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(AvgTimeToEmpty)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(AvgTimeToFill)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(TimeRemaining)"s+=s+(d+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(IsCharging)"s+=s+(w+)/) {
$batteryHash->{$1} = $2;
} elsif (/"(FullyCharged)"s+=s+(w+)/) {
$batteryHash->{$1} = $2;
}
}

my $DEBUG = 0;
print Dumper $batteryHash if $DEBUG;

my $percentCapacity = ( $batteryHash->{"CurrentCapacity"} / $batteryHash->{"MaxCapacity"} ) * 100;
$percentCapacity = int($percentCapacity + .5 * ($percentCapacity <=> 0));

my $timeLeft;
if ($batteryHash->{"IsCharging"} eq "Yes" && $batteryHash->{"FullyCharged"} eq "No") {
# battery is charging so can't calculate time left (yet)
$timeLeft = "( + )";
# $timeLeft = "(~/~)";
} elsif ($batteryHash->{"FullyCharged"} eq "Yes") {
$timeLeft = "( f )";
} elsif ($percentCapacity != 100) {
# OK, calculate time left
$timeLeft = $batteryHash->{"TimeRemaining"} / 60;
my ($hours, $minutes) = split(/./, $timeLeft);
$minutes = 60 * ( "." . $minutes );
$minutes = int($minutes + .005 * ($minutes <=> 0));
$minutes = sprintf ("%02d", $minutes);
$timeLeft = "($hours:$minutes)";
} else {
# not charging and 100% capacity
# $timeLeft = "( - )";
$timeLeft = "( f )";
}

print "battery: $percentCapacity% $timeLeft
";

exit;

source

Edit the ignored files in the current directory.

svn propedit svn:ignore .

source

sync a file or dir to remote location

rsync -P -az -e ssh sourcedir  <a href="mailto:username@andric.us">username@andric.us</a>:~/targetdir/

source

svn diff with 20 lines of context

svn diff --diff-cmd=diff -x -U20

source

Downcase stdin

tr [A-Z] [a-z]

source

recursively delete CVS directories

find . -depth -name 'CVS' -exec rm -rf '{}' ; -print

source

Preventing Apache memory leaks with ulimit

#!/bin/sh

HTTPD=/usr/local/apache2/bin/httpd
CONF=/bmi/httpd-php/conf/httpd.conf

exec 2>&1
echo starting...
ulimit -v 100000
exec $HTTPD -f $CONF -D NO_DETACH

source

infinite loop in shell

while true; do command1;command2;command n;sleep 30; done;

source

Screen Saver Background

/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background

source

Sheller example

class GnuPackage < BasePackage
def initialize()
super()

@base = "#{@name}-${version}"
@tarball = "#{@base}.tar.bz2"
@url = "ftp://ftp.gnu.org/pub/gnu/#{@name}/#{@tarball}"
@dir = @base
@build_dir = "#{@name}-build"
end

end

class BinutilsPackage < GnuPackage
def initialize()
@version = "2.17"
@name = "binutils"
super()

@config_opts = "--disable-nls --with-sysroot="#{$sys_root}" --enable-shared --disable-multilib"
end

def build
block("build") do
run "cd #{$build_dir}"
run "mkdir -p #{@build_dir}"
run "cd #{@build_dir}"
configure "script" => "../#{@dir}/configure", "opts" => @config_opts
run "make configure-host"
run "make"
end
end

def install
block("install") do
run "cd #{$build_dir}"
run "cd #{@build_dir}"
run "make install"
end
end
end

binutils = BinutilsPackage.new
binutils.download "/tmp"
binutils.extract
binutils.build
binutils.install

source