Advertisement
vzmej

OS UNIX - Задача 3 - Exercises

May 20th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.20 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Function for comparing two files
  4. function compareFiles {
  5.         if diff $1 $2 >/dev/null; then
  6.                 echo true
  7.         else
  8.                 echo false
  9.         fi
  10. }
  11.  
  12. # Arguments check
  13. if [[ $# -lt 3 ]]; then
  14.         echo "Usage: `basename $0` source_folder/ destination_folder/"
  15.         exit 1
  16. fi
  17.  
  18. # Source directory existence check
  19. if [[ ! -d $1 ]]; then
  20.         echo "Source directory is not existing on the file system."
  21.         exit 1
  22. fi
  23.  
  24. # Destination directory existence check
  25. if [[ -d $2 ]]; then
  26.         rm -r $2
  27.         mkdir $2
  28. else
  29.         mkdir $2
  30. fi
  31.  
  32. # Variables
  33. files=()
  34. count=0
  35.  
  36. # Listing the content from $1 (source) directory
  37. for file in `find $1 -type f`; do
  38.         files[((count++))]=$file
  39. done
  40.  
  41. # Iterating the memory stored files
  42. echo "" > $3
  43. for file in ${files[@]}; do
  44.         filename=$(echo $file | sed 's/\//__/g')
  45.         destination="${2}${filename}"
  46.         cp $file $destination
  47.         for copiedFile in `find $2 -type f`; do
  48.                 check=$(compareFiles $file $copiedFile)
  49.                 if [[ ${check} = true ]]; then
  50.                         echo "$file $copiedFile" >> $3
  51.                 fi
  52.         done
  53. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement