Wednesday, May 04, 2005

HowTo Shell Scripts :dollar mania,backtricks and set

1.>Dollar Mania
Echo $0 will show the command itself
Echo $1 will show the first argument
Echo $$ will show the PID
Echo $? will show the last command status whether it succeeds or not.
Echo $* will show all the arguments
Echo $# will show the number of arguments

=====================vi lerndollar========================
#this shell script will tell u about various $ options
#$0 represents the command itself
#$1 will tell u the first arguments
#$* or $@ will show u all parameters or arguments
#$# will tell u bout no of arguments
#$? will show u the statuts of the last command
#$- will show u if u have used any options
#$$ will tell u the process id if its 0 success 1 failed
echo "Script name is [$0]"
echo "First Parameter is [$1]"
echo "Second Parameter is [$2]"
echo "This Process ID is [$$]"
echo "This Parameter Count is [$#]"
echo "All Parameters [$@]"
echo "The FLAGS are [$-]"
echo "The status of the last command is [$?]

$ lerndollar a b cScript name is [lerndollar]
First Parameter is [a]
Second Parameter is [b]
This Process ID is [7503]
This Parameter Count is [3]
All Parameters [a b c]
The FLAGS are []
The status of the last command is [0]
2.) BackQuote Backtrick or CommandSubstitution
$ echo date ß will echo literally
$ echo `date` ß will now show u the date
Wed May 4 11:41:46 GMT 2005
3.) Using BACKSPACE as used in DOS
$ stty erase [hit backspace]
Now urs backspace will act as like dos…wont give u garbled character….
4.) The magic of set
The command "$set" we use generally to see the system variables.
=========== $set ==============
$ set
HOME=/data9/home/hpqar1
HZ=100
IFS=

LOGNAME=hpqar1
MAIL=/usr/mail/hpqar1
MAILCHECK=600
OPTIND=1
PATH=/usr/bin:
PS1=$
PS2=>
SHELL=/bin/sh
TERM=xterm
TZ=GMT+5
_INIT_NET_STRATEGY=none
_INIT_PREV_LEVEL=S
_INIT_RUN_LEVEL=3
_INIT_RUN_NPREV=0
_INIT_UTS_ISA=sparc
_INIT_UTS_MACHINE=sun4u
_INIT_UTS_NODENAME=venus
_INIT_UTS_PLATFORM=SUNW,Ultra-5_10
_INIT_UTS_RELEASE=5.8
_INIT_UTS_SYSNAME=SunOS
_INIT_UTS_VERSION=Generic_117350-18
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but set can be used to assign positional parameter.Lets discuss more…
$ who am i
hpqar1 pts/2 May 4 10:58 (192.168.5.227)

but ………
$ set who am i
$ echo $1
who
$ echo $2
am
$ echo $3
i

That means here set sets the who am I command as positional parameters making “who” as the first parameter as on.
This property of set command is very useful. We’ll use the backtrick( in sec 2) with this property of set now.

Now suppose we say
$ set `who am I` ß literally means “set hpqar1 pts/2 May 4 10:58 (192.168.5.227)”
or we can say echo $1 will show hpqar1 and so on….
Lets check it….
$ set `who am i`
$ echo $1
hpqar1
$ echo $2
pts/2

Now see a nice example on the above usage…
~~~~~~~~~~~~~~~~~~~ vi welcome ~~~~~~~~~~~~~~~
set `who am i`
echo Hello $1 working in the terminal $2
echo Its now $5 and today is $3 $4
echo And Yah...I have hacked your machine...
echo Urs IpAddress is $6

~~~~~~~~~~~~~~~~~~~~~~ $welcome ~~~~~~~~~~~~~
Hello hpqar1 working in the terminal pts/2
Its now 10:58 and today is May 4
And Yah...I have hacked your machine..
Urs IpAddress is (192.168.5.227)
[ to be continued………..]

Labels:

0 Comments:

Post a Comment

<< Home