Monday 30 May 2016

How Shell Locates the file

To run script, you need to have in the same directory where you created your script, if you are in different directory your script will not run (because of path settings), For e.g.. Your home directory is ( use $ pwd to see current working directory) /home/vivek. Then you created one script called 'first', after creation of this script you moved to some other directory lets say /home/vivek/Letters/Personal, Now if you try to execute your script it will not run, since script 'first' is in /home/vivek directory, to overcome this problem there are two ways first, specify complete path of your script when ever you want to run it from other directories like giving following command
$ /bin/sh   /home/vivek/first 

Now every time you have to give all this detailed as you work in other directory, this take time and you have to remember complete path. 
There is another way, if you notice that all of our programs (in form of executable files) are marked as executable and can be directly executed from prompt from any directory. (To see executables of our normal program give command $ ls -l /bin ) By typing commands like
$ bc
$ cc myprg.c
$ cal 

etc, How its possible? All our executables files are installed in directory called /bin and /bin directory is set in your PATH setting, Now when you type name of any command at $ prompt, what shell do is it first look that command in its internal part (called as internal command, which is part of Shell itself, and always available to execute), if found as internal command shell will execute it, If not found It will look for current directory, if found shell will execute command from current directory, if not found, then Shell will Look PATH setting, and try to find our requested commands executable file in all of the directories mentioned in PATH settings, if found it will execute it, otherwise it will give message "bash: xxxx :command not found", Still there is one question remain can I run my shell script same as these executables?, Yes you can, for this purpose create bin directory in your home directory and then copy your tested version of shell script to this bin directory. After this you can run you script as executable file without using command like
$ /bin/sh   /home/vivek/first 

Command to create you own bin directory.
$ cd
$ mkdir bin
$ cp first ~/bin
$ first
Each of above commands can be explained as follows:

Each of above commandExplanation
$ cdGo to your home directory
$ mkdir binNow created bin directory, to install your own shell script, so that script can be run as independent program or can be accessed from any directory
$ cp   first ~/bincopy your script 'first' to your bin directory
$ firstTest whether script is running or not (It will run)

vi editor command list

For this PurposeUse this vi Command Syntax
To insert new textesc + i ( You have to press 'escape' key then 'i')
To save fileesc + : + w (Press 'escape' key  then 'colon' and finally 'w')
To save file with file name (save as)esc + : + w  "filename"
To quit the vi editoresc + : + q
To quit without savingesc + : + q!
To save and quit vi editoresc + : + wq
To search for specified word in forward directionesc + /word (Press 'escape' key, type /word-to-find, for e.g. to find word 'shri', type as
/shri)
To continue with search n
To search for specified word in backward directionesc + ?word (Press 'escape' key, type word-to-find)
To copy the line where cursor is locatedesc + yy
To paste the text just deleted or copied at the cursoresc + p
To delete entire line where cursor is locatedesc + dd
To delete word from cursor positionesc + dw
To Find all occurrence of given word and Replace then globally without confirmation esc + :%s/word-to-find/word-to-replace/gFor. e.g. :%s/mumbai/pune/g
Here word "mumbai" is replace with "pune"
To Find all occurrence of given word and Replace then globally with confirmationesc + :%s/word-to-find/word-to-replace/cg
To run shell command like ls, cp or date etc within viesc + :!shell-command

For e.g. :!pwd