Simple Phonebook Script

Sep 20
2008
#!/usr/bin/env ruby
 
class Hash
  def search(arg)
    self.select { |k,v| k =~ /#{arg}/ or v =~ /#{arg}/ }
  end
end
 
search = ARGV[0]
search =~ /^[0-9]+/ and search = search.to_i
 
phones = {
  "me"        => "359880101020406",
  "Hitler"     => "+49 666",
  "Van Gog" => "+31",
  "devil"      => "666",
  "God"       => "1",
}
 
phones.search(search).each do |person|
  print "person:#{person.first}\tnumber:#{person.last}\n"
end

Random strings

Aug 19
2008
module Kernel
  def random_uc(size=16)
    Array.new(size){ rand(26)+65 }.pack('C*')
  end
  def random_lc(size=16)
    Array.new(size){ rand(26)+97 }.pack('C*')
  end
  def random_num(size=16)
    Array.new(size){ rand(10) }.join
  end
end
 
random_uc => "HRTWYJBJEZDKYPTJ"
random_lc => "onxkvkezqukspmcx"
random_num => "5862195932579950"

Converting Between Numeric Bases

Jul 31
2008

Sample .bashrc file:

# decimal to binary
alias d2b='ruby -e "puts ARGV.first.to_i.to_s(2)"'
# decimal to hexademical
alias d2h='ruby -e "puts ARGV.first.to_i.to_s(16)"'
# decimal to octal
alias d2o='ruby -e "puts ARGV.first.to_i.to_s(8)"'
 
# binary to decimal
alias b2d='ruby -e "puts eval(%q|0b| + ARGV.first)"'
# binary to hexademical
alias b2h='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(16)"'
# binary to octal
alias b2o='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(8)"'
 
# hexademical to decimal
alias h2d='ruby -e "puts eval(%q|0x| + ARGV.first)"'
# hexademical to binary
alias h2b='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(2)"'
# hexademical to octal
alias h2o='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(8)"'
 
# octal to decimal
alias o2d='ruby -e "puts eval(%q|0o| + ARGV.first)"'
# octal to binary
alias o2b='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(2)"'
# octal to hexademical
alias o2h='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(16)"'

Dracula (The Vampire King)

Jul 26
2008

TRAC – change SVN repository

Jul 07
2008

Go to your TRAC project dir and issue the following commands:

1. cd to_your_trac_project_dir

2. ruby -i -pe ’$_.sub! /(repository_dir\s=\s).+/, “repository_dir = NEW_repository_PATH”’ trac.ini

3. trac-admin . resync

Ruby system administration scripts

Mar 22
2008

OS users:

users = File.new("/etc/passwd").collect{|x| x.split(':')[0]}

Number of files in directory:

puts %x{ls}.split(/\n/).size
puts %x[ls|wc].split(/\s+/)[1]

Network interfaces:

inet = Hash.new{|h,k| h[k.split(/\s/)[0]] = k.scan(/addr:(\d+\.\d+\.\d+\.\d+)/)[0].to_s}
 
%x[ifconfig].to_s.split("\n\n").collect{|x| inet[x]}
 
# dump the structure: puts inet.inspect

to be continued….

How to find free domain name

Feb 22
2008
#!/usr/bin/perl
 
use strict;
use warnings;
 
my $dom = shift || die "missing name\n";
my $res = qx{whois $dom};
 
die $1 . "for '$dom'\n" if $res =~ /(no\s+whois\s+server\s+)/i;
die "$dom is free\n"    if $res =~ /no\s+match|does\s+not\s+exist/i;
die "$dom is reserved\n";
 
__END__
_whois 123456.com
123456.com is reserved
 
_whois _123456_.com
_123456_.com is free

Perl script opening (vim variant)

Feb 04
2008
#!/bin/bash
 
file=$1
 
if [ -e $file ]
then
        vim $file
else
        touch $file
        echo '#!/usr/bin/perl' > $file
        echo '' >> $file
        echo 'use strict;' >> $file
        echo 'use warnings;' >> $file
        echo 'use diagnostics;' >> $file
        echo '' >> $file
        chmod +x $file
        vim $file
fi

find command (practical books’ dir copy)

Feb 03
2008

Copy all those formats (pdf|chm|txt|html) from $HOME/Desktop to the current working directory:

find  ~/Desktop/ -iname "*.pdf" \
  -o -iname "*.chm" \
  -o -iname "*.txt" \
  -o -iname "*.html" \
-exec cp -v {} . \;

more about find command

HTML To WIKI converter

Feb 02
2008

Perl script (thanks to the author of HTML::WikiConverter):

#!/usr/bin/perl
 
use strict;
use warnings;
use HTML::WikiConverter;
use Perl6::Slurp;
 
my $file_name = shift;
 
-f $file_name or die "No file given!\n";
 
my $wiki = new HTML::WikiConverter(dialect=>'MediaWiki');
 
print $wiki->html2wiki(slurp $file_name);
 
__END__
 
  Supported dialects:
 
  DokuWiki
  Kwiki
  MediaWiki
  MoinMoin
  Oddmuse
  PbWiki
  PhpWiki
  PmWiki
  SlipSlap
  TikiWiki
  UseMod
  WakkaWiki
  WikkaWiki

Calendar

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Tags