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)


No comments:

Post a Comment