Saturday, April 30, 2005

Already using the bash power :)

In the DBUS python code,i came across few examples which had a missing interpreter at the top .
So i sent a mail to mailing list and J5 let me contribute by asking me to submit the patch.
I really like the spirit of Free Software ,i started creating the patches after creating a couple of them.I said
"Wait"...Whats the point of bash if you keep repeating the samething. :)
So here is the script that creates the patch for the missing interpreter.

#!/bin/bash

for sourcefile in $(ls *.py )
do
if head -n 1 $sourcefile |grep -e '^#!' &> /dev/null
then
echo "$sourcefile has some interpretor"
else
cp $sourcefile ${sourcefile}_orig
cat ${sourcefile}_orig | sed -e '1i#!/usr/bin/env python\' -e '' > $sourcefile
diff -u ${sourcefile}_orig $sourcefile >> examples.patch
fi
done

Yo

Friday, April 29, 2005

strlen of Bash

$ echo ${#EDITOR}
9
$ echo $EDITOR
/bin/nano


Yo!


Thursday, April 28, 2005

Shell Parameter Expansion

This one is big so here I go.

${!prefix*}

This one expands the variables whose name starts with the prefix.One thing to note is that '*" is needed as shown below

$ echo ${!H*}
HISTCMD HISTFILE HISTFILESIZE HISTSIZE HOME HOSTNAME HOSTTYPE
$ echo ${!HISTCMD}

echo ${!HISTCMD*}
HISTCMD


${#parameter}
length in characters of the parameter but in case * and @ they refer to number of parameters.

$ cat parameter.sh
#!/bin/bash

echo ${#1}
echo $(#*}
$./parameter.sh abcdefghijklmnopqrstuvwxyz
26
1


${parameter#word}
${parameter##word}
try to match the word(it can be a pattern) against the parameter at the beginning and if it matches delete the shortest match

$ cat parametermatch.sh
#!/bin/bash

echo ${1#ab*}
echo ${1##ab*}
$ ./parametermatch.sh abcdefghi
cdefghi
$

${parameter%word}
${parameter%%word}
Samething as above but matches and deletes at the end
$ ./parametermatch.sh abcdefghi
abcdefg

$ cat parametermatch.sh
#!/bin/bash

echo ${1%*hi}
echo ${1%%*hi}

${parameter/pattern/string}

$ cat parametermatch.sh
#!/bin/bash

echo ${1/pattern/replacepattern}
$ ./parametermatch.sh hipattern
hireplacepattern

Bash Expansions

I'll come back to numbers before i post some cool features of bash.
Note that the '$' at the beginning of the line denotes the prompt.

1.Brace Expansion

$ echo alumn{a,i,ae,us}
alumna alumni alumnae alumnus

2.Tilde Exansion.

$ echo ~
/home/kalyan

3.Command substitution

$ ls $(which ls)
/bin/ls

Backticks also..

$ ls `which ls`
/bin/ls

4.Filename exapansion

$ ls s*
sd shift.sh string.sh stringcmp.sh
$ ls s?
sd
$ ls s[hd]*
sd shift.sh

5.Arithmetic Expansion
$ n=7
$ ((n=n+10))
$ echo $n
17

6.Paramenter expansion

$ ps=${ps:=lambadamda}
$ echo $ps
lambadamda


Bash Comparisons

So here is the bottomline as far comparing strings and integers in bash is concerned.
You want to compare strings use

[[ $stringa < $stringb ]]

You want to compare numbers use

(( $numbera < $numberb ))

This will take you a long way without much heartburns. :)
Now lets try out a few examples to understand this thing better.

#!/bin/bash
#string.sh
EQUAL="equal"

if [ $EQUAL == "equal" ]
then
echo "I am equal"
else
echo "I am not equal"
fi

$ ./string.sh
I am equal

Now lets try something more now see if 'a' < 'b'
#!/bin/bash
#stringcmp.sh
A="a"

if [ $A < "b" ]
then
echo "a is lesser than b"
else
echo "Something went wrong"
fi


$ ./stringcmp.sh
./stringcmp.sh: line 5: b: No such file or directory
Something went wrong

Ah so we cant do that. :) . So we try '[[ ]]'

#!/bin/bash
#stringcmp.sh
A="a"

if [[ $A < "b" ]]
then
echo "a is lesser than b"
else
echo "Something went wrong"
fi

$ ./stringcmp.sh
a is lesser than b

So a more comprehensive example

#!/bin/bash
#stringcmp.sh
A="a"
C="c"
B="b"

if [[ $A < "b" ]]
then
echo "a is lesser than b"
else
echo "Something went wrong"
fi
if [[ $C < "b" ]]
then
echo "Something went wrong"
else
echo "c is not lesser than b"
fi
if [[ $B < "b" ]]
then echo "Something went wrong"
else
if [[ $B == "b" ]]
then
echo "b is equal to b"
fi
fi


$ ./stringcmp.sh
a is lesser than b
c is not lesser than b
b is equal to b

Numbers are pretty straightforward as they only work with circular parenthesis "(())".
So you cant use the examples listed above to compare numbers.