Shell Programming - Control Structures


SHELL PROGRAMMING


// Shell programs

// IF STATEMENT


// Even or Odd

echo -n "Enter a number : "
read n

if [ `expr $n % 2` -eq 0 ]
then
echo "$n is Even"
else
echo "$n is Odd"
fi      

// Positive negative or zero
echo -n "Hello "; logname
echo -n "enter a number : "
read a

if [ $a -lt 0 ]
then
echo " $a is negetive"
elif [ $a -gt 0 ]
then
echo "$a is Positive"
else
echo "$a is Zero"
fi

// Biggest if three numbers
echo -n "Enter three numbers : "
read a b c
if [ $a -gt $b ]
then
if [ $a -gt $c ]
then
echo "Biggest is $a"

else
echo "Biggest is $c"
fi
else
if [ $b -gt $c ]
then
echo "Biggest is $b"
else
echo "Biggest is $c"
          fi
fi

//SWITCH STATEMENTS

// Arithmetic Operations
echo "Menu"
echo "1 - Addition"
echo "2 - Subtraction"
echo "3 - Multiplication"
echo "4 - Division"

echo -n "Enter your choice : "
read n

echo -n "Enter the numbers : "
read a b

case $n in
1) echo "Sum of $a and $b is `expr $a + $b`";;
2) echo "Difference of $a and $b is `expr $a - $b`";;
3) echo "Product of $a and $b is `expr $a \* $b`";;
4) echo "Division of $a by $b is `expr $a / $b`";;
*) echo "Invalid choice";;
esac




// Number – words
echo -n "Enter the number less than 10 : "
read n

case $n in
1) echo "$n - ONE";;
2) echo "$n - TWO";;
3) echo "$n - THREE";;
4) echo "$n - FOUR";;
5) echo "$n - FIVE";;
6) echo "$n - SIX";;
7) echo "$n - SEVEN";;
8) echo "$n - EIGHT";;
9) echo "$n - NINE";;
0) echo "$n - ZERO";;
*) echo "Invalid choice";;
esac

//WHILE LOOP

// Squares and Cubes up to given number
echo -n "Enter a number : "
read n

echo -e "\t Number \t Square \t Cube"
i=1
while [ $i -le $n ]
do
sq=`expr $i \* $i`
cu=`expr $i \* $sq`

echo -e "\t $i \t\t $sq \t\t $cu "
i=`expr $i + 1`
done




// Reverse a number
echo -n "Enter a number : "
read n
ori=$n
i=0

while [ $n -gt 10 ]
do
mod=`expr $n % 10`
i=`expr $i \* 10 + $mod`
n=`expr $n / 10`
done

i=`expr $i \* 10 + $n`
echo "Entered Number : $ori"
echo "Reversed Number : $i"

// Number of digits
echo -n "Enter a number : "
read n
i=1
while [ $n -gt 10 ]
do
n=`expr $n / 10`
i=`expr $i + 1`
done
echo “Number of digits is $i”

// FOR LOOP

// Read and display array.
// Find biggest and smallest in array

echo "Enter 5 values : "
for i in 0 1 2 3 4
do
read a[$i]
done




echo "Entered values are "
for (( i=0; i<5; i++))
do
echo -e "\t ${a[$i]}"
done

big=${a[0]}
small=${a[0]}

for ((i=1;i<5;i++))
do
if [ $big -lt ${a[i]} ]
then
     big=${a[$i]}
fi
if [ $small -gt ${a[$i]} ]
then
     small=${a[$i]}
fi
done

echo "Biggest is $big"
echo "Smallest is $small"


// Sorting 5 numbers

echo "Enter 5 values : "
for i in 0 1 2 3 4
do
read a[$i]
done

echo "Entered Values are "
for (( i=0; i<5; i++))
do
echo -e "\t ${a[$i]}"
done




// Sorting loop
for ((i=0;i<4;i++))
do
for ((j=`expr $i + 1`; j<5;j++))
do
if [ ${a[$j]} -lt ${a[i]} ]
then
t=${a[$i]}
          a[$i]=${a[$j]}
          a[$j]=$t
fi
done
done

// Output
echo "Sorted Numbers : "
for ((i=0;i<5;i++))
do
echo -e "\t ${a[$i]}"
done


