Wednesday, May 26, 2010

Reading and Writing to Excel Spreadsheets in Python

I find it useful.
excel-spreadsheets-and-python

Tuesday, May 25, 2010

Script to log stepwise execution and output of another bash script into syslog

The input arguments for this script would be another script with its input arguments. First, this script stores the stepwise execution and the output of the called bash script into a temporary file. When the called bash script finishes its execution, this script reads the temporary file and logs them into syslog. We are using a temporary file because if we pipe the output of the called script we will lose the true return value of that script.
#!/bin/bash

tmpfile=$(mktemp)
(/bin/bash -x $@ 2>&1) > $tmpfile
retval=$?
cat $tmpfile | logger -t "logger [$1]"
rm -f $$tmpfile

exit $retval
   
  

Tuesday, May 18, 2010

Display code blocks on web page

In my blog, almost all my posts have some code blocks. Whenever I tried to post some code, I had greate difficulties to show the code blocks on the web page as how it should be on the normal editor. Later I realized that we can do it easily with the help of <pre> tag and some more changes and here is the script to do that some more.
#!/bin/bash
usage ()
{
    cat <<EOF
Usage: $(basename $0) <code file>
EOF
exit 1;
}

# Check for the input argument
if [ $# -ne 1 ]; then
    usage
fi

if [ ! -e "$1" ]; then
    echo "ERROR: File ($1) does not exist"
    exit 1
fi

cat <<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>$1.html</title>
<meta content="MSHTML 6.00.6000.17023" name="GENERATOR">
<meta http-equiv="Content-Type" content="text/html; charset=unicode">
</head>
<body>
<pre>
EOF

sed -e 's/\&/\&amp;/g' -e 's/\"/\&quot;/g' -e 's/˜/\&tilde;/g' -e 's/>/\&gt;/g' -e 's/</\&lt;/g' $1

cat <<EOF
</pre>
</body>
</html>
EOF

Wednesday, May 12, 2010

Script to mount partitions from image file in linux

#!/bin/bash

usage ()
{
    cat <<EOF
Usage: $(basename $0) <image file>
EOF
exit 1;
}

# Check for the input argument
if [ $# -ne 1 ]; then
    usage
fi

sfdisk -l -uS $1 

sfdisk -l -uS $1 2>/dev/null | awk '
           BEGIN {
             i = 1
           }
           /sectors\/track$/ {
             split ($2, array, ":"); 
             imagefile = array[1];
           }
           /^Units = sectors of/ {
             secsize = $5;
           }
           !/#sectors|sectors\/track$|^$|^Units = sectors of/ && $4 != 0 {
             partoffset = secsize * $2;
             cmd = sprintf ("test -e /mnt/tttt%d", i);
             cmd1 = sprintf ("test -d /mnt/tttt%d", i);
             if (system (cmd) != 0)
             {
               cmd = sprintf ("mkdir /mnt/tttt%d 2>/dev/null", i);
               if (system (cmd) != 0)
               {
                 printf ("Error: Could not create the director /mnt/tttt%d.Please try with root privilege.\n", i);
                 exit;
               }
             }
             else if (system (cmd) != 0)
             {
               printf ("Error: /mnt/tttt%d is not a directory.\n", i);
               exit;
             }
             cmd = sprintf ("mount -o loop,offset=%d %s /mnt/tttt%d", partoffset, imagefile, i); 
             if (system (cmd) != 0)
             {
               exit;
             }
             printf ("Mounting: %s ---> /mnt/tttt%d\n", $1, i);
             i++;
           }'

  To run this script:
  root@ubuntu:~# mountImageFile.sh <image file>

  Run this script with root privilege.

Note: This script will not work for large partitions because of the limitation in sfdisk.
Reference: http://lists.samba.org/archive/linux/2005-April/013444.html & manpages awk and sfdisk