Advertisement
Nattack

Untitled

Mar 1st, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.42 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.                 7z a <"$file" >"file.7Z" &      pid_of_7z=$!
  34.                 zip <"$file" >"$file.zip" &     pid_of_zip=$!
  35.                 gzip <"$file" >"$file.gz" &     pid_of_gz=$!
  36.                 bzip2 <"$file" >"$file.bz2" &   pid_of_bz=$!
  37.                
  38.                 #wait on subprocesses to finish, otherwise we'll have a 0byte file
  39.                 wait $pid_of_7z $pid_of_zip $pid_of_gz $pid_of_bz
  40.                
  41.                 #list in a single column all the files we just created, sort with -S, show only the largest 4 out of 5, and then remove them.
  42.                 ls -1 -S -s --file-type "$file" "$file.7Z" "$file.zip" "$file.gz" "$file.bz2" | while read line; do
  43.                     echo $line
  44.                 done
  45.                
  46.             ) 2>/dev/null &
  47.            
  48.             #delete the biggest files
  49.         else
  50.             echo "$file is not a valid file" >&2
  51.         fi
  52.     done
  53. fi
  54.  
  55. #wait until everything is done for a clean shell.
  56. wait
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement