Tuesday, June 7, 2011

Unix Scripting - bash scripts and initializing bash during startup.

Bash is a scripting language and also is  provided as a base application
from which the user can use tools like the vi editor, check on process,
navigate folders etc etc.
Here, I will write about simple tips and tricks to get maximum use of the
bash scripts.



Contents of .bash_history



 .bash_profile : this is a hidden file in the user's home directory, and is the first script to get executed automatically when you launch the"xterm" or the terminal.
To view the contents of this file ( as this is a hidden file - you'd have to type in ls -al ( a - yel )
A sample .bash_profile that I have configured is shown below, You will see that I have exported some environment variables like CVSROOT and CDPATH.

export CVSROOT="some value" will mean that this variable's value is visible
when the user types in echo $CVSROOT.
CDPATH is another variable that helps the UNIX user a lot. e.g: if there are 3 folders directories in /cygdrive/c/Downloads called "A", "B" and "C", the user would have to type in cd /cygdrive/c/Downloads/A , or
cd /cygdrive/c/Downloads/B, and similarly to navigate to C, he/she would
cd /cygdrive/c/Downloads/C
But, with the CDPATH="/cygdrive/c/Downloads", the user has to just enter "cd A" or "cd B" and "cd C", the OS would automatically prepend the /cygdrive/c/Downloads. This saves a lot of typing.


Experiment that by setting it to your favorite directory and you have to
reinitialize the .bash_history by executing it again by
typing . ./bash_history ( <dot> <space> <dot-slash> )
and now cd to your favorite directory and create 3 folders A, B, and C.
and now type in "cd A". you will notice that the control goes to the A
folder without the need to enter the full path.
Similarly check out "PATH", and "alias" commands that can be put into this
file.
Troubleshooting tips.
-Check that you executed the .bash_profile after editing. If not - close
this window - open a new window and see that this line gets executed.
- echo $CDPATH, to check that the variable is updated.
- insert a echo "hello world" in the beginning to see that this file gets
called during startup.

Exercise :
How do you see that when the user types in "ls", it should execute "ls
-l" i.e the long format.
Your new application is a new folder /cygdrive/c/Downloads/FavFolder, and you should be able to cd to /cygdrive/c and when you type in cd FavFolder - the control should automatically go to /cygdrive/c/Downloads/FavFolder.

No comments:

Post a Comment