Flush perl‚Äôs print buffer – $|=1 If you put a print statement inside of a loop that runs really really quickly, you won‚Äôt see the output of your print statement until the program terminates. The solution to this problem is to ‚Äúflush‚Äù the output buffer after each print statement. Where $| is non zero, it […]
Mac Book Pro – Sleeping Issues OS X supports five different sleep modes: 0 – Old style sleep mode, with RAM powered on while sleeping, safe sleep disabled, and super-fast wake. 1 – Hibernation mode, with RAM contents written to disk, system totally shut down while ‚Äúsleeping,‚Äù and slower wake up, due to reading the […]
GET or POST – Secure GET is NOT Secure GET is not a secure method of sending data. Don’t use it for forms that send password info, credit card data or other sensitive information. Since the data is passed through as part of the URL, it’ll show up in the web server’s logfile (complete with […]
Perl – Trim Text, Trim String Left trim- ltrim or lstrip removes white spaces from the left side of a string: [perl] $str =~ s/^\s+//; [/perl] Right trim – rtrim or rstrip removes white spaces from the right side of a string: [perl] $str =~ s/\s+$//; [/perl] Trim Both [perl] $str =~ s/^\s+|\s+$//g [/perl] [perl] […]
Recursively Expand a NIS Netgroup @results=expand_ng(); # Given a netgroup, will return a list of members. Will recurse # netgroups if the $recurse flag is set sub expand_ng { my($netgroup)=@_; my(@returnng); my($ng)=`ypmatch $netgroup netgroup`; $? != 0 and return; #Don’t continue for duff netgroups my($value); $ng =~ s/\s+/ /g; #Compress whitespace $ng =~ s/\s+,/,/g; #Remove […]
RedHat vs Ubuntu – Useful Package Commands Task Red Hat/Fedora Ubuntu Refresh list of available packages Yum refreshes each time it’s used apt-get update Install a package from a repository yum install package_name apt-get install package_name Install a package file yum install package.rpm rpm -i package.rpm dpkg –install package.deb Remove a package rpm -e package_name […]
Given a size in KB, try to return a more readable MB, GB or TB answer string function readable_size_for_kb($mykb) { if ( $mykb > 1073741824 ) { return sprintf(“%5.1f TB”,$mykb / 1024 / 1024/1024); } elseif ( $mykb > 1048576 ) { return sprintf(“%5.1f GB”,$mykb / 1024 / 1024); } elseif ( $mykb > 1024 […]
Readable Time – PHP, given a UNIX timestamp, return a readable time # Given a unix timestamp, returns in readable time ie “1 day, 7 hours, 23 minutes, 2 seconds” function readable_time($timestamp, $num_times = 2) { //this returns human readable time when it was uploaded (array in seconds) $times = array(31536000 => ‘year’, 2592000 => […]
Add a timestamp to every STDOUT line I found this useful for debugging and just handy to have in the toolbox. Alias within your profile! Use: ./anyscript_you_choose | ./addtime [perl] #!/bin/perl -w # filename: ./addtime my($old)=select(STDOUT); $|=1; select(STDIN); $|=1; select ($old); while(<>) { my($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)= localtime (time); printf “%02d:%02d:%4d %02d:%02d:%02d %s”,$mday,$mon+1,$year+1900,$hour,$min,$sec,$_; } [/perl]
Perl – Get the md5 hash for a file Get the MD5 hash for a file on the command line, using Digest::MD5 usage: ./md5sum.pl [filename] [-f] -f do not display filename [perl] use warnings; use strict; use Digest::MD5; sub md5sum { my $file = shift; my $digest = “”; eval{ open(FILE, $file) or die “Can’t […]