picaxmad
Posts: 3
Joined: Mon May 20, 2013 8:56 am

creating new shell commands

Mon May 20, 2013 9:58 am

Tutorial

step 1:

do sudo nano /usr/local/bin/<shell command name>

in the new file type in the shell script start it with

#!/bin/bash

after you write the bash script save it and type in the shell
sudo chmod +x /usr/local/bin/<shell command name>

then now if you type into the shell <shell command name> it will execute the program.

Explanation

linux and unix computers have a file which stores all the paths it searches through to execute a
command and /usr/local/bin is one of them.

The file also has to be executable so that's why you need to use chmod.

And because when you execute a command it starts by searching the directory you are in, then
it goes and looks through other directories like /bin and the one the program you wrote is in
(/usr/local/bin) and when it comes across the program is does the directory the program is found in (in this case /usr/local/bin) and then executes /usr/local/bin/<shell command name>

gregor3000
Posts: 114
Joined: Tue Nov 20, 2012 12:57 pm

Re: creating new shell commands

Tue May 21, 2013 7:43 am

a bti offtopic but since this is educational project....
linux and unix computers have a file which stores all the paths it searches through to execute a
command and /usr/local/bin is one of them
Windows and MS DOS OS also have these. they are called batch files (.bat) and the path is set in autoexec.bat

e.g.

Code: Select all

PATH C:\DOS;C:\WINDOWS
in this case every .bat put into windows or dos folder can be executed form anywhere in the OS.

the difference here is that .bat is automaticly executable while in linux you (the user) as administrator of the system have to asign a file to be executable (any file can be executable in linux). and it required you to have admin password and be in admin users group. this is one of many Linux security layers.

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: creating new shell commands

Tue May 21, 2013 10:24 am

/usr/local/bin is available to all users and thus, as a shared resource, protected (i.e. you need root to add/edit/chmod files in it)

~/bin (~ is Unix shorthand for "your home directory", so ~/bin is /home/pi/bin by default) is a per-user version of /usr/local/bin

Code: Select all

mkdir bin
#log out and log on again, ~/bin is only added to $PATH if it exists when you log on, see "~/.profile"
vi ~/bin/SomeCommand
#or, if you must, use nano :-)
chmod +x ~/bin/SomeCommand
Look Ma, no sudo!

Note that .../bin can contain any executable, not just bash scripts.

picaxmad
Posts: 3
Joined: Mon May 20, 2013 8:56 am

Re: creating new shell commands

Tue May 21, 2013 10:59 am

sprinkmeier wrote:/usr/local/bin is available to all users and thus, as a shared resource,
Note that .../bin can contain any executable, not just bash scripts.
True, this is just a simple way of doing it, for example when somone logs onto my pi it runs a perl script, which in turn runs some servers like an NX one to conenct with x windows.

User avatar
DaveDriesen
Posts: 113
Joined: Sun Mar 31, 2013 8:28 pm
Location: Top of the food chain
Contact: Website

Re: creating new shell commands

Tue May 21, 2013 7:52 pm

Adding stuff to the standard directories such as /bin and /usr/bin is easy, but it's actually good practice to keep all your custom scripts and apps in a separate location outside of the standard directories instead.

This will make it easier for you to duplicate your work to other machines, and also prevent conflicts/rework when upgrading.

You can then add this location to your PATH, or call them using their full path and file name, or create symlinks etc.

Dave Driesen
Linux Dev and oldskool elite

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: creating new shell commands

Tue May 21, 2013 9:00 pm

DaveDriesen wrote:Adding stuff to the standard directories such as /bin and /usr/bin is easy, but it's actually good practice to keep all your custom scripts and apps in a separate location outside of the standard directories instead.
If using Debian based systems (like Raspbian), you should put individual user custom programs in ~/bin, custom programs in /usr/local/bin and restricted (root/sudo only) programs in /usr/local/sbin

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: creating new shell commands

Tue May 21, 2013 9:11 pm

rpdom wrote:
DaveDriesen wrote:If using Debian based systems (like Raspbian), you should put individual user custom programs in ~/bin, custom programs in /usr/local/bin and restricted (root/sudo only) programs in /usr/local/sbin
There's even a standard(*) for this:
https://en.wikipedia.org/wiki/Filesyste ... _structure


(*)one of many to be sure :-)
https://xkcd.com/927/

Guy999
Posts: 20
Joined: Tue May 14, 2013 8:37 pm

Re: creating new shell commands

Sun May 26, 2013 7:09 pm

Just a suggestion for the O/P. As this is in the New user help wouldn't it be a good idea to have your post heading as something like: A tutorial - creating new shell commands so that people know that this is a tutorial not a question.

Best wishes
Guy

Return to “Beginners”