Thursday, June 28, 2007

TO know current shell

echo $SHELL
wrong as it gives the default shell path as set in the $SHELL variable
example:
mycomp@mydomain:/proc$ echo $SHELL
/bin/bash
mycomp@mydomain:/proc$ sh
$ echo $SHELL note :shell is sh here not bash
/bin/bash

SOlution use $$ which qives pid of current shell
ps -eaf | grep $$ | head -1 | tr -s ' ' ' ' | cut -f8 -d" "
it includes 5 steps
1 & 2 will get the lines of process of (current)shell and processes spawend by the current shell
i.e
$ ps -eaf | grep $$
tbxk67 20253 19978 0 13:35 pts/3 00:00:00 bash
tbxk67 21049 20253 0 14:01 pts/3 00:00:00 ps -eaf
tbxk67 21050 20253 0 14:01 pts/3 00:00:00 grep 20253
3) head -1 : to Extract the first line
NOw we need the last column for that we have to cut it But we need a delimiter for that
lets use single space for that
but the output which we have has multiple spaces what to do ??
4) we can translate multiple Spaces to single Spaces
tr command Very useful
cat file7 | tr '[A-Z]' '[a-z]' > file8 changes all Capital letter to small leters

tr [ options ] [ set1 [ set2 ] ]
options -d to delete ,ex cat temp | tr -d [123456789] delete all numbers
-s to squezze (to remove repetation : we will use this )
cat file | tr -s ' ' ' ' >newfile
or tr -s' ' ' ' < file9 > file10
repeated element of set 1 will be changed to single instance of set2

5)STEP is to cut on the basis of -d" " get -f8 (8 field )
example of cut
cut -f4-7 -d"" foo # cuts fields 4, 5, 6, and 7 from foo.
cut -f5- -d: foo # would cut from field 5 to the end of the line from foo delimitter is coclon

No comments: