So, I’m back to school this semester (part time for now) and one of the first things we’re doing in our class is Shell Scripting. As a girl who got into this whole “computer science thing” after playing around with several Linux distros and loving the terminal, this is very fun for me! The course’s focus is mainly UNIX and C, but this is how we’re getting introduced to the terminal and I have no qualms. Shell scripting is almost like a game, and fairly is easy to pick up, even for someone who has little to no knowledge of the shell/terminal. So here’s a little script to get you started!

Before you begin…

Before you begin, you will need two things:

  1. Something to write your scripts in;
  2. Something to run your scripts in.

Most operating systems will come with #1 – in Windows, the notepad can be used and in linux, simple text editors like gedit are really nice to work with (especially if you like graphics over something fully text-based, like vim). Other things you can use are:

If you’re using a UNIX based system (mac, ubuntu, etc.) then you already have #2. The terminal in your system will run your bash and ch scripts, so no need to download anything. Windows users on the other hand face a harder battle. Your system will not run the bash scripts we are about to write, so you need to download cygwin. The instructions in this tutorial will be based on the Bourne shell, but your scripts should run fine on all systems.

Basic outline

All shell scripts share a basic outline:

#!/bin/sh

# instructions

#done

The first line – #!/bin/sh (shebang or hashbang) – is not mandatory, but useful to include, especially if you want to export your script. It tells the shell where to find and run bash (and this script) from. Just get into the habbit of writing it in at the beginning of every script, sort of like including stdio.h in C or the package in Java.

The Cat’s Meow

meowOkay, so now that we have our editors and cygwin out of the way and know the basic outline of a shell script, we can get to writing one!

Our script is going to be called “catsmeow.sh“; the .sh extension is for the Bourne shell. The premise is basically cats (which means there is no premise and this tutorial is just an excuse to squee over cat pics as “research”, but I digress). So, let’s start by echo-ing the word “meow” on our screen. We would do that just so:

#!/bin/sh

echo "meow!" #this is a comment... and that was a cat's meow

There, it’s that easy!

But wait! What if instead of the script “meowing” on it’s own, WE tell it to meow (or “say” anything else for that matter!). Here’s where “read” comes in:

#!/bin/sh

string meow_this    # we declare our variable

echo "Please tell the cat to meow something"

read meow_this    # we set variable using user input via read functionality

echo "$meow_this"    # we echo the variable back to user

The read command basically takes the user input and assigns it to a variable. You can then use that variable however you please. It is important to put the quotation marks around the variable when you echo it back because that way, no matter what the user input is, the shell won’t interpret it and there will be no errors while echo-ing.

Layering the cake

So, we’ve made a cat script and taught it how to repeat whatever we tell it to. But… cats don’t actually speak human 😦  

So we need to correct this in our program! What we’re going to do is validate the user input – if it’s “cat speak”, we let it through, it not, we tell the user to try again!

How do we do that? Using the “if” statement!

#!/bin/sh

echo "Please tell the cat to meow something"
read meow_this    # notice we didn't need to declare the variable before using it in read - this is a special case; not norm

if [[ $meow_this == *meow* ]]
then
echo "$meow_this"
else
echo "Sorry! This cat can only say things that have the word meow in them!"
fi

The if and else statements above evaluate the input assigned to read and then echo a statement depending on their evaluation.

if [[ $meow_this == *meow* ]]

then

echo "$meow_this"

This code means that if the string in the variable $meow_this contains the word “meow”, THEN (and only then!) echo the variable.

The following line:

else
echo "Sorry! This cat can only say things that have the word meow in them!"
fi

This code states that in all other cases (i.e. when the input string does not contain the word “meow”), echo “Sorry, this cat can only meow!”. The fi is used to close off the if statement.

What we just did here is called “pattern matching” – it’s a type of string manipulation where you go through the string and check it against some pattern to see if it exists within the provided string. In this instance, we’re going through the user input to see if it contains the word “meow” in it, and then proceeding with our script based on the results of this pattern matching.

There are other ways to do this. Another great way to pattern match is by using cases.

case "$meow_this" in
*meow*)
echo "$meow_this"
;;
*)
echo "Sorry! This cat can only say things that have the word meow in them!"
;;
esac

In this case (lol), we’ve replaced the “if statement” with a “case statement” (so to speak).

The first statement starts off the cases and specifies the variable ($meow_this) to look through. We then go through each case – in this instance, we have two cases:

  1. case 1: $meow_this contains the string “meow”
  2. case 2: $meow_this does not contain the string meow (i.e. everything else, as signified by the *)

The case function will match the string with first case, and if it does not succeed proceed to the next one.

The esac at the end closes off the cases.

At this point, now that we have a functioning cat that only meows, let’s learn how to run this script.

Run the script

Okay, so you’ve written a script, you understand how it works and it’s time to let the magic work itself out on your screen.

  1. Save your code in a file called “catsmeow.sh”.
  2. Open up the terminal.
  3. cd into the directory/folder where you’ve stored your catsmeow.sh file.
  4. Now chmod it – that is to say, change it’s permissions to make it executable. You can do this by typing:
    chmod 755 catsmeow.sh
  5. Now run it by calling the file like this:
     ./catsmeow.sh
    • tip: if calling the file that way doesn’t work, try this:
       bash catsmeow.sh

And that’s it! You will see the following on your screen:

shell-1

At this point, go ahead and type in your command for the cat. If it contains “meow”, the cat will echo it back to you. If it doesn’t, it will give you the default message: “Sorry! This cat can only say things that have the word meow in them!”

…But wait!

But, what what if you didn’t know that the cat only said things that contained the word meow before? What if you wanted to “try again”? In our current script, the shell will only evaluate one user input and then exit. How do we make it so that it goes back and keeps asking us for an input until we give it one with “meow” in it?

Well, I’ll cover that in the next tutorial, so check back soon! 😉