Saturday, January 16, 2010

Make a shell script work like a UNIX command

        Whenever we write a shell script, we run the shell script by using the syntax ./first, assuming first is the name of the shell script. In this way, the user always has to be in the directory containing the script in order to run this. If the user is in a different directory, the user has to give the full path to run the command ~xxx/abc/first. 

    Now, as the number of scripts keeps increasing, it becomes tougher to run the script in this way, moreover if they are kept in different directories. Let's see how we can run this script like any other UNIX command.

The following are the steps:

1.  Create a directory bin under the home directory.
2.  Place the shell script file in the bin directory.
3.  Update the PATH variable.
        PATH=$PATH:~gpr/bin  (assuming gpr is the username)

   The content in the file first  is as shown below:

[XXX# ~gpr/bin]# cat first
echo "Welcome to shell scripting"
[XXX# ~gpr/bin]#

  Once this setting is done, you can use the first script being in any directory in the account, like any other user command as shown below:

[XXX# ~gpr]# first
Welcome to shell scripting

    From now on, any new script created needs to be put in the bin directory. As soon as it is put, it can be used like any other unix command.

  In case other members of the group wants to use these scripts, they need to do the Step 3 alone in their user account.

Related Posts Plugin for WordPress, Blogger...

No comments:

Post a Comment