In my last post we discussed what scripts are and how they make your life easier. We also saw a how to get a script up and running within no time.
Today, we’ll use that knowledge to tackle something more than a ‘Hello World’ example 🙂
We’ll start with variables in bash (the terminal). Note that the shell need not be bash. Anyway, first are the system variables. They are named in all capital alphabets. Examples would be PATH, BASH, HOME, USERNAME etc. Go here for a full list. These variables are maintained by Linux itself.
To access the value of any one of them, open the terminal (ctrl+alt+t) and type, for example, echo $PATH and hit the return key. You’ll see something like this:
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
These are the paths in your root directory that the OS looks in when you open a certain program.
Anyway, moving on to user defined variables. These variables are defined and initialized by you – the user. The general format is as follows:
var_name=value
Note that there are no spaces on either side of the = sign. That’s required. Unlike most of the programming languages that allow spaces, shell doesn’t. Also, there are some naming conventions. Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character.
Now, you can define variables. Now, we’ll move on to accepting input from the user. For this, the following syntax is used:
read var_name
So, for example, if you want the user to enter a number, you could use:
echo "Enter Number" read number
Now we can move on to conditions and looping.
Conditions
The if condition:
Syntax
if condition
then
statement1
statement2
....
fi
Note: Unlike programming languages where a non negative value evaluates to true and a 0 value evaluates to false, in shell, it’s just the opposite.
| Boolean Value | Programming languages | Linux Shell |
|---|---|---|
| True | Non – Zero | 0 |
| False | 0 | Non – Zero |
The conditions are a bit different compared to programming languages.
For comparing integers:
| Expression in programming lang | Usage in shell | Example |
| == | -eq | if [ 5 -eq 6 ] |
| != | -ne | if [ 5 -ne 6 ] |
| < | -lt | if [ 5 -lt 6 ] |
| > | -gt | if [ 5 -gt 6 ] |
| <= | -le | if [ 5 -le 6 ] |
| >= | -ge | if [ 5 -ge 6 ] |
For strings, we have following expressions:
| Condition | Usage in shell | Example |
| Strings are equal | = | if [ “$str1” = “$srt2” ] |
| Strings are unequal | != | if [ “$str1” != “$srt2” ] |
And yes, that’s a single equals sign, not a ‘==’.
Similarly, we can have if-then-else style conditions:
Syntax
if condition
then
statement1
statement2
....
else
statement3
statement4
....
fi
Also, we can have multiple conditions checked using elif in between then and else blocks. See here
Also, note that the condition used here is provided in square brackets and the variables are put in double quotes
Now, we’ll move on to loops.
Loops:
The for loop:
Syntax:
Fortunately, the for loops executes just as a for loop in C/C++
for (( initialization; condition; counter )) do ..... ..... repeat all statements between do and done until condition is TRUE done
Here, the condition is also the of the same type as used in C/C++ and Java etc.
Also, there’s another type of loop that can be used which is more python oriented:
Syntax:
for {variable name} in {list}
do
.....
.....
done
As an example
for i in {0..10..2}
do
.....
.....
done
Here the list is constructed similar to the range function in python. In this example, the list will be { 0, 2, 4, 6, 8 }.
The while loop:
Syntax
while [ condition ] do statement1 statement2 statement3 .... .... done
Note:Here the condition used is similar to that used with the ‘if’ condition.
As an example, to print numbers from 1 to 10:
count=1 while [ "$count" -le 10 ] do echo -e "$count \n" count=$[count + 1] done
The echo -e represents that the echo command will interpret the ‘\n’ as a new line character.
In the fifth line, where we have performed the increment in counter. To know more about shell arithmetic, see this page.
Okay. So, that will be it for today. In the next part, we’ll see some real world examples of using scripts. Go here for the next part.