Tuesday, May 31, 2005

UNIX "basename" command

wats its doing PROGRAM_FILE=`basename $0 .sh`
in it a variable named PROGRAM_FILE is taken .now understand wats basename is doing.basename is an UNIX linux command to remove any unwanted parts.like as if we use command like…
$ basename my_script.sh.sh .sh
<< check for the intentional .sh which is written twice>>

wat it`ll do is that it will remove that extra .sh and will make it as my_script.sh.but if we give the command as
$basename my_script.sh .sh

wat will it do is that it`ll make it as only my script.look for the sample run from the system.
$ basename my_script.sh.sh .sh
my_script.sh
$
$
$ basename my_script.sh .sh
my_script

Labels:

Monday, May 09, 2005

The "environment" and the "export"

Here we’ll discuss bout the environment. Why do we need to set up an environment? Why do we need to export? Etc.
Well..when we export a variable then it got enlisted in the environment.lets try it…
$ set
HOME=/data9/home/hpqar1
HZ=100
IFS=

LOGNAME=hpqar1
MAIL=/usr/mail/hpqar1
.
[heavily edited]
.

_INIT_UTS_VERSION=Generic_117350-18
$

it’s the result b4 exporting a variable.

N see the result after exporting a variable named “name”
$ name=hasim
$ export name
$ set
HOME=/data9/home/hpqar1
HZ=100
IFS=

LOGNAME=hpqar1
MAIL=/usr/mail/hpqar1
.
[heavily edited]
.

_INIT_UTS_VERSION=Generic_117350-18
name=hasim
$
One thing to keep in mind that the new exported variable will lasts till duration of the shell. Why we need to export? Lets say we want to take some of our executable files to work fine from a user created directory named shellscript for this session.bcz for others they may be of no use. Then we will export as..

$PATH=/somedir/somesubdir/shellscript
But here in this case all executable will look for the specified path above only. Be aware of this thing.
So wat we have got is that environment is just a shared pool of information to the shell to have better control over things.

Labels:

How to send mail from unix

To send mail from UNIX account
At first create ur letter in a file named “letter.txt” using vi.
And then
$ mailx –t rakib004@yahoo.com < letter.txt
or create at once and to send it..

$cat letter.txt |mail –t rakib004@yahoo.com
Or
$echo “Hey sorry Pal...I cant put emoticons... “ | mail –t rakib004@yahoo.com

Full Format:(-s means SUBJECT,-c means TO,-b means BCC)
$cat letter.txt |mail –s "HI" -c rakib004@yahoo.com -b ma_hasim@excite.com

I want to get my name as sender? How to do?
--------------------------------------------
Use sendmail instead of mail or mailx.
Format:
$cat letter.txt|sendmail -F "yours mail id" "receipient`s mail id"

Labels:

Neutralizing metacharacter – a cute trick

First do a thing –type a command like this
$echo * these are ur current items in your current directory

And voilaaa…..urs * will be replaced by all the items in ur directory!!!
That means here “*” sends a different signal to the shell. Otherwise it would have been printed like
“* these are ur current items in your current directory”.
Now the question is how to neutralize this effect. We use “\” or “ ’ ” for that.
Say u wanna echo this –“*?*”
See how to do that--
$ echo *?*
bckup cal cuckoo dir lerndollar mkex ren sh2.sh sh3.sh splls welcome

$ echo \*\?\*
*?*

$ echo '*?*'
*?*
$

That means here that back slash and the quote neutralize the metacharacters.

Another point here sometimes while transferring folder or files from windows to Unix we face a great problem with those transferred items in UNIX as they have longgg file or directory names. Then always treat them with simple quote (‘) like
Cat ‘My Documents’ etc
Now lets make the things quite tough. Till now we know bout only two escapes. Simple quotes and backslash. But there is the big brother also .its double quotes. So the common question comes bout their performances. Ok…. we can say the backslash is the most soft among them. Then comes simple quotes n then double quotes. Lets see an example and then we’ll discuss further….

