Python Basics

Debugging

http://docs.python.org/library/pdb.html
python -m pdb my.py

# launch pdb after a crash.
import pdb; pdb.set_trace()   

# compile without executing it
python -m py_compile test.py 

Lib Path

export PYTHONPATH=/home/python
$ python
Python 2.4.3 (#1, May 24 2008, 13:47:28) 
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/python', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/Numeric', '/usr/lib/python2.4/site-packages/gtk-2.0']

Useful

help(int)
dir(int)
locals()
globals()

Variables

everything in python is an object. 
All python variables hold value of references to objects.
parameter passing is by value.
String, numbers, tuples are immutable. 

Integers, floats, complex numbers, booleans
x = 5
y = 6
print `x`;
print "x is %i y is %i" % (x, y)
 

Lists

a=[1,4,5,6]
b=[1,"two",3,["a","b"],4]

Tuples

Same as lists but not changeable
a=(1,2)
b=(1,"two",3,["a","b"],4)

Dictionaries

a={1:"one", 2:"two"}
a["first"]="one";
list(a.keys())
a.get(1, "not availabe)

Sets

unorder collection of objects.
>>> x=set([1,2,3,2,4,1])
>>> x
set([1, 2, 3, 4])
>>> 1 in x
True
>>> 6 in x
False

Conditionals

if x < 10:
   y = 1
elif x > 12:
   y = 2
else:
   y = 3

Loops

For
for x in [1,2,3,4,5]:
  print x;
While
x=5
while x < 10:
  print x
  x=x+1

Functions

def func(x, y, z):
   print (x, y, z)
   print x, y, z
func(1,2,3)
mult=lambda x,y: x * y   # return is implicit
map( lambda x: x*x, [1,2,3,4])
filter(lambda x: x > 3, [ 1,2,3,4,5,6])

Class

class X:
  "Sample class"
  x=5
  def setVal(self,val):
    self.x=val

External Commands

os.system
import os
res=os.system("ls myfile");
if res>>8 != 0:
  print "err"
commands
import commands
>>> res, output = commands.getstatusoutput('ls /bin/ls')
>>> res
0
>>> output
'/bin/ls'
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x. 1 root root 105112 Feb  8  2011 /bin/ls'

Perl Basics

Debugging

 -w option
Debugger
perl -w -d my.pl  # with perl
perl -w -d -e "1;"    # no code

Variables

Perl has 3 kinds of variables:
  • scalar
  • array
  • hash 
Parameters can only be scalar. To pass other kind, it needs to be converted to a scalar (using reference).
scalar
$a=5; 
local $b='hello';
Array
my @num=(1,2,3);
my @str=("hello", "one");
my @mixed=("hello",1,2);
print $mixed[1];

@colors = ("red","green","blue");
($a, @somecolors) = @colors; # $a is first item; @somecolors are rest of @colors
(@somecolors, $a) = @colors; # @somecolors are @colors; $a is undefined
Hash
%fruit = (
       one => "apple",
       two => "orange",
       three => "peach"
       );

@key = keys %fruit;
@value = values %fruit;
print "$fruit{one} $key[1] $value[1]\n";

#hash of hashes
%HoH = (
  t1 => { a1 => "a1",
          a2 => "a2",},
  t2 => { b1 => "b1",
          b2 => "b2",},
);
print "$HoH{t1}{a2}\n";

Complex Data Type using Reference

# reference
$multiple = {
  m1 => { 
           onee => "app",
                  twoo => "ppa",
         },
         m2 => {
           test => "t",
    test2 => "y",
                },

};


print "$multiple->{m1}{onee} \n";

Conditionals

# if
if ( condition ){
  ...
}
elsif (condition ) {
  ...
}
else{
  ...
}

# unless
unless ( condition )
{
   ...
}

Loop

While
while (condition) { ... }
until (condition) { ... }
for
for ($i = 0; $i < 10; $i++)
{
   ...
}

for my $i ('a','b','c'){
  print $i;
}
foreach
foreach my $file (@mdfiles) 
{
   print "$file";
}

foreach (@array) {
   print "$_\n";
}

print $list[$_] foreach 0 .. $max;

foreach my $key (keys %hash) {
    print "The value of $key is $hash{$key}\n";
}

Operators

Numbers
 ==, !=, <, >, <=, >= 
Strings
 eq, ne, le, ge, lt, gt

Regular Expression

if (/tst/) { ... }         # true if $_ contains "tst"
if ($a =~ /hello/) { ... } # true if $a contains "hello"

s/a/b/;                    # replaces a with b in $_
$a =~ s/a/b/;              # replaces a with b in $a

External Commands

#1
$res = system("ls");

#2
my $exam=`mdadm --examine /dev/$dev 2>&1`; 

if ($? != 0){
   print "error\n";
}

Functions

sub setError($)
{
    $errorCode |= shift;
    return 1;
}
 

Bash Programming Basics

Debugging

Start up the shell with the -x option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.
#!/bin/bash -x        #on first line

Variable

a=5
export b='whatever'
c="$a $b"
d='$a $e'                                    # no substitution
local e=56

Array

list=(/tmp/m*)
echo ${#list[@]}
echo ${list[0]} 
echo ${list[@]}
echo ${list[*]} # same as @

Conditionals

Empty or unset variable
if [[ ${b:-z} == z ]]; then 
    echo "variable is empty or unset"; 
fi
String
if [ "a" = "b" ]; then
  echo yes;
else                                         #elif
  echo no
fi
multiple condition
if [ "a" = "b" -a "c" == "c" ]; then       # string = or ==
  echo "and condition true";
else                                         #elif
  echo no
fi

if [ "a" = "b" -o "c" == "c" ]; then       # string = or ==
  echo "or condition true";
else                                         #elif
  echo no
fi

if [[ $a == "b" && $b == "c" ]]; then
   echo yes;
fi

[[ -f $NATION_FILE ]] || fail "No nation file";
Numbers
if [ 5 -eq $? ]; then                         # numbers
  echo yes
fi
commands
if grep a * >/dev/null; then 
  echo yes; 
fi

Loop

For loop
for each in $(ls); do
  echo $each
done
While loop
COUNTER=0
while [  $COUNTER -lt 10 ]; do
    echo The counter is $COUNTER
    COUNTER=$((COUNTER+1)) 
done
Util
COUNTER=20
until [  $COUNTER -lt 10 ]; do
     echo COUNTER $COUNTER
     let COUNTER-=1
done

Read Each Line in a Variable

files=$(ls -1)
echo "$files"      # one line
echo $files | while read -r line; do echo $line; done    #one line
echo "$files" | while read -r line; do echo $line; done  # per line
echo "$files"|xargs  echo      #one line
echo "$files"|xargs -L1 echo   # per line

Functions

myecho()
{
  echo $*  
  echo $@
}

func_parm()
{
  [ -z $1 ] || echo parm missing && exit 1
  echo parm
}

Signal Handling

handle_int()
{
  echo "Control C"
}

trap handle_int SIGINT 

Manipulating Strings

http://tldp.org/LDP/abs/html/string-manipulation.html
${#String}
${string:position}
${string:position:length}
${string#substring}
${string##substring}

Redirection

   1>filename
      # Redirect stdout to file "filename."
   1>>filename
      # Redirect and append stdout to file "filename."
   2>filename
      # Redirect stderr to file "filename."
   2>>filename
      # Redirect and append stderr to file "filename."
   &>filename
      # Redirect both stdout and stderr to file "filename."
      # This operator is now functional, as of Bash 4, final release.

   M>N
     # "M" is a file descriptor, which defaults to 1, if not explicitly set.
     # "N" is a filename.
     # File descriptor "M" is redirect to file "N."
   M>&N
     # "M" is a file descriptor, which defaults to 1, if not set.
     # "N" is another file descriptor.

Operators

Numbers
arg1 OP arg2
       OP  is one of -eq, -ne, -lt, -le, -gt, or -ge.  These arithmetic binary operators return true if arg1 is equal
       to, not equal to, less than, less than or equal to, greater than, or greater than or equal  to  arg2,  respec‐
       tively.  Arg1 and arg2 may be positive or negative integers.  
String
string
-n string
       True if the length of string is non-zero.

string1 == string2
string1 = string2
       True if the strings are equal.  = should be used with the test command for POSIX conformance.

string1 != string2
       True if the strings are not equal.

string1 < string2
       True if string1 sorts before string2 lexicographically.

string1 > string2
       True if string1 sorts after string2 lexicographically.