Internal Unix commands are built with the shell and hence it will run in the same shell.
eg: cd
External Unix commands will start a new shell and then it gets executed
eg: ls
Note: If you execute a shell script, it will create or fork a new shell and then execute the commands present in the script file. You can also execute the shell script in the same shell by using source or . infront of the shell script.
eg:
sujay@laptop:~/scripts$ cat test.sh
cd /
echo "Current dir is `pwd`"
sujay@laptop:~/scripts$ test.sh
Current dir is /
sujay@laptop:~/scripts$ pwd
/home/sujay/scripts
sujay@laptop:~/scripts$ source test.sh
Current dir is /
sujay@laptop:/$ pwd
/
Monday, January 25, 2010
Saturday, January 23, 2010
How to run a unix shell script from any directory
To run a script from anywhere inside UNIX directory, you need to include the script absolute directory in the PATH variable. After that without using the pathname we can execute the script successfully.
eg:
In .profile, update PATH=$PATH:/home/sujay/scripts and source the .profile
Script Loc: /home/sujay/scripts/
Script Name; anywhere
sujay@laptop:~$ anywhere
++ pwd
+ echo 'Executed from /home/sujay'
Executed from /home/sujay
eg:
In .profile, update PATH=$PATH:/home/sujay/scripts and source the .profile
Script Loc: /home/sujay/scripts/
Script Name; anywhere
sujay@laptop:~$ anywhere
++ pwd
+ echo 'Executed from /home/sujay'
Executed from /home/sujay
Subscribe to:
Posts (Atom)