7 Shell Scripts
7.1 Shell Scripts
So far, we have been running commands directly on the console in an interactive way. However, to re-run a series of commands (or an analysis), we can save the commands in a file and execute all those operations again later by typing a single command. The file containing the commands is usually called a shell script (you can think of them as small programs).
For example, let’s create a shell script that counts the number of atoms in one of our molecule files (in the molecules
directory): We could achieve this with the following command:
cat cubane.pdb | grep "ATOM" | wc -l
To write a shell script we have to save this command within a text file. But first we need to see how we can create a text file from within the command line.
7.2 Editing Files
There are many text editors available for programming, but we will cover two simple ones that can be called from the command line: nano
, which is purely based on the terminal; and gedit
, which has a graphical user interface.
We can create a file with Nano in the following way:
nano count_atoms.sh
This opens a text editor, where you can type the commands you want to save in the file. Note that the mouse does not work with nano
, you have to use your
For now, type this code to your script (or copy-paste it):
#!/bin/bash
# count the number of lines containing the word "ATOM"
cat cubane.pdb | grep "ATOM" | wc -l
Two things to note about our code:
- We started the script with a special
#!/bin/bash
line, which is known as a shebang. The shebang is optional, but in some cases is used to inform that this script should use the programbash
to be executed. - The other line starting with the
#
hash character is known as a comment and is not executed bybash
(it is ignored). Comments are extremely useful because they allow us to annotate our code with information about the commands we’re executing.
Once we’re happy with our text, we can press Ctrl+X to exit the program.
As we have made changes to the file, we will be asked the following:
Save modified buffer?
Y Yes
N No ^C Cancel
That’s a slightly strange way that nano
has of asking if we want to save the file. We can press Y and then we’re asked to confirm the file name. At this point we can press Enter ↵ and this will exit Nano and take us back to the console.
We can check with ls
that our new file is there.
Note that because we saved our file with .sh
extension (the conventional extension used for shell scripts), Nano does some colouring of our commands (this is called syntax highlighting) to make it easier to read the code.
Alternatively, you can use the gedit
text editor, which is a little more user-friendly (but is not always available, for example on macOS). The command to open a script is: gedit count_atoms.sh
. This opens the text editor in a separate window, which has the advantage that you can work on the script while having the terminal open.
You can save the file using Ctrl+S. Remember to save your files regularly as you work on them.
7.3 Running Scripts
Now that we have our script, we can run it using the program bash
:
bash count_atoms.sh
16
Which prints the result of running those commands on our screen. In summary, running a shell script is exactly the same as running the commands one-by-one on the shell.
However, saving our commands in a script has some advantages: it serves as a record of our analysis, making it more reproducible and it allows us to adapt and reuse our code to run other similar analysis.