Tuesday, May 31, 2005

bash version of colorise python script

Well not exactly it only does a word not a string but i guess you get the idea.
Here is the python version.
script in python


#!/bin/sh

col=""
function colorize {
case "$1" in
"red" )
col=1;;
"green" )
col=2;;
"yellow" )
col=3;;
"blue" )
col=4;;
* )
echo "wrong color dude"
exit;;
esac

returnstring="\033[;3${col}m${2}\033[0m"
echo -e $returnstring

}

strings=($*)

for(( i=1;i<$#;i++))
do
colorize $1 ${strings[$i]}
echo -en " "
done
echo




Searching forward through history

Well the key to do is ctrl-s but thats already bound to stop for the tty.
So we undef that and use it :).

$cat .bash_profile |grep stty
stty stop undef start undef
(We press ctrl-s )
(i-search)`':

Substitution in the previous command

Thanks to nujoom,there is one more bash trick.Only thing it works for the immediately previous command.The following sequence would end up opening the file in 'vi'.

$ls bajajautofinance
bajajautofinance
$^ls^vi
vi bajajautofinance

Sunday, May 29, 2005

shopts

#shopt -s cdable_vars
#fd=/home/kalyan/fun/bash/
#cd fd
/home/kalyan/fun/bash/
#pwd
/home/kalyan/fun/bash

Learning the Bash shell

Got my hands on the third edition of this book...
Really good.
So for sometime my glob would include stuff i try out reading this book..

So here is something on aliases and options (set -o)

$set -o ignoreeof
'if you type ctrl-d now'
$Use "logout" to leave the shell.
$set -o noclobber
$echo hi > temp.txt
$cat temp.txt
hi
$echo sddsshi > temp.txt
-bash: temp.txt: cannot overwrite existing file
$echo sddsshi > temp.txt
-bash: temp.txt: cannot overwrite existing file
$echo sddsshi > temp.txt
-bash: temp.txt: cannot overwrite existing file
$rm temp.txt
$set -o noglob
$ls *
ls: *: No such file or directory
$set -o ignoreeof
$set -o nounset
$ls $ASD
-bash: ASD: unbound variable
$cd mybash
-bash: cd: mybash: No such file or directory
$alias
alias links='links -http-proxy rio:8088'
$alias mybash=/home/kalyan/fun/bash/
$cd mybash
-bash: cd: mybash: No such file or directory
$alias cd='cd '
$cd mybash
$pwd
/home/kalyan/fun/bash

Friday, May 27, 2005

Routing for localhost

Cant believe it but needed this to be done to ping localhost on vmware ...O o O

# ip route add 127.0.0.0/8 dev lo

Thursday, May 26, 2005

A thing about sed regex

Okay so on the left side you need to escape things like "(" "?" which are needed for example:

To remove - from -bash from ps axo command listing ,i needed to do this.

#ps axo command|sed -ne "1d;s/^-\(.*\)/\1/p"


And to make that thing optional,dont ask me why i did that ..yeah so to print all commands and to remove - from -bash when encountered.

#ps axo command|sed -ne "1d;s/^-\?\(.*\)/\1/p"

command command

# ls
awk complete2.sh complete.sh downloads medrec-1.1 ps.sh rmmod.sh test.sh url.sh
# command ls
awk complete2.sh complete.sh downloads medrec-1.1 ps.sh rmmod.sh test.sh url.sh


Now command executes a command yeah " Run command with args suppressing the normal shell function lookup"

So my deduction is color gotta be a shell function ;)

Monday, May 23, 2005

Basic usage of awk

I keep forgetting this,so i posted this.This is how you change the the way awk tokenizes...Here I use '/' instead of whitespace

echo "http://www.google.com/test" | awk -F/ '{print $4}'


My remote package installer ;)

helps u when you dont want to copy urls from the Redhat RPMS directory .
It depends heavily on the links output so it maynot work always..
Didnt work for vedanta... :D

#!/bin/bash
url="http://hostname/AS3u3/RedHat/RPMS/"
package=`links --dump ${url} |grep $1 |awk '{print $3}'`
rpm -ivh ${url}$package

Thursday, May 12, 2005

My Package Remover

It all started with me wanting to clean up a system with zillion packages .
After an half ass attempt to find out what are the packages with biggest size,vedanta pointed me to the rpm's --queryformat option.

So this solved part of the job,finding out what packages to remove.
So here it is .

$rpm --queryformat="%{size} %{name}\n" -qa |sort -n


After this obviously when you want to remove a package ,it takes time as the package maybe required by other packages.So i thought of writing a small bash script to do it.So here goes


#!/bin/sh
removepackage()
{
local fullpackage=$*
if rpm -e $fullpackage 2>/dev/null
then
echo "Removed the damn package"
exit
else
rpm -e $* 2>&1 | awk -F")" '{print $2}' > /tmp/removepackages
while read package

do
fullpackage="$fullpackage $package"
done < /tmp/removepackages
fi
echo $fullpackage
removepackage $fullpackage
}

removepackage $*



So thats it just run it with the a package name as an argument and toast your system.
By and by got to know that if you use a pipeline in the while loop above fullpackage scope wont last after the while loop.Bash will put the value it has before it entered the loop thats because while is done using a subshell.
But how does it matter if its a pipe or a redirection??......

Wednesday, May 11, 2005

Using ebuild

Now i wanted to try out d4x on vedanta's (http://vedanta.freecoolsite.com) recommendation so i tried to emerge it.But the compile failed .So i looked up gentoo's bugzilla and found the cause.gtk+ 2.6 has a function with the same name as one used by d4x so there is a conflict.

So i got the patch ( http://bugs.gentoo.org/attachment.cgi?id=49573 ) from the bugzilla and followed these steps to install the package.

$ebuild /usr/portage/net-misc/d4x/d4x-2.5.0.ebuild fetch
$ebuild /usr/portage/net-misc/d4x/d4x-2.5.0.ebuild unpack
$cd /var/tmp/portage/d4x-2.5.0/work/d4x-2.5.0final/
$patch -p0 < /tmp/d4x.patch $ebuild /usr/portage/net-misc/d4x/d4x-2.5.0.ebuild compile $ebuild /usr/portage/net-misc/d4x/d4x-2.5.0.ebuild install $ebuild /usr/portage/net-misc/d4x/d4x-2.5.0.ebuild qmerge


That's it. Enjoy :)

Tuesday, May 10, 2005

Silencing grep

$ ps aux|grep xinit
kalyan 28918 0.0 0.0 1484 472 pts/2 R+ 17:37 0:00 grep xinit
$ ps aux|grep -q xinit

the -q option to grep is useful when you dont want to see the output in a script but want to just test for a match using the return value.

if $( ps aux|grep xinit); then echo "yo"; fi

Now this will try to run the output of that command $( ps aux|grep xinit)
and bash will say

-bash: kalyan: command not found

but now when you write a for loop when you have to use it else bash will complain.
So the right thing to do is

$ if ps aux|grep -q xinit; then echo "yo"; fi

$for mycom in $(ps aux|grep xinit); do echo "yo"; done


I know the examples are weird ;)

A thing about nfs

Though I thought i should only blog about bash related stuff,i like breaking the rules sometimes.
In case you want a read write mount then you have to set up a few things at server end , basically not to map the root on the client to anonymous on the server.

Here is the thing you'd like to add to /etc/exports

/storage *(rw,no_root_squash,sync)

Sunday, May 08, 2005

Finding use

Finding use for using find

$ find -size +100000k
./.evolution/mail/local/Inbox
./fun/kernel/src/linux-2.6.11-gentoo-r4/cscope.out
./fun/kernel/src/linux-2.6.11.7/cscope.out
./eclipse.tgz


$ find -size +100000k -and -name 'In*'
./.evolution/mail/local/Inbox


$ find -size +100000k ! -name 'In*'
./fun/kernel/src/linux-2.6.11-gentoo-r4/cscope.out
./fun/kernel/src/linux-2.6.11.7/cscope.out


$ find -size +100000k -or -name 'cscope_maps.vim*'
./.evolution/mail/local/Inbox
./fun/kernel/src/linux-2.6.11-gentoo-r4/cscope.out
./fun/kernel/src/linux-2.6.11.7/cscope.out
./.vim/plugin/cscope_maps.vim

$ find -amin -1 -name '*.c'
./test.c

$ find . -name "test.c" -and -maxdepth 1 -exec ls -l '{}' \;

Thursday, May 05, 2005

ulimit

$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7168
virtual memory (kbytes, -v) unlimited

For core file generation

$ulimit -c unlimited

Named pipes

$mkfifo pipe

On terminal 1

$cat pipe


On terminal 2

$cat > pipe

Now you know what i mean

Wednesday, May 04, 2005

Using file test - Part 2

if [ -p "/dev/initctl" ]
then
echo "I am the pipe file"
fi

if [ -b "/dev/hda" ]
then
echo "I am the block file"
fi

if [ -c "/dev/vc/1" ]
then
echo "I am the character file"
fi

Tuesday, May 03, 2005

Using file test - Part 1

#!/bin/bash
#filetest.sh

if [ -x "filetest.sh" ]
then
echo "filetest.sh is executable"
else
echo "filetest.sh is not executable"
fi


$ sh filetest.sh
filetest.sh is not executable
$ chmod +x filetest.sh
$ ./filetest.sh
filetest.sh is executable


Now time for something interesting ..let me find out the executable files inside my home directory using this.Well i dont like the find options.

cat filetest.sh
#!/bin/bash

if [ -d "$1" ]
then
:
else
echo "A directory expected"
exit 1;
fi

for myfile in $( find $1 )
do
if [ -x "$myfile" -a -f "$myfile" ]
then
echo "$myfile is executable"
fi
done

A bash webserver

Dancing bars

while true; do if(( count % 2 == 0)); then tput clear; echo '\'; else tput clear ;echo "/"; fi; ((count++)); done

Now i'll use switch case

#!/bin/sh

printbar()
{
case $1 in
'0') echo "/";;
'1') echo "|";;
'2') echo '\';;
'3') echo "-";;
'4') echo "|";;
'5') echo '\';;
'6') echo "-";;
*) echo "not found";;
esac
}
clear
count=0
while true
do
tput cup 10 10
printbar $(($count % 7))
(( count++ ))
done

Constantly monitor disc usage

while true;do du -sm .;sleep 1;done

Monday, May 02, 2005

The truth about -z and -n

if [ -z $null ]
then
echo '$null is null'
else
echo '$null file is not null.'
fi

if [ -n $null ]
then
echo '$null file is not null.'
else
echo '$null is null'
fi

The output is
$$null is null
$$null file is not null.

Not quite as we expected,So we learn that we should always quote the tested string and -n needs it.

if [ -z $null ]
then
echo '$null is null'
else
echo '$null file is not null.'
fi

if [ -n "$null" ]
then
echo '$null file is not null.'
else
echo '$null is null'
fi