Basic Shell script programming.

Basic Shell script programming.
1. Variable
You can set value for variable are :

bash-2.05$ a=”SHELL SCRIPT”
bash-2.05$ echo $a
SHELL SCRIPT
bash-2.05$ b=9
bash-2.05$ echo $b
9
bash-2.05$ echo ${b} <- You much use ‘{‘ and ‘}’ for make sure when you use variable.
9


2. Arithmetic Operation
When you want to operation of Arithmetic , you can user 2 solution.

2.1 By Basic

bash-2.03$ ((a = 1 + 3))
bash-2.03$ echo $a
4
bash-2.03$ ((a = 3 – 1))
bash-2.03$ echo $a
2
bash-2.03$ ((a = 3 % 2))
bash-2.03$ echo $a
1
bash-2.03$ ((a = 3 / 3))
bash-2.03$ echo $a
1
bash-2.03$ ((a = 3*3)) # You can press or not press space bar.
bash-2.03$ echo $a
9

2.2 By expr

bash-2.03$ expr 1 + 3
4
bash-2.03$ expr 3 – 1
2
bash-2.03$ expr 3 % 2
1
bash-2.03$ expr 3 / 3
1
bash-2.03$ expr 3*3 #You must press space bar.
3*3
bash-2.03$ expr 3 * 3 #You must use back slash (\) in font of star symbol
expr: syntax error
bash-2.03$ expr 3 \* 3
9

3. Flow Control
3.1 for loop

bash-2.05$ for x in a b c d
> do
> echo ${x}
> done
a
b
c
d

3.2 while loop
In the first time, you must initial value of variable x.

bash-2.05$ x=1
bash-2.05$ while [ ${x} -lt 4 ]
> do
> echo ${x};
> ((x = x + 1));
> done
1
2
3

3.3 if/else
bash-2.05$ vi test_if.sh
“test_if.sh” 11 lines, 146 characters

#!/bin/ksh
x=4;
if [ ${x} -lt 4 ]
then
echo “${x} less than 4”;
elif [ ${x} -eq 4 ]
then
echo “${x} equal 4”;
else
echo “${x} more than 4”;
fi

When you run test_if.sh script.
bash-2.05$ ./test_if.sh
4 equal 4

Other operation are :
-lt = less than
-le = less than or equal to
-gt = greater than
-ge = greater than or equal to
-eq = equal
-ne = not equal