Shell Script

Introduction

Using the Unix commands, you can manipulate files as you like. Sometimes, you need to repeat the very similar commands multiple times. Also, you have to memorize what you did to repeat a task. Let’s think below as an example. You have five data files named as data01.csv, data02.csv, data03.csv, data04.csv, and data05.csv. Here, you want to change the file name to temperature-01.csv, temperature-02.csv, …, temperature-05.csv. Using the Unix shell, you will try:

$ mv data01.csv temperature-01.csv
$ mv data02.csv temperature-02.csv
$ mv data03.csv temperature-03.csv
$ mv data04.csv temperature-04.csv
$ mv data05.csv temperature-05.csv

I guess that you can do it. If we have 100 files instead of 5, however, you have to repeat the mv command 100 times. It sounds super annoying and you may have a mistake during this process. When you write a shell script, you can do it more efficiently. Here, you can learn how the shell script works.

Let’s try

Let’s start to write a simple shell script as shown below. You can use the text editor whatever you want. A filename may be “test01-v1.sh”.

#!/bin/bash
# --- This is comment ---
echo "PSC6125 is awesome!"

After you saved this file, let’s type a command “sh” with that filename. $ sh test01-v1.sh

You can see the word “PSC6125 is awesome!” in your prompt. This is a simple example of a shell script. The shell script consists of a bunch of commands and every line shows one command. The first line defines that your shell script is written by a bash script. When you put “#” like the second line (except for “#!”), the computer doesn’t care what you write after the mark. So, the second line is just a comment. In the third line, you can see the “echo” command, which prints out the word in prompt.

Let’s modify the file a little. First, you copy the file and then modify it like this.:

$ cp test01-v1.sh test01-v2.sh

Then, edit the file “test01-v2.sh” like below

#!/bin/bash
# --- This is comment ---
echo "PSC6125 is awesome!"
echo 'PSC6125 is awesome!'

The fourth line is the newly added command. It looks like the third line but it’s single-quotes instead of double-quotes.:

$ sh test01-v2.sh

You can see “PSC6125 is awesome!” twice in your prompt. You may think that the single-quote and double-quote are the same. But those are not the same. Let’s modify your file like below.:

$ cp test01-v2.sh test01-v3.sh</code>

Then, edit the file “test01-v3.sh” like this

#!/bin/bash
# --- This is comment ---
class="PSC6125"

echo "$class is awesome!"
echo '$class is awesome!'

And then run the shell script.:

$ sh test01-v3.sh

You can see what is the difference between double and single quotes.

Loop

The loop (or do-loop) is one of the biggest advantages in shell script (or computer). There are two types of do-loops: for and while. Whereas the for-loop is convenient for file manipulation, the while-loop is more general in computer programing. Here, this module shows an example of a while-loop.

Example: test02_loop-v1.sh:

#!/bin/bash
# --- This is comment ---
ix="1"
mkdir TEST02
cd TEST02
while [ $ix -le 10 ]
do
   touch "text$ix"
   ix=`expr $ix + 1`
done </code></pre>

Example: 02_02-loop_text.sh:

#!/bin/bash
#----------------------------
#-- This is the definition --
#----------------------------
 ix="1"
#----------------------------
#-- This is the do loop --
#----------------------------
 while [ $ix -le 10 ]
 do
   echo "text$ix" > test-$ix.txt
   ix=`expr $ix + 1`
 done

If statement

Example: if_state-v1.sh:

#!/bin/bash
# --- This is comment ---
echo 'How is PSC6125? Please scale it for 1-5 [1:poor, 5:excellent]'

read num

if [ $num == 1 ]
then
  wrd="terrible"
elif [ $num == 2 ]
then
  wrd="boring"
elif [ $num == 3 ]
then
  wrd="OK"
elif [ $num == 4 ]
then
  wrd="good"
elif [ $num == 5 ]
then
  wrd="excellent"
else
  wrd="what?"
fi

echo "PSC6125 is $wrd"</code></pre>