10

I have shell script for monitoring local area traffic for my system. Now I want to install it, and want to run like other unix command

muru
  • 197,895
  • 55
  • 485
  • 740
ADR
  • 323
  • 2
  • 4
  • 9

3 Answers3

14

The usual location to install local scripts is /usr/local/bin or /usr/local/sbin. See man hier for details of the directory structure.

These directories are usually included if the corresponding /usr/bin or /usr/sbin directories are in the path. See man eviron for information on the standard environment variables including PATH.

The directory hierarchy for many distributions is document in the heir man page. It can be displayed with the command man heir

/usr/local/bin is for programs that all users should be able to run. It is equivalent to /bin and /usr/bin. Normally, most users will not have these on their path.

/usr/local/sbin is for programs that are used for system administration. It is equivalent to /sbin and /usr/sbin.

BillThor
  • 4,698
10

The way I would solve this (with my fairly rudimentary linux skillz) is to make an alias to the shell script.

First ensure the shell script is executable.

chmod u+x,g+x script.sh

Then, edit your .bashrc file like following:

cd
vi .bashrc

Add this towards the bottom. (I think you can also add this in a specific alias file, such as .bash_aliases, but I don't.)

alias commandtorun='/home/user/script.sh'

Here, commandtorun will be the command you type in to run the script, and '/home/user/script.sh' is the path to the script.

To save changes to your .bashrc in vi editor, :wq, which writes to file and quits.

Edit: You'll also need to re-source your .bashrc to use the changes in the current session. (Or just restart the session / close and reopen the terminal).

source ~/.bashrc

Good luck!

David_G
  • 358
  • 1
  • 5
  • 12
  • 4
    I would (personally) not create the alias and instead put the file in $HOME/bin. Creating an alias for every script is going to be hard to maintain if you create a lot of them. Also the chmod command would be a lot better off if it were either chmod +x script.sh or chmod a+x script.sh There should be no other users in your "user" group so the o+x option is kinda pointless. If you want others to be able to execute the file you need to either chown user:group script.sh and make sure that the "other users" are in that group. Or just let everyone execute it with chmod a+x script.sh – coteyr Nov 28 '12 at 02:25
  • 2
    Also.... For scripts to be run system wide the convention is to put them in /usr/local/bin/. Scripts that are in your $HOME may not b visible to everyone else. – coteyr Nov 28 '12 at 02:26
  • I had to log out and log in again for this to work on Ubuntu. And ultimately preferred user76204's solution. – Andriy Makukha Mar 03 '18 at 06:47
  • @AndriyMakukha You will need to re-load the .bashrc (see my edit at the end). Thanks for the suggestion. – David_G Mar 06 '18 at 07:01
3

On Ubuntu, it is possible to create a bin folder in your home folder and place your user scripts in there. Indeed, your ~/.profile will contain the following:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"

After you have created your bin folder you must logout and login again for /home/$USER/bin to appear in your path when you enter echo $PATH. Once it is in your path you will be able to call scripts in there by name and execute them just like you can any other programs.

The bin folder does not need any special permissions, and if you just want your user to be able to execute the scripts enter chmod u+x when you make them executable.

Your new output of echo $PATH when your bin folder is added should be:

/home/mike/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

You can place your scripts in /usr/local/bin, but you will need to use sudo to copy them to the folder and then use sudo again when you want to edit them there, so I find having a bin folder in the home folder is very convenient and sensible, particularly on a single user system.

However, it is important to note that if you have more than one user on your system and you want the script to be available for them all, you would definitely need to put it in /usr/local/bin.