Making your own custom command for a script file which can be executed from any user.

05 / Oct / 2011 by Anuj Aneja 2 comments
One way to execute the script file via command can be making of alias and put it in .bashrc , but this will only be available to that user. So for creating a user-defined command which can be executed from any where for any user following steps need to be followed:
1. Make a script file which has the set of commands to be executed and add ‘x’ permission for all.e.g.

Here is a script file(/home/anuj/myscript/myscript.sh) which echo’s current directory on the terminal.

[java]echo $PWD[/java]

2. Make a symbolic link  mycommand pointing to myscript.sh in /usr/bin or /sbin/ or /usr/sbin directory  (Differences between them can referred from here).

[java]
cd /sbinln -s /home/anuj/myscript/myscript.sh mycommand
[/java]

Now you can use command “which” to see which file will executed if we type the command named “mycommand”

[java]

which mycommand

Output:  /sbin/mycommand

anuj@intelligrape:/tmp$ mycommand

Output:  /tmp/

[/java]

Its done! 🙂  Next question would be where we can use it to make our life easier??. Well ! Generally we have deployment script for that we need to switch to user and then we need to run the script file instead of doing that we can make a softlink of the script file and put it in the /sbin or /usr/sbin etc. Like some software packages like apache put softlink to there script file in /sbin.e.g.  a2ensite is a softlink to a2enmode in /usr/sbin which is a executable script file.
There can be some better way to achieve the same so suggestions are most welcomed.

Hope this helps you guys !

Anuj Aneja
Intelligrape Software

FOUND THIS USEFUL? SHARE IT

Tag -

Linux

comments (2)

  1. Bhagwat Kumar

    Nice Blog.

    However creating soft links for too many commands will be irritating instead I would like to move all the custom script files that are supposed to be globally available in a folder and somewhat change the PATH environment variable to include that folder in command search path.

    There are few files like /etc/profile, /etc/bash.bashrc etc. which are executed for each new shell(any user). We can set the PATH variable here so that it will be included in all the user’s PATH variable.

    I appended the following at the end of /etc/bash.bashrc file (sudo vim /etc/bash.bashrc) :

    PATH=$PATH:/home/bhagwat/globalScripts

    After then it looks for the scripts in above mentioned directory as well.

    Reply

Leave a Reply to Bhagwat Kumar Cancel reply

Your email address will not be published. Required fields are marked *