Simulation of Unix Commands - grep


SIMULATION OF UNIX COMMANDS

Programs
// 3. Grep command
#include <stdio.h>                 
#include <stdlib.h>

int substr( char *line, char *pat);
int main(int argc, char *argv[])
{
        FILE *fp;
        char line[128];

        if ( argc == 2 )
                fp = stdin;
        else if ( argc == 3 )
        {
                fp = fopen(argv[2], "r");
                if ( fp == NULL )
                        printf("file %s cannot be opened \n", argv[1]);
        }
        else
        {
                printf("Usage: %s <pattern> <file> \n", argv[0]);
                exit(1);
        }
        while( fgets(line, sizeof(line), fp) )
                if ( substr(line, argv[1]) )
                        printf("%s",line);
        fclose (fp);
        return 0;
}
int substr( char *line, char *pat)
{
        int i,j,k;
        for( i=0; line[i] != '\0'; i++)
                for( j=i,k=0; line[j]==pat[k] && pat[k] != '\0'; j++,k++);
                             if ( pat[k] == '\0')
                                      return 1;
        return 0;
}




UNIX COMMANDS


UNIX Commands

1.    To create a directory.
a.    Syntax : mkdir (directory name)
b.    Example : mkdir CIVIL
2.    To rename a directory.
a.    Syntax : mv (directory 1) (directory 2)
b.    Example : mv CIVIL IT
3.    To change the directory.
a.    Syntax : cd (directory name)
b.    Example : cd IT1
4.    To copy the directory and its file to another directory.
a.    Syntax : cp –r (source directory) (destination directory)
b.    Example : cp –r IT1/ IT2
5.    To remove directory.
a.    Syntax : rmdir (empty directory name)
b.    Example : rmdir IT1
c.     Syntax : rmdir –r (non-empty directory name)
d.    Example : rmdir –r IT2
6.    To print present working directory. / To display complete path of the working directory.
a.    Syntax : pwd
7.    To create a file.
a.    Syntax : vi (file name)
b.    Example : vi aa.txt
8.      To rename a file.
a.    Syntax : mv (old file name) (new file name)
b.    Example : mv aa.txt a1.txt
9.      To view / display a file.
a.    Syntax : cat (file name)
b.    Example : cat a1.txt
10.    To copy files.
a.    Syntax : cp (source filename) (destination filename)
b.    Example : cp a1.txt a2.txt
11.    To move files.
a.    Syntax : mv (filename 1) (filename 2)
b.    Example : mv a1.txt a2.txt
12.    To list the contents of a directory.
a.    Syntax : ls
13.    To view first 10 lines of a file / to display the beginning of a file
a.    Syntax : head (file)
b.    Example : head a1.txt



14.    To view last 10 lines of a file / to display the end of a  file
a.    Syntax : tail (file)
b.    Example : tail a1.txt
15.    To display a string.
a.    Syntax : echo (string)
b.    Example : echo “IRTT”
16.    To count the number of words, lines and characters in a file.
a.    Syntax : wc (file)
b.    Example : wc aa.txt
c.     Syntax : wc –w (file)         // Count number of words only
d.    Example : wc –w aa.txt
e.     Syntax : wc –l (file)           // Count number of lines in file
f.     Example : wc –l aa.txt
g.     Syntax : wc –m (file)         // Count number of characters in file
h.    Example : wc –m aa.txt
17.    To search for a specific word in a file.
a.    Syntax : grep (word) (file)
b.    Example : grep civil aa.txt
18.    To count the number of occurrence of a character in a file.
a.    Syntax : grep –c (word) (file)
b.    Example : grep –c civil aa.txt
19.    To view the commands which was executed
a.    Syntax : history
20.    Show manual for a command
a.    Syntax : man (command)
b.    Example : man cat
21.    To clear the screen.
a.    Syntax : clear
22.    To print the calendar
a.    Syntax : cal
b.    Syntax : cal (year)
c.     Example : cal 2013
d.    Syntax : cal (month) (year)
e.     Example : cal 3 2012
23.    To print date and time.
a.    Syntax : date
24.    To find out who is logged in. / To print who is using the system presently.
a.    Syntax : who
25.    Display user name.
a.    Syntax : whoami
26.    Display users full detail, user-id and group-id.
a.    Syntax : id