$ echo `ls`
bckup cal cuckoo dir lerndollar mkex ren sh2.sh sh3.sh splls welcome

( remember the usage of back quotes ---if u cant see go to http://mahasim.blogspot.com/2005/05/howto-shell-scripts-dollar.html )

$ echo '`ls` ls is treated as a $ command'
`ls` ls is treated as a $ command

$ echo "`ls` hi hello"
bckup
cal
cuckoo
dir
lerndollar
mkex
ren
sh2.sh
sh3.sh
splls
welcome hi hello

So the learning is that backslash demolish the meaning of the character after that. Simple quote demolishes the in-between characters .And double quote turns off all metacharacters except $,`,and \"

Labels:

How to reach directly to ur home directory

Use cd to go to your HOME directory.
$ pwd
/data9/home/hpqar1/mahasim/shellscript
$ cd
$ pwd

/data9/home/hpqar1

Labels:

Wats the difference between $* and $@

You probably know the difference between "$@" and $*. Both stand for "all arguments specified on the command line". "$@" does preserve whitespace, while $* does not.
Lets make this more clear.create a script named “quote” as follows…

========================== vi quote ==============
cat "$*"
cat "$@"
~
======================end==============

now make it executable.
Make another two files as follows
============vi test1=============
hi this is test 1


============= vi test2====================
this is test2


$ quote test1 test2
cat: cannot open test1 test2
hi this is test 1

this is test2

wats the hell is going on here..?? first its telling It cant open files.n then opening it…!!!
Actually wats happening inside is first Mr “$*” tried…. And expanded the command as “test1 test2”. That means it told the shell to cat a file named “test1 test2”. And this is wrong. Thus shell replied “ no no…there is no single file named ‘test1 test2’”. Then came another partner Mr “$@” inside the “quote” file. It took a different approach to expand things .wat it did is that it told the shell to cat two files named “test1’ and “test2”. And shell happily did that.
In brief
“$*” expands to “test1 test2”
“$@” expands to “test1” “test2”

Labels:

Thursday, May 05, 2005

How to open a site from command mode?

The command is like so…

C:\Documents and Settings\Abul.Hasim>
C:\Documents and Settings\Abul.Hasim>start iexplore http://mahasim.blogspot.com

Labels:

HowTo UNIX VI EDITOR : Some tricks and tips

1>WATS the basic difference to go to the command mode using “o”,”O”,”i” or “a”?
U r in CM and if u use “I” to switch to IM then where the cursor was before the words .in case of “o” it will start appending in the line below the cursor where it was and in case of “a” it will start appending just after the cursor position.”O” will start appending in the line above the cursor where it was
2>How to move cursor inside VI?
· alt+k -will take up
· alt+j-will go down
· alt+h- left
· alt+l- right
· alt+w-advance by one word and
· alt+b backtracks by one word at a time
· and if u at the midst of a longgggg word then alt +e to go to the end of that longgggggg word( for IM and CM)
· alt+x to remove the char under the cursor….mode will change to CM……..( IM)
· CM:alt+0 start of the line
3>Is there any undo in vi?
Yes,while in IM and u made any mistake u can use alt+u for undoing. But it will undo the whole work. Not the last word.
4> As we use cntrl+f in MSWORD to find any particular word.is there something like that in vi?
Yes,just go to CM. And then /, keep on pressing “n” to see the next occurrences of that word.

i am here
where are you?
is the any options there
any guy will come
and then any boy will leave
~
~
~
~
:/any

5>And then if I want to replace a particular word by any particular word then….?
Hmm…then use this command from CM …..( this will change that ‘any” to “ANY”)
i am here
where are you?
is the ANY options there
ANY guy will come
and then ANY boy will leave
~
~
~
~
~
:%s/any/ANY


6>And if I want to comment out all lines in my shell script then….?
%s/^/#/g

Labels:

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: