Writing and running scripts#
Last week you learned about the command history or key combination ‘Ctrl + r’ for searching through previously run commands (notably these commands are only useful for finding individual commands). In most cases however, you want to save a series of commands and re-run them at any time in the future. If you construct a series of commands that you want to perform repeatedly, you can write them into a script and then run this script instead of each command individually. This makes it less likely that you make an error in one of the individual commands, and also keeps a record of the computation you performed so that your work is reproducible.
Creating a script in text editors#
You can write the script using a text editor on your computer and then uploading it with scp (for examble notepad). If you want to write a script directly in the terminal there are text editors available such as vim and emacs - you should be able to find tutorials for both online (for example openvim or GNU Emacs).
By convention, a script should be named ending in .sh and it can be created by vim in the following way:
# Create a script with .sh ending from within the command line
vim myscript.sh
# Some basic keys needed to manipulate a vim file
press 'i' # to start inserting text
press 'Esc' # to change from inserting text to view, always needed to run the two following commands
press ':wq' # to save the script
press ':q!' # to quit without saving
press 'Left/Right/Up/Down' # to navigate through the file
press 'Backspace' # to delete text
When writing a file in vim, make sure to not introduce any spaces that are not needed as these can have an impact on the commands that you want to run. Thus rather use the ‘Right’ key to move towards the end of a line and press ‘Enter’ to write another command on the next line.
Creating a script within R-Studio#
For this course, you will not necessarily be required to use these text editors, because here we provide a simpler solution of generating a script from within R-Studio:
Open a new R-Studio session
Click on ‘File’ -> ‘New File’ -> ‘Shell Script’
Copy/paste your commands into the file
Save the script within your home folder as myscript.sh
If you want to practice in advanced settings though, we would advise you to try to create your own script files with one of the text editors above.
Running a script#
The command line interface, or shell, that we use to run scripts is called bash and it allows you to run an existing bash script as follows:
# Run a script in the same directory
bash ./myscript.sh
bash myscript.sh # identical, works if you are in the same directory
# Run a script in another directory
bash ./mydir/myscript.sh
You can also run the same script with changing arguments, encoded as variables $1, $2, etc.
For instance we could have a simple script:
# myscript.sh
echo "Hello, my name is $1"
# Running my simple script
bash ./myscript.sh Chris
"Hello, my name is Chris"
This means you could write a script that performs some operations on a file, and then replace the file path in your code with $1 to allow you to declare the file when you execute the script. Just remember that if your script changes working directory, the relative path to your file may be incorrect, so sometimes it is best to use the absolute path.
Exercise 2.5#
Exercise 2.5
Create a script called myscript.sh containing the command echo “This is my first script!” and save it into your home folder on Cousteau. You can either use a text editor or use R-Studio.
#Within the command line with the text editor vim
cd
vim myscript.sh
press 'i'
Add the following command to the script: echo "This is my first script!"
press 'Esc'
press ':wq'
#Within R-Studio
Click on 'File' -> 'New File' -> 'Shell Script'
Add the following command to the script: echo "This is my first script!"
Click on 'Save as' -> myscript.sh
Execute the script you created from the command line to see whether it prints the output correctly.
#Within the command line
cd
bash myscript.sh
Copy your script file to create another secondscript.sh file and replace “first” by an argument “$1”.
#Within the command line
cd
cp myscript.sh secondscript.sh
#Within the command line with the text editor vim
vim secondscript.sh
press 'i'
replace the text first with $1
press 'Esc'
press ':wq'
#Within R-Studio
Open the file secondscript.sh
Replace first with $1
Save the script as secondscript.sh
Run your secondscript.sh script with the argument second. What happens if you run it without any argument?
# Run the script from the command line with the argument second
bash secondscript.sh second
# Run the script without an argument
bash secondscript.sh
# Note that the script does not print the empty argument