Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Please write a shell script that does the following:
- Takes in two arguments - one being a directory name, and the other being a user name.
- If the program is invoked without the required number of arguments, print an error message and exit.
- If the given directory does not exist, print an error message and exit.
- If the given user is not a valid user on the system, print an error message and exit.
- Find all regular files under the given directory (and any subdirectories in it) and do the following:
- If the file is not a regular file, ignore it.
- If the file belongs to the given user (the user is the owner of the file), or if the user is a part of the group owner of the file, make a copy of the file under a directory named backup under your home directory.
- The relative pathname of the copied file should be the same as that of the original file. For example, if the original file was named <given_directory>/dir1/dir2/file1.pl, then you should back it up as <your_home_directory>/backup/dir1/dir2/file1.pl.
- You should keep a log of all files being backed up in a file named backup.log in your home directory. You should take care of the following:
- The file backup.log should not have more than 10 lines. If the number of lines increased beyond 10, rename the file as backup.log.<n>, where n is a serial number, and then start writing the log into a new file named as backup.log. This means that if you have copied 25 files, then at the end of the exercise, you should have the following files in your home directory:
- backup.log - This will contain five entries (for the last five files).
- backup.log.1 - This will contain ten entries (for the first ten lines).
- backup.log.2 - This will contain ten entries (for the second ten files).
- ############### THIS IS THE CODE #######################################################
- #!/bin/sh
- ##########################################################################################
- clear
- echo $1
- echo $2
- UName=$1
- DirName=$2
- CountFlLn=0
- CountFl=1
- HomePathOfCUser=""
- ###########################################################################################
- if [ -z "$UName" ] || [ -z "$DirName" ];then # checking Usernmae and Folder name argument is valied
- echo " ====================================================================="
- echo "| Usage: Argument.sh <Username> <FolderName> |"
- echo "| ----------------------------------------------------------- |"
- echo "| |"
- echo "| Either Dirictory name or username field is blank |"
- echo " ====================================================================="
- exit 1
- else
- UserSts=`grep -c ^$UName /etc/passwd` # Reterieving value from passwd file into setting verible value for Checking user existence
- if [ -d $DirName ] && [ $UserSts -eq 1 ] ;then # Checking is Username and Folder is Valied
- echo "The given diroctory and user is exist"
- HomePathOfCUser=`grep ^$UName /etc/passwd | gawk -F: '{print $6}'`
- for file in $DirName/* # looping files is file veriable
- do
- if [ -f "$file" ] ;then #Checking is a regular file
- #echo "$file"
- #ls -l "$file" | awk '{ print $3 }'
- #ls -l "$file"| awk '{ print $4 }'
- if [ "$UName" = `ls -l "$file" | awk '{ print $3 }'` ] || [ "$UName" = `ls -l "$file"| awk '{ print $4 }'` ] ;then #Checking ownership and permission
- if [ -d "$HomePathOfCUser/Backup" ] ;then # Checking Backup folder exist
- #echo "$HomePathOfCUser/Backup"
- if [ $CountFlLn -eq 10 ] ;then
- (( CountFlLn= $CountFlLn + 1 )) # Value for current line number
- cp -R "$file" $HomePathOfCUser/Backup"${file#$DirName}".bak
- echo "$file as a {file#$DirName}.bak" >> $HomePathOfCUser/Backup/Backup.log
- mv $HomePathOfCUser/Backup/Backup.log $HomePathOfCUser/Backup/Backup.log.$CountFl
- touch $HomePathOfCUser/Backup/Backup.log
- sleep 5
- (( CountFl= $CountFl + 1 ))
- (( CountFlLn= $CountFlLn - 10 ))
- else
- (( CountFlLn= $CountFlLn + 1 )) # Value for current line number
- mkdir -p "$HomePathOfCUser/Backup${file#$DirName}"
- cp -R "$file" "$HomePathOfCUser/Backup${file#$DirName}.bak"
- echo "$file as a $file.bak" >> $HomePathOfCUser/Backup/Backup.log
- fi
- else
- mkdir -p "$HomePathOfCUser/Backup"
- touch "$HomePathOfCUser/Backup/Backup.log"
- mkdir -p "$HomePathOfCUser/Backup${file#$DirName}"
- (( CountFlLn= $CountFlLn + 1 ))
- cp -R "$file" $HomePathOfCUser/Backup"${file#$DirName}".bak
- echo "$file as a ${file#$DirName}.bak" >> "$HomePathOfCUser/Backup/Backup.log"
- fi
- else
- echo "You dont have permission to access tis file $file "
- fi
- fi
- done
- else
- echo "The given diroctory or user is not exist"
- exit 1
- fi
- fi
Add Comment
Please, Sign In to add comment