Guest User

Untitled

a guest
Jan 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.95 KB | None | 0 0
  1.  
  2. Please write a shell script that does the following:
  3.  
  4.     Takes in two arguments - one being a directory name, and the other being a user name.
  5.     If the program is invoked without the required number of arguments, print an error message and exit.
  6.     If the given directory does not exist, print an error message and exit.
  7.     If the given user is not a valid user on the system, print an error message and exit.
  8.     Find all regular files under the given directory (and any subdirectories in it) and do the following:
  9.         If the file is not a regular file, ignore it.
  10.         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.
  11.         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.
  12.     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:
  13.         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:
  14.             backup.log - This will contain five entries (for the last five files).
  15.             backup.log.1 - This will contain ten entries (for the first ten lines).
  16.             backup.log.2 - This will contain ten entries (for the second ten files).
  17.  
  18.  
  19. ############### THIS IS THE CODE #######################################################
  20.  
  21. #!/bin/sh
  22. ##########################################################################################
  23. clear
  24. echo $1
  25. echo $2
  26. UName=$1
  27. DirName=$2
  28. CountFlLn=0
  29. CountFl=1
  30. HomePathOfCUser=""
  31. ###########################################################################################
  32.  
  33. if [ -z "$UName" ] || [ -z "$DirName" ];then # checking Usernmae and Folder name argument is valied
  34.     echo " ====================================================================="
  35.     echo "|            Usage: Argument.sh <Username> <FolderName>               |"
  36.     echo "|     -----------------------------------------------------------     |"
  37.     echo "|                                                                     |"
  38.     echo "|        Either Dirictory name or username field is blank             |"
  39.     echo " ====================================================================="
  40.     exit 1
  41. else
  42.     UserSts=`grep -c ^$UName /etc/passwd` # Reterieving value from passwd file into setting  verible value  for Checking user existence
  43.    
  44.     if [ -d $DirName ] && [ $UserSts -eq 1 ] ;then  # Checking is Username and Folder is Valied
  45.         echo "The given diroctory and user is exist"
  46.         HomePathOfCUser=`grep ^$UName /etc/passwd | gawk -F: '{print $6}'`
  47.        
  48.         for file in $DirName/* # looping files is file veriable
  49.         do
  50.             if [ -f "$file" ] ;then #Checking is a regular file  
  51.                 #echo "$file"
  52.                 #ls -l "$file" | awk '{ print $3 }'
  53.                 #ls -l "$file"| awk '{ print $4 }'
  54.                 if [ "$UName" = `ls -l "$file" | awk '{ print $3 }'` ] || [ "$UName" = `ls -l "$file"| awk '{ print $4 }'` ] ;then #Checking ownership and permission
  55.                    
  56.                    
  57.                     if [ -d "$HomePathOfCUser/Backup" ] ;then   # Checking Backup folder exist
  58.                         #echo "$HomePathOfCUser/Backup"
  59.                         if [  $CountFlLn -eq 10  ] ;then
  60.                         (( CountFlLn=  $CountFlLn + 1 ))  # Value for current line number
  61.                             cp -R "$file" $HomePathOfCUser/Backup"${file#$DirName}".bak
  62.                             echo "$file as a {file#$DirName}.bak" >> $HomePathOfCUser/Backup/Backup.log
  63.                             mv $HomePathOfCUser/Backup/Backup.log $HomePathOfCUser/Backup/Backup.log.$CountFl
  64.                             touch $HomePathOfCUser/Backup/Backup.log
  65.                             sleep 5
  66.                             (( CountFl=  $CountFl + 1 ))
  67.                             (( CountFlLn=  $CountFlLn - 10 ))
  68.            
  69.                         else
  70.                         (( CountFlLn=  $CountFlLn + 1 ))  # Value for current line number
  71.                             mkdir -p "$HomePathOfCUser/Backup${file#$DirName}"
  72.                             cp -R "$file" "$HomePathOfCUser/Backup${file#$DirName}.bak"
  73.                             echo "$file as a $file.bak" >> $HomePathOfCUser/Backup/Backup.log
  74.                            
  75.                         fi
  76.                        
  77.                     else
  78.                         mkdir -p "$HomePathOfCUser/Backup"
  79.                         touch "$HomePathOfCUser/Backup/Backup.log"
  80.                         mkdir -p "$HomePathOfCUser/Backup${file#$DirName}"
  81.                         (( CountFlLn=  $CountFlLn + 1 ))
  82.                         cp -R "$file" $HomePathOfCUser/Backup"${file#$DirName}".bak
  83.                         echo "$file as a ${file#$DirName}.bak" >> "$HomePathOfCUser/Backup/Backup.log"
  84.                        
  85.                     fi
  86.                 else
  87.                     echo "You dont have permission to access tis file $file "
  88.                 fi
  89.             fi
  90.         done
  91.     else
  92.         echo "The given diroctory or user is not exist"
  93.         exit 1
  94.     fi
  95. fi
Add Comment
Please, Sign In to add comment