Make sh script into a command

zionsrogue

Registered
I have a sh script that I use a lot called "dumpback". I invoke it using the terminal and I'd like to register it as a normal command. So instead of typing "sh dumpback <options here>" I would just like to say "dumpback <options here>"
 
Are you using the "shebang" requirement at the beginning of the script? It should be something like the following at the topmost section of your script:

Code:
#!/bin/sh

You can replace "/bin/sh" with whichever shell you want to run the script with, but I believe that "/bin/sh" makes sure that the script is compatible in any Unix system.
 
Remember to make the script executable, like

$ chmod u+x dumpback

Also, remember that the current directory is not normally part of the command search path (environment variable $PATH), so you might need to run it as

$ ./dumpback

or move to a directory that is part of the PATH (the home directory is, but if you make a lot of scripts, make some other directory, or move them to /usr/local/bin).

DO NOT make current directory (.) part of the PATH. Even if we forget crackers etc., think about this: what is the most common name of the script (or compiled program for that matter) ?

"test" (like I am just testing the compiler, a new library etc.).

So what, you ask. One of the most common command used by several scripts is test. It is used for example to test if a file exists etc. Think how some installation script works, if test prints only "Hello world"...
 
Thanks for the addendum, artov. I had forgotten about a few of the things you mentioned. :eek:
 
Could you explain more about how to set the PATH so I can run "dumpback" from any directory? (Not just the one that contains it)
 
PATH is environment variable that lists directories, like (for user "ville")

$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/Users/ville:/usr/local/bin:/usr/X11/bin

Normally /usr/bin and /bin contain commands of the operating system, like /sbin and /usr/sbin. Previously, "s" in /sbin and /usr/sbin meant that the commands are static, i.e. they do not use dynamic libraries, but in OS X it means "system", that is commands that normal user does not use. /usr/X11/bin contains X Window programs (if you have installed X Window).

As I said, you can put your commands either on your home directory or on /usr/local/bin. So, put the script at your home directory, and you can run it from any directory by using its name.

So:

$ cd
$ cat > myowncommand <<ENDOF
#!/bin/sh
echo "My own command"
ENDOF
$ chmod u+x myowncommand
$ cd /tmp
$ myowncommand

(The <<ENDOF is a thing called "here" document, i.e. everything between texts ENDOF goes to the file myowncommand)
 
Back
Top