If there's a set of commands you need to run repeatedly, for example for a Cron job or each time after you deploy, then maybe it's time to write a script to run the commands for you.
This has the following benefits:
- Saves time - you only have to run one command
- Prevent errors - it'll be the same each time
- Version control - you can place the script in version control so other developers can make use of it.
Each script must start with a shebang line. The shebang line basically tells the machine which program should be ran to interpret this script.
Here are some shebang for common languages:
| Language | Shebang | |----------|----------------------| | Shell | #!/bin/sh | | Perl | #!/usr/bin/perl | | Python | #!/usr/bin/python | | Ruby | #!/usr/bin/ruby | | PHP | #!/usr/local/bin/php |
The shebang line is made up of the shebang (
#!), followed by the absolute path to the interpreter.
#!is used because
#indicates the beginning of a comment in most scripting languages, and so the interpreter will ignore it. The Unix system, hoever, does read
#!and know that this file is a script.
However, the same program might be installed at different locations on different machines, for example at
/usr/local/bin/ on one and
/usr/bin/ on another. Thus, using the absolute paths might not work.
Luckily for us, there's the
/usr/bin/env program which determines the path to our interpreter at runtime. So instead of
#!/usr/bin/python, you'd instead use
#!/usr/bin/env python. The
env program will check the
$PATH environment variable for the first program that matches that name, and use that as the interpreter.
Apart from ensuring the path is valid across different machines, using
env also ensures it uses the version of the interpreter available on the user's machine. If there are multiple versions of Python installed on the user's machine, it will use the one which is first on the
$PATH environment variable.
If your script is specific to one version of the program, you may specify a version number in the program name.
#!/usr/bin/env python # Interpret using the first version of python found #!/usr/bin/env python3 # Interpret using Python 3.x #!/usr/bin/env python2.6 # Interpret using Python 2.6
A special case is the
/bin/sh, because it's usually a link to the system's default shell, for example
/usr/bin/bash. So don't use
/bin/shto use the system's default shell. Only if you have bash-specific commands should you specify
Writing the Script
Since a script can be in many languages - Shell, Python, Ruby, PHP - we won't cover them.
After we finished the script, we need to make the script executable. We must set the proper permissions on the file.
$ chmod +x ourscript
This execute (
x) permission as well as the shebang line makes the script executable. For example, before we might have ran our python script like so:
$ python ourscript.py
Now we can execute it simply by running it
If you enjoyed this article, why not check out the rest of the series - System Administration 101!