∙ Shell scripts
Files containing commands - a step towards programming
Scripts
So far, we have been running things directly on the command line, “interactively”. However, you might want to keep your commands in a file, and be able to run, share, modify, or use files as templates. This is the gateway to coding - and is great practice for developing your reproducible work, for communicating your methods with others, and for archiving your work.
A “script” is a file of instructions that can be directly read and run by a program. For example, a file of Python code is a script, since it does not need to be compiled into binary - this is done by the Python interpreter, working as it goes along. In our case, we are going to make a script, but the language will be shell (all of the commands we have been using so far are shell commands).
Let’s create a very simple script - open nano
, creating a file called “my-first-script.sh” (or anything you like!) and include two lines
ls -l *part*
echo "That is all of the files with \"part\" in that I could find here."
- If you didn’t look up
echo
earlier, useman
to work out what it does. - Think of
echo
asprint
, used commonly in programming languages.
Save the file and exit nano
. Now, we are going to run this file, in the folder some_plays
. To do this, we are going to ask the program bash
(“Bourne Again shell”, a widely-used shell program) to run it, with
bash my-first-script.sh
Exercise
Let’s use all of the skills we have learned today to build a script to complete a task. Be in the folder some_plays
. Use nano
to start a new, empty file. Create a script that will:
- Print the final line of Macbeth
- Count how many time the word “merry” occurs in all plays starting with the letter “m”
- Count how many times Hamlet speaks in Hamlet
- Print the line of who edited each play
- Use
head
andtail
to print just the 500th line of All’s Well That Ends Well - Make a new file, in the folder containing
some_plays
, of the total count of the lines containing the word “love” in all of the plays (combined - ie a single number).
If you can complete this task - congratulations, you now know all of the important ways of navigating the file system, of searching files, and the basics of shell scripting.
When learning to use computer clusters and high-performance computers, we often run jobs using “submission scripts”. These are just the same as a shell script - they are not magic, merely running commands one after another, just like you could on the command line itself.
You can learn these skills in our course Introduction to HPC, do an internet search for our current courses.