Pages

Saturday, July 13, 2013

how to debug bash shell script

 Unlike C/C++ applications, wherein you will you have to printf messages to debug, there is an easier and more detailed way of debugging shell scripts, specially in bash.

In your shell script, you can add

set -x

at the top of the script, this will make bash to print each command line it parse in your shell script, plus the result of the command as well.

Doing this permanently enable tracing, until you remove it or put "set +x" which is the default.

If you just want to see how it goes once, you can run your script "myscript.sh", try

$ bash -x myscript.sh

output looks like

+ var1=hi
+ var2='how are you'
++ paste hi how are you
paste: hi: No such file or directory
+ var3=
+ echo

"+" means, the sublevel of the command from the main shell script. i.e one + indicates the exact line of command as written in shell script, 2 +s indicate another command within the line of code you have written inside the script.

There is another, lesser debug info method.

$ bash -v myscript.sh

output looks like

#!/bin/bash
var1="hi"
var2="how are you"
var3=`paste $var1 $var2`
paste $var1 $var2
paste: hi: No such file or directory
echo $var3

This will just echo each line of the script as it parses, plus errors or echos from script if any.

Upgrade Java Runtime (JRE) on Debin/Ubuntu

Its high time you upgrade your JRE on your linux, as any version before Java 7 update 11 has serious security flaws. 

Firefox, by default blocks java based on the version you have installed. It means it doesn't allow you to run any java application using these security-issue-ridden java versions.

But, not the same with other browsers or your OS. You got to update it immediately.

NOTE: openjdk jre is also similar to oracle java, hence you can expect same issues with open jdk too. Until you get an update on openjdk, I suggest you use oracle java, well it is not too complex to install oracle java as it looks like on the surface.

Here is how to be done: (OS: Ubuntu)


  1. Download latest java from oracle site http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html
  2. if you are using 32 bit linux(most likely intel, x86 arch), choose the right file to download(rpm type for redhat based, .gz for debian based)
  3. untar using command 'sudo tar -xzvf jre-7u25-linux-x64.tar.gz -C /usr/java/
  4. To enable this java on Firefox, create soft link for a lib libnpjp2.so from your java directory to firefox plugin directory. It looks something like this:                                                                 "sudo ln -s /usr/java/jre1.7.0_25/lib/amd64/libnpjp2.so /usr/lib/firefox/plugins/"
  5. to verfiy, restart firefox, go to "about:plugins" in address bar,  see if you have following information: "

    Java Plug-in 1.7.0_25

    File: libnpjp2.so
    Path: /usr/java/jre1.7.0_25/lib/amd64/libnpjp2.so "

    that's it, you are upgraded!

    For google-chrome, you don't have to do anything if you have untarred the jre under /usr/java. To verify, restart your chrome, go to 

    chrome://plugins/

    Look for 

    Java(TM) Download Critical Security Update
    Java plug-in for NPAPI-based browsers.
    Name:Java Plug-in 1.7.0_25
    Description:Java plug-in for NPAPI-based browsers.
    Version:
    Location:/usr/java/jre1.7.0_25/lib/amd64/libnpjp2.so

    (don't forget to remove older versions of java)

Saturday, July 6, 2013

convert images to pdf


to convert one or multiple images to pdf file, use the command

$convert scan_*.jpg scanned_docs.pdf

convert command comes with package imagemagick

Monday, June 17, 2013

code complexity tool pmccabe

whenever we write code, it is advisable to test the complexity of the code we are writing.  This will help in source code maintainability, readability etc.

The best algorithm to do the same is cyclomatic complexity. (http://en.wikipedia.org/wiki/Cyclomatic_complexity)

One of the utility implementing this algorithm is pmccabe.

This ain't any IDE also! A simple CLI utility.


install:
sudo apt-get install pmccabe

usage:
<username>@<hostname>:~~$ find ./ -name *.c | xargs pmccabe  | sort -nr | head -10
49    49    116    361    134   <file>(362):<function>

The first column indicates the complexity score of each of the functions mentioned.
The wiki suggests that the complexity score should be kept below 10, and in inevitable cases, it can go upto 15. 

vlc player, enable en queue(add to playlist) on right click

By default, on double clicking on a song in folder, the song is opened in new vlc player session. There is no "add to playlist" option. To change this settings,


  1. On the vlc player, go to tools->preferences
  2. click on 'interface' (default) on the left menu,  on the  right, click on 'instances'
  3. check both 'allow only one instance' and 'enqueue when in one instance mode'

Tuesday, June 11, 2013

blogger can't import wordpress posts!

I was on wordpress before. Thought of moving to blogger, as wordpress want me to pay for each and everything more than what they consider 'basic'.


  1. I went my wordpress dashboard->settings->export
  2. exported an xml file (selected all as an option, to include posts, tags comments etc)
  3. Then I created a blogger, went to settings->other->import blog
  4. my xml files was just in Kbs, nevertheless, the blogger went on taking hours to import
  5. What I noticed is that it was stuck on first post itself to import. Then I learnt that to import from wordpress to blogger, a conversion is needed.
  6. So, go to wordpress to blogger convertor and get your xml file converted.
  7. Try repeating step #3. 
  8. It should work!

linux: search grep all man pages

If you would like to perform some operation on linux, and is not aware of utility that need to be run, you can search all available commands man pages to search.

There is a fine utility to do so, called apropos.

For example, I want to know all commands that operate on pdf files, like conversion from pdf to something else and vice versa, I will run
uid@host:~/apropos pdf
acroread (1)         - Adobe PDF Viewer for viewing PDF files on X Window System.
dvipdf (1)           - Convert TeX DVI file to PDF using ghostscript and dvips
evince-thumbnailer (1) - create png thumbnails from PostScript and PDF documents
fix-qdf (1)          - repair PDF files in QDF form after editing
foomatic-ppdfile (1) - Generate a PPD file for a given printer/driver combo
ghostscript (1)      - Ghostscript (PostScript and PDF language interpreter and previewer)
gs (1)               - Ghostscript (PostScript and PDF language interpreter and previewer)
gsnd (1)             - Run ghostscript (PostScript and PDF engine) without display
pdf2dsc (1)          - generate a PostScript page list of a PDF document
pdf2ps (1)           - Ghostscript PDF to PostScript translator
pdffonts (1)         - Portable Document Format (PDF) font analyzer (version 3.00)
pdfimages (1)        - Portable Document Format (PDF) image extractor (version 3.00)
pdfinfo (1)          - Portable Document Format (PDF) document information extractor (version 3.00)
pdfopt (1)           - Ghostscript PDF Optimizer
pdfseparate (1)      - Portable Document Format (PDF) page extractor
pdftocairo (1)       - Portable Document Format (PDF) to PNG/JPEG/PDF/PS/EPS/SVG using cairo
pdftohtml (1)        - program to convert PDF files into HTML, XML and PNG images
pdftoppm (1)         - Portable Document Format (PDF) to Portable Pixmap (PPM) converter (version 3.00)
pdftops (1)          - Portable Document Format (PDF) to PostScript converter (version 3.00)
pdftotext (1)        - Portable Document Format (PDF) to text converter (version 3.00)
pdfunite (1)         - Portable Document Format (PDF) page merger
ps2ascii (1)         - Ghostscript translator from PostScript or PDF to ASCII
ps2pdf (1)           - Convert PostScript to PDF using ghostscript
ps2pdf12 (1)         - Convert PostScript to PDF 1.2 (Acrobat 3-and-later compatible) using ghostscript
ps2pdf13 (1)         - Convert PostScript to PDF 1.3 (Acrobat 4-and-later compatible) using ghostscript
ps2pdf14 (1)         - Convert PostScript to PDF 1.4 (Acrobat 5-and-later compatible) using ghostscript
ps2pdfwr (1)         - Convert PostScript to PDF without specifying CompatibilityLevel, using ghostscript
qpdf (1)             - PDF transformation software

run "man apropos" for extra features with it.