Saturday, 21 April 2012

A Menu Based Program in shell script & Creating Backup of Home directory


1. The menu application should have at least four choices.
2. One of the choices should allow the user of the application to backup all of the files in their home directory into a special “backup” directory.
If the “backup” directory does not exist, your script should create it.
If the “backup” directory is not empty, display a message to the user that it is not empty, ask the user if they want to delete all the files. If they say yes, delete the files and perform the backup. If they say no, display a message that the backup was not performed.
3. One of the choices should display the current date and time.
4. One of the choices should display the current month calendar.
5. One of the choices should be the execution of a command that is often used.
6. If the choice is invalid, redisplay the menu.




Desgined Code:-
#! bin/bash

#menu

echo enter [1] to backup                                                        

echo enter [2] to see  current date and time

echo enter [3] to see current month calender

echo enter [4] to execute the command

read a                 # a is the variable to store 1,2,3,4....

case "$a" in

1)cd /                # change dir to root

cd /home/saurabh      # change dir to home/saurabh

mkdir backup          # make a dir named backup in home/saurabh

cd backup             # change dir to backup

[ "$(echo .* *)" == ". .. *" ] && a=0||a=1      # it searches files and folder in backup dir if there is no files and folders

                                                #  then a is set to 0, if it contains then a is set to one



if [ $a -eq 0 ]                                 # if a=0 then display backup directory is empty

then

echo backup directory is empty

fi

if [ $a -eq 1 ]                                 # if a=1 then display backup directory has files

then

echo backup directory has files

echo if you want to remove all the files enter 1 if not enter any another no.

read b             # b is a variable to store a no.

if [ $b -eq 1 ]

then

rm -r *            # it removes all the files and folder from backup dir.

fi
[ "$(echo .* *)" == ". .. *" ] && echo now dir is empty|| echo dir is non empty  
#if backup dir has files and    folder then it shows dir is non empty

#if backup dir has no files and folder then it shows dir is empty
                 
fi

echo enter 5 to backup your all files and folder if you dont want.... enter any other no.

read c           # c is a variable to store a no.

if [ $c -eq 5 ]

then

cd /            # change dir to root

cp -r home home/saurabh/backup 
# copy all the files and folder of home to home/saurabh/backup
fi ;;


2) echo todays date and time is $(date);; # shows todays date and time

3) cal ;;                                 # shows calender of month

4) touch sau.c;;                          #create a file...

*) bash menu.sh;;                         # redisplay the menu after entering any other no. other than 1,2,3,4

esac

Input:-
bash menu.sh

Output#1:-
enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

1
backup directory is empty

enter 5 to backup your all files and folder if you dont want.... enter any other no.
5
All the files and folder of home directoty will be copied & stored to home/saurabh/backup
directory.
Output#2:-
enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

2   
todays date and time is Sun Apr 15 13:29:12 IST 2012


Output#3:-
enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

3

     April 2012

Su Mo Tu We Th Fr Sa

 1  2  3  4  5  6  7

 8  9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30


Output#4:-

enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command

8      

enter [1] to backup

enter [2] to see current date and time

enter [3] to see current month calender

enter [4] to execute the command



 Limitations:-
To backup the files of Home directory all the files should have read permissions for all user group and others...otherwise it will show permission denied.


When I excuted the program I got errors as:-

cp: cannot stat `home/saurabh/Desktop/assignment/shell/modes/f1': Permission denied

cp: cannot stat `home/saurabh/Desktop/assignment/shell/modes/modes.sh': Permission denied

cp: cannot stat `home/saurabh/Desktop/assignment/change/f1': Permission denied

cp: cannot stat `home/saurabh/Desktop/assignment/change/d1': Permission denied


 References:-
Beginning Linux Programming(Text Book)


Tuesday, 10 April 2012

File Management System In Shell Script



echo------------------------------FILE MANAGEMENT SYSTEM--------------------------------------------
echo give a path of folder which contain some files:
cd /
read a
cd $a
echo enter 1 to List files stored along with their sizes.
echo enter 2 to Create files
echo enter 3 to Allow changes to files
echo enter 4 to Delete files
echo enter 5 to store information of files
echo enter 6 to move files between the directories
read n
case "$n" in


1) ls -s;;
2) echo enter file_name and write in file
   read f
   cat> $f;;
3) echo give path of folder to make changes in the files
   read c
   cd /
   cd $c
   echo folder contain files:-
   ls
   echo give the name of file you want to make changes
   read d
   echo "enter 1 for read,write,execute to user group and others"
   echo "enter 2 for read and write to users and group"
   echo "enter 3 for read write and execute to only users"
   read m
   case "$m" in
   1) chmod 777 $d;;
   2) chmod 660 $d;;
   3) chmod 700 $d;;
   esac
   echo changes happened:-
   ls -l;;
4) echo files are:-
   ls
   echo enter no. of files you want to delete
   read countt
   for(( i=1 ; i<=$countt ; i++ ))
   do
   echo give filename to delete
   read e
   rm -r $e
   echo file $e is deleted
   echo now files are:-
   ls
   done;;


5) echo files are:-
   ls
   echo enter name of file you want to see the information and store it in status file.
   read g
   echo information of file is:
   stat $g
   touch status
   stat $g>>status
   echo information of file is stored in file name status and its path is $a/status;;


6) echo enter the path of source directory from where you want to copy the files:
   read p
   echo enter the path of destination directory to where you want to move the files:
   read q
   cd /
   cd $p
   echo files of source dir are:-
   ls
  
   echo enter no. of files you want to move.
   read count
   for(( i=1 ; i<=$count ; i++ ))
   do
   cd /
   cd $p
   echo files of source dir are:-
   ls
   echo enter the name of file you want to move
   read name
   mv /$p/$name  /$q
   cd /
   cd $q
   echo content of destination dir $q now are:
   ls
   done ;;
  
esac

Note:- To run the program save it as filename.sh and give a command bash filename.sh






Sunday, 8 April 2012

Gauss Seidal Method in C to solve equations.

Applied Gauss Seidal method to solve following system of equation up to given no. of iterations.
(1) 4x+y-z=7
(2) 2x+3y+z=4
(3) x+y-2z=2

#include<conio.h>
#include<stdio.h>
void main()
{
float x,y,z,a,b,c;
int i,n;
clrscr();
printf("enter no of iteration n");
scanf("%d",&n);
x=0;
y=0;
z=0;
a=(7-y+z)/4;
b=(4-(2*a)-z)/3;
c=((-1)*(2-a-b))/2;
printf("x            y           z");
printf("\n%f  %f  %f",a,b,c);
x=a;
y=b;
z=c;
for(i=1;i<n;i++)
{
x=(7-y+z)/4;
y=(4-(2*x)-z)/3;
z=(-1)*(2-x-y)/2;
printf("\n%f  %f  %f ",x,y,z);
}
getch();
}