Advertisement
Nattack

Untitled

Mar 1st, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.73 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #RJ Trenchard 100281657
  4.  
  5. # Prints a message describing program usage and exit 3 if no argument is given
  6. # Takes a list of arguments representing files
  7. # Tries to compress each file using 4 different compression utilities: gzip, bzip2, zip, 7z
  8. # Use the following file extensions:
  9. # .gz : gzip
  10. # .bz2 : bzip2
  11. # .zip : zip
  12. # .7z : 7z
  13. # Keeps whatever version is smallest, and deletes the other (including the original)
  14. # If the original is smallest, keeps it.
  15. # The kept file should be in the same directory as the original was
  16. # Prints a message saying which file it's keeping
  17. # Does everything in parallel. All of the input files are processed simultaneously. All 4 compressions are done for each file simultaneously.
  18. # Files and paths may contain spaces
  19. # You may assume that there are no naming collisions
  20. # HINT: this as not as complicated as it seems. Do some actions, compare results, choose best result. Repeated for each input.
  21. # HINT: make it work in serial first
  22. # HINT: use the compression programs as filters with file redirection. eg: 7z < fin > fout
  23. # HINT: you way want to use basename and dirname, but it's easier without them
  24. # HINT: you may want to use stat. Dont. Just read the man page for ls.
  25.  
  26. if [ $# -eq 0 ]; then
  27.     echo "Usage: enter filenames separated by spaces."
  28. else
  29.     for file in "$@"; do
  30.         if [ -f "$file" ]; then
  31.             #quietly compress the file in 4 formats simasldifjeously
  32.             (
  33.                 #I don't get 7zip's command line at all
  34.                 #and I don't understand it quietly!
  35.                 7z a "$file.7Zip" "$file" >/dev/null &  pid_of_7z=$!
  36.                 zip <"$file" >"$file.zip" &             pid_of_zip=$!
  37.                 gzip <"$file" >"$file.gz" &             pid_of_gz=$!
  38.                 bzip2 <"$file" >"$file.bz2" &           pid_of_bz=$!
  39.                
  40.                 #wait on subprocesses to finish, otherwise we'll have a 0byte file
  41.                 wait $pid_of_7z $pid_of_zip $pid_of_gz $pid_of_bz
  42.                
  43.                 #list in a single column all the files we just created, sort by size with -S, show only the largest 4 out of 5, and then remove them.
  44.                 ls -1 -S --file-type "$file" "$file.7Zip" "$file.zip" "$file.gz" "$file.bz2" | head -n 4 | while read line; do
  45.                     rm $line
  46.                 done
  47.                
  48.                 #lazy
  49.                 echo "keeping `ls -1 --file-type \"$file\" \"$file.7Zip\" \"$file.zip\" \"$file.gz\" \"$file.bz2\"`"
  50.                
  51.             ) 2>/dev/null &
  52.            
  53.             #delete the biggest files
  54.         else
  55.             echo "$file is not a valid file" >&2
  56.         fi
  57.     done
  58. fi
  59.  
  60. #wait until everything is done for a clean shell.
  61. wait
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement