Thursday, June 23, 2005

Mama i am coming home..

Some times mama maynot be back home to receive you ,in that case you become a zombie .Mama need to call wait to make sure all the kids are back.

%cat checkzombie.sh
#!/bin/bash

./zombie1 &
./zombie
wait
%cat zombie1
#!/bin/bash

sleep 60
%cat zombie
#!/bin/bash

sleep 2


If you dont call wait at the end,the script will return before zombie1 is done as
zombie was executed as a background job.

Job Control

Job control lets you suspend _lot of jobs and then bring them into foreground.
Ctrl-Z takes the job into background...
But you can get it back by 'fg %n' where n is the ID of the job.

$ jobs
[1]- Stopped ./trace.sh
[2]+ Stopped ./trace.sh
$ fg %2
Coming soon are the traps in bash..beware..:)

Wednesday, June 22, 2005

A text browser with links --dump

This browser implemets quit (q) , back (b) and url.And a hidden command called 'd' which dumps history.More importantly i use the bash arrays.On L.H.S its HISTORY[$i] where as on R.H.S its ${HISTORY[$i]} and the length of array is ${#HISTORY[@]}...

#!/bin/bash
HISTORY=()
BACKINDEX=0

function updatehistory() {
numvalues=${#HISTORY[@]}
if (( $numvalues < 3 ))
then
HISTORY[$numvalues]=$1
else
index=$(( numvalues -1))
for (( i=0;i<$index;i++))
do
j=$(( i+1))
HISTORY[$i]=${HISTORY[$j]}
done
i=$(( i++))
HISTORY[$i]=$1
fi
}



function dump_history(){

numvalues=${#HISTORY[@]}

for (( i=0; i < $numvalues; i++))
do
echo ${HISTORY[$i]}
done
}

while true
do
echo "Enter a url.q for quit and b for back"
read choice

case $choice in

q) exit ;;

b) BACKINDEX=$(( BACKINDEX+1))
numvalues=${#HISTORY[@]}
INDEX=$(( numvalues - BACKINDEX -1 ))
links -http-proxy proxyserver:8088 -dump ${HISTORY[$INDEX]}
;;

d) dump_history;;

*) links -http-proxy proxyserver:8088 -dump $choice
if (( $? == 0 ))
then
updatehistory $choice
fi
BACKINDEX=0
;;
esac

done

Counting lines with grep

Interesting option called -c , which lets you suppress output and oonly print the count of lines that matched so here is a replacement for "wc -l".

%cat bajajautofinance |grep -c ^.*$
11
%cat bajajautofinance |wc -l
11


Tuesday, June 21, 2005

A menu based bash directory browser

This could be useful when you have long pathnames to browse around and these pathnames have similar names...so CDPATH may not work.

#!/bin/bash

function cd()
{
if [ -z "$1" ]
then
pushd $HOME
return 0
fi
THEDIR=$(echo $1|sed -e "s/\~/$(echo $HOME|sed -e 's/\//\\\//g')/g")
pushd $THEDIR >/dev/null 2>&1


}

function lsd()
{
if [ -z "$1" ]
then
select mydir in $(dirs -v|awk '{print $2}'|sort -u)
do
cd $mydir
break
done
else
if [ -d "$1" ]
then
select mydir in $(find $1 -type d)
do
cd $mydir
break
done
fi

fi

}

Friday, June 17, 2005

Blocks and IO

{

echo "Hello World"

echo "Bye World"

} > output.txt


And here is some bad news..Daniel Robbins(Gentoo Founder) joins MS...isn't that cheap ;)

Monday, June 13, 2005

Basic usage of select

Select creates a menu for you and its very simple to use.Here is an example

#cat select.sh
select test
do
echo $test
done
#./select.sh item1 item2 item3
1) item1
2) item2
3) item3
#? 2
item2
1) item1
2) item2
3) item3

Also this creates an infinite loop...so you need a break :)

Friday, June 10, 2005

Using eval

Here is some recursive(?) cool usage of eval.Thanks to Chandra..

$p=i
$i=1
$CM1=World
$CM=Hello
$eval HOST=\$CM\$$p
$echo $HOST
Hello1
$eval HOST=$CM\$CM$i
$echo $HOST
HelloWorld
$eval HOST=\$CM$i
$echo $HOST
World

Bash string operators

#!/bin/bash

while read foo
do
echo ${foo%%:*}
case ${foo%%:*} in
cm1*) export CM1=${foo##*:};;
esac

done < config.txt

echo $CM1

And the config.txt has syntax like
tunnel_port : 9999

Enjoy :)

Tuesday, June 07, 2005

Alternative for loop syntax

for(( i=1;i<6;i++))
do
echo $i
done