Simple Shell Programming

Simple Shell Programming

Aim:

To write simple shell scripts using shell programming fundamentals.

The activities of a shell are not restricted to command interpretation alone. The shell also has rudimentary programming features. When a group of commands has to be executed regularly, they are stored in a file (with extension .sh). All such files are called shell scripts or shell programs. Shell programs run in interpretive mode.

The original UNIX came with the Bourne shell (sh) and it is universal even today. Then came a plethora of shells offering new features. Two of them, C shell (csh) and Korn shell (ksh) has been well accepted by the UNIX fraternity. Linux offers Bash shell (bash) as a superior alternative to Bourne shell.

Preliminarie:

1. Comments in shell script start with #. It can be placed anywhere in a line; the shell ignores contents to its right. Comments are recommended but not mandatory

2. Shell variables are loosely typed i.e. not declared. Their type depends on the value assigned. Variables when used in an expression or output must be prefixed by $.

3. The read statement is shell’s internal tool for making scripts interactive.

4. Output is displayed using echo statement. Any text should be within quotes. Escape sequence should be used with -e option.

5. Commands are always enclosed with `    ` (back quotes).

6. Expressions are computed using the expr command. Arithmetic operators are + – * / %. Meta characters * ( ) should be escaped with a \ .

7. Multiple statements can be written in a single line separated by ;

8. The shell scripts are executed using the sh command (sh file-name).

Result:

Thus using programming basics, simple shell scripts were executed

Program 1. Swapping values of two variables-

Algorithm:

Step 1 : Start 
Step 2 : Read the values of a and b 
Step 3 : interchange the values of a and b using another variable t as follows: 
              t=a 
              a=b 
              b=t 
Step 4 : Print a and b 
Step 5 : Stop 

Program (swap.sh)

# Swapping values with third variable
echo -n "Enter value for A : " 
read a 
echo -n "Enter value for B : " 
read b 
t=$a 
a=$b 
b=$t 
echo "Values after Swapping" 
echo "A Value is $a" 
echo "B Value is $b" 

# Swapping values without third variable
echo "Enter the number of A:"
read a
echo "Enter the number of B:"
read b
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo "Values after swapping:"
echo "A value is $a"
echo "B value is $b"

Output

[Administrator@localhost rathin]$ sh swap.sh 
Enter value for A : 22
Enter value for B : 44

Values after Swapping 
A Value is 44
B Value is 22

Program 2. Farenheit to Centigrade Conversion

Algorithm

Step 1 : Start 
Step 2 : Read fahrenheit value 
Step 3 : Convert fahrenheit to centigrade using the formulae: (fahrenheit – 32) × 5/9 
Step 4 : Print centigrade 
Step 5 : Stop 

Program (degconv.sh)

# Degree conversion 
echo -n "Enter Fahrenheit : " 
read f 
c=`expr \( $f - 32 \) \* 5 / 9` 
echo "Centigrade is : $c" 

Output

[Administrator@localhost rathin]$ sh degconv.sh 
Enter Fahrenheit : 213 
Centigrade is : 100 

Program 3. Area & Circumference of Circle

Algorithm

Step 1 : Start 
Step 2 : Define constant pi = 3.14 
Step 3 : Read the value of radius 
Step 4 : Calculate area using formula: pi × radius2 
Step 5 : Calculate circumference using formula: 2 × pi × radius
Step 6 : Print area and circumference 
Step 7 : Stop 

Program (circle.sh)

# Circle metrics using read only variable 
pi=`expr "scale=2; 22 / 7" | bc` 
readonly pi 
# Use the readonly command to make variables and functions read only i.e. you cannot change the value of variables.
echo -n "Enter value for radius : " 
read radius 
area=`expr "scale=2; $pi * $radius * $radius" | bc` 
circum=`expr "scale=2; 2 * $pi * $radius" | bc` 
echo "Area : $area" 
echo "Circumference : $circum" 

Output

[Administrator@localhost rathin]$ sh circle.sh 
Enter value for radius : 12 
Area : 452.16 
Circumference : 75.36 

Program 3. Simple Interest Calculation

Algorithm

Step 1: Start
Step 2: Read the values principal, rate and years
Step 3: Compute simple interest using the formula: (principal × rate × years) / 100
Step 4: Print simple interest
Step 5: Stop

Program (simpint.sh)

# Interest computation using bc
echo -n "Enter Principal amount : "
read p
echo -n "Enter number of years : "
read n
echo -n "Enter rate of interest : "
read r
si=`expr "scale=2; $p * $n *$r / 100" | bc`
echo "Simple Interest : $si"

Output

[Administrator@localhost rathin]$ sh simpint.sh
Enter Principal amount : 1285
Enter number of years : 3
Enter rate of interest : 5
Simple Interest : 192.75

Leave a comment