better bash debugging

I was trying to debug some bash scripts yesterday and learnt something new 😉
There are lot of things in bash that you can use to debug and to start with you can enable xtrace mode as follows:

set -x

With this set you will see that all the commands are printed along with all other information as they are executed.
This you can do for any line or for the function or for the whole script. Once you set the option you can turn it
off with

set +x

Now, once you have enabled this, you will see a lot of output, but you will still not see the function name and the line numbers. If you do want to see them then you will need to set the PS4 variable like below and bingo :

 export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
Enhanced by Zemanta

The Hidden PS

1
 for p in `ps L|cut -d' ' -f1`;do echo -e "`tput clear;read -p$p -n1 p`";ps wwo pid:6,user:8,comm:10,$p kpid -A;done

While going through the source code for the well known ps command, I read about some interesting things.. Namely, that there are a bunch of different fields that ps can try and enumerate for you. These are fields I was not able to find in the man pages, documentation, only in the source.

Here is a longer function that goes through each of the formats recognized by the ps on your machine, executes it, and then prompts you whether you would like to add it or not. Adding it simply adds it to an array that is then printed when you ctrl-c or at the end of the function run. This lets you save your favorite ones and then see the command to put in your .bash_profile like mine at : http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

Note that I had to do the exec method below in order to pause with read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
t ()

{

local r l a P f=/tmp/ps c='command ps wwo pid:6,user:8,vsize:8,comm:20' IFS=' ';

trap 'exec 66

exec 66 $f && command ps L | tr -s ' ' >&$f;

while read -u66 l >&/dev/null; do

a=${l/% */};

$c,$a k -${a//%/} -A;

yn "Add $a" && P[$SECONDS]=$a;

done

}

* View this command to comment, vote or add to favourites * View all commands by AskApache

commandlinefu.com

by David Winterbottom (codeinthehole.com)

Gmail Twitter Facebook Delicious Tumblr 1 person recommended this

Enhanced by Zemanta

Skyline with Red color – from mobile.

Have you ever wondered, how the red skyline would look if you did not have your favourite camera with you to capture it. I had only my Samsung Omni Windows Mobile with me to capture this, do comment on how they look 🙂

Red Skyline
Red Skyline
Red Skyline
Red Skyline
Red Skyline
Red Skyline
Red Skyline
Red Skyline
Enhanced by Zemanta