6. Basic Shell Scripts#

Multiple shell commands can be used as part of a larger “program” by combining them together in a “shell” script. A shell script is a file containing the text you would normally enter at the command prompt within a terminal.

6.1. A simple shell script#

6.1.1. Exercise#

What do you think the following command will do ?

echo "echo \"hello\"" > script-1.sh

Navigate to your work directory, run it and see if you were correct. What do the \’s do ?

6.1.2. Exercise#

Now “run” the file using

source ./script-1.sh

What happened ?

What are the permissions on the file script-1.sh ?

What happens if you try this ?

./script-1.sh

6.1.3. Exercise#

Change the file permissions of script-1.sh to executable and run the previous command again.

What happens now ?

6.2. Shebang#

If you were to look for examples of shell scripts online you might find that many (most ?) of them inlude the following line (or something like it) at the top of the file.

#!/bin/bash

This instructs the system to initialise a bash terminal to run the script in (this is the terminal you have been typing in). This allows the shell script to be run by other systems that are not currently using a terminal to succesfully run your script.

In Unix speak #! is pronounced she bang, or sometimes hash bang.

6.2.1. Exercise#

Use cat to add the above hash bang to your script-1.sh file.

6.3. Variables#

Within a shell you can create and assign values to variables. For example

var="val"

You can access the value (val) stored in the variable (var) by prefixing the variable name with a $.

echo $var

Variables can be set using other variables

new_var=$var
echo $new_var

The shell uses variables to store information about the environment.

6.3.1. Exercise#

What is the value of these variables - HOME, PATH, PWD, USER ?

What does the printenv command do ?

6.4. Variables and shell scripts#

6.4.1. Exercise#

Create a file script-3.sh with the following content.

#!/bin/bash

echo $1
echo "what a " $2

Make executable then execute as follows

./script-3.sh hello world

What is happening ?

What is the value of $0 ?

What is the value of $# ?

6.5. Setting the PATH#

6.5.1. Exercise#

Change directory to your work directory and execute the following

WORK_DIR=$PWD

What does this do ?

cd to your home directory. What will this command do ?

ls $WORK_DIR

Variables can be concatenated. For example

var1="one"
var2="two"
var3=$var1$var2
echo $var3

The PATH variable is used by the shell to search for programs. You can add the name of a directory where your shell scripts are by concatenating it to the existing path. For example

echo $PATH
PATH=$PATH:$WORK_DIR
echo $PATH

You can now run your shell scripts from the previous examples using just their name. For example

script-3.sh

6.6. Iteration#

6.6.1. Exercise#

Check you understand what the following shell script does

#!/bin/bash

echo "simple loop"
for VARIABLE in 1 2 3 4 5 .. imposter N
do
    echo "VARIABLE = "$VARIABLE
done

echo "loop over input values"
for VARIABLE in $@
do
    echo $VARIABLE
done

6.6.2. Exercise#

Create an executable shell script that uses R to generate random numbers from a normal distribution and send them to stdout in a nice format. Your shell script should accept three arguments : the sample size, the mean, and the standard deviation. Put your shell script in a new directory and make it executable. Add the new directory to PATH.