Basic Linux Shell Scripting Concepts

Select Statements

This is used to present the use with a simple menu of choices. Consider the following example.

#!/bin/sh
echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
break
done
echo "You have selected $var"

For Loops

These loops are used when you have a list of values you want to test. Consider the following example.

#!/bin/bash
for myvar in a b c; do
echo $myvar
done

Quoting

Finally we will cover the idea of quoting. This can be confusing when you first deal with it. Quoting takes the value that is contained within the quotes and treats it as a literal value. For example, ‘$var’ will print $var, as opposed to the value of $var. Both single and double quotes can be used to represent literals, with the difference being that a single quote is an absolute literal, while a double quote will display the value of a variable, if it is inside the quote.

That should be enough to get you started. The next article in this series will further address scripting by taking a look and some commands that are commonly used in scripts such as GREP, as well as the concept of input and output redirection. Finally, my third article will tie things together to create usable administrative scripts.