BASHing Through Scripts

Written by Ken Gypen

November 26, 2007 | 08:12

Tags: #script #shell #unix

Companies: #bash

Conditional statements

The previous examples are great for doing things that need to be repeated a lot of times, but are useless when a script actually has to make decisions.

For that purpose there are lots of tools, but the most used are certainly ‘if’ and ‘case’ structures. ‘If’ verifies if a condition is met, and then decides what to do while a ‘case’ structure looks for a specific match, and acts accordingly.

Here's an example - Let's say a simple program gives you two choices - Yes, or No. IF you say yes, THEN the program will continue...ELSE, it will take you back a step to fix what's wrong. That's all there is to it! IF/THEN/ELSE statements can get into pretty complex groups called 'decision trees,' but we'll save the real complex stuff for a later date.

Now, what ‘if’…


 #! /bin/bash

echo "black or white?"
read choice

if [ $choice == "black" ]
then
  echo "dark"
else
  echo "light"
fi
'If' is a great way to choose between two options.

'If' is a great way to choose between two options.

Now, we ask the user to choose between “black” or “white”. If the user enters black (hence the string in the variable choice matches “black”), the script will return ‘dark’, otherwise it will return ‘light’. The end of the ‘if’ structure is given by ‘fi’ ('if' spelled backwards).

As you see, there’s a flaw in the script - when the user enters ‘blue’ for example, the script will still return ‘light’. This is the limit of an 'if' statement - something is or is not, there's not a way to handle more than two options. Programmers get around this limitation by creating nested (one inside another) 'if' statements, so that if something is true or false (depending how you like to write programs), it will check a new statement to compare the answer to the next choice.

This isn't so bad if you only have three options, but it gets to be a bear when you have many options - each 'if' needs to be properly written and closed, and by the time you get four or five, you start to wish there was a more elegant solution. Well, there is - 'case' can check for an exact answer. It's not more intelligent - it still only reads whether something is or isn't - but it's a great alternative to writing several 'if' statements to check for more than one option.

So let’s increase the accuracy of our script a bit by using a ‘case’ structure…
 #! /bin/bash

echo "black or white?"
read choice

case $choice in
  "black")
    echo "dark"
    ;;
  "white")
    echo "light"
    ;;
  *)
    echo "Please choose either black or white"
    ;;
esac 
The beginning is the same as before, but now we issue a ‘case’ structure that checks the variable choice. If it’s “black”, then we display dark and exit the case structure with the ‘;;’. If it matches “white”, we display “light”. We tell the program that the ‘case’ structure is over with ‘esac’ (case spelled backwards). See, BASH is easy!

'Case' works better for multiple or open choices,where  the user can put in unexpected answers.

'Case' works better for multiple or open choices,where
the user can put in unexpected answers.

But here's where the benefit of 'case' comes in - if the answer doesn’t match “black” or “white”, we include a catch-all line that alerts the user that his choice was none of the options. The same statement done with a nested 'if' statement would take considerably more code, which gives you one more space to make errors.

That's great...now how can I use this?

By now you should be able to write some semi-intelligent shell scripts that make the life of a server administrator a lot easier. When you write your scripts intelligent enough to operate entirely on their own and call in the help of Cron, a scheduling service that can run scripts and commands at regular intervals, you can start to create a machine that can organise your files, process images, copy directories from or to another computer or a variety of other server-style tasks.

Of course, you don’t need a server to create useful scripts - almost any task can be done through the use of scripts, particularly since almost all 'Nix software has a command-line function. A rule of thumb is that if you need to do a task more than three times, you should write a script for it.
Discuss this in the forums
YouTube logo
MSI MPG Velox 100R Chassis Review

October 14 2021 | 15:04

TOP STORIES

SUGGESTED FOR YOU