5. 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.
5.1. A simple shell script#
5.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 ?
5.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
5.1.3. Exercise#
Change the file permissions of script-1.sh to executable and run the previous command again.
What happens now ?
5.2. Shebang#
A shell file will execute the shell commands in it line by line as if you had entered them one by one in the terminal. However, they can also be used to start another program and then feed the “code” in that script to that program. For example, create a file script-2.sh with the following content
#!/usr/bin/env python3 print(“hello”)
Now change the permissions to executable and run
./script-2.sh
The first line of the script is pretty standard. The #!/usr/bin/env is standard, the next part is the program you want to execute the code that follows. In Unix speak #! is pronounced shebang, or sometimes just hash bang.
5.2.1. Exercise#
Try running some R code from a script.
5.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
Variables can be used as commands
new_ls="ls"
$new_ls
The shell uses variables to store information about the environment.
5.3.1. Exercise#
What is the value of these variables - HOME, PATH, PWD, USER ?
What does the printenv command do ?
5.4. Variables and shell scripts#
5.5. Exercise#
Create a file script-3.sh with the following content.
#!/usr/bin/env bash
echo $1
echo "what a " $2
Make executable then execute as follows
./script-3.sh
What is happening ?
What is the value of $0
what is the value of $# ?
5.6. Setting the PATH#
5.6.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
5.7. Iteration#
5.7.1. Exercise#
Check you understand what the following shell script does
#!/usr/bin/env bash
echo "simple loop"
for VARIABLE in 1 2 3 4 5 .. N
do
echo $VARIABLE
done
echo "loop over input values"
for VARIABLE in $@
do
echo $VARIABLE
done