Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #RJ Trenchard 100281657
- # Prints a message describing program usage and exit 3 if no argument is given
- # Takes a list of arguments representing files
- # Tries to compress each file using 4 different compression utilities: gzip, bzip2, zip, 7z
- # Use the following file extensions:
- # .gz : gzip
- # .bz2 : bzip2
- # .zip : zip
- # .7z : 7z
- # Keeps whatever version is smallest, and deletes the other (including the original)
- # If the original is smallest, keeps it.
- # The kept file should be in the same directory as the original was
- # Prints a message saying which file it's keeping
- # Does everything in parallel. All of the input files are processed simultaneously. All 4 compressions are done for each file simultaneously.
- # Files and paths may contain spaces
- # You may assume that there are no naming collisions
- # HINT: this as not as complicated as it seems. Do some actions, compare results, choose best result. Repeated for each input.
- # HINT: make it work in serial first
- # HINT: use the compression programs as filters with file redirection. eg: 7z < fin > fout
- # HINT: you way want to use basename and dirname, but it's easier without them
- # HINT: you may want to use stat. Dont. Just read the man page for ls.
- if [ $# -eq 0 ]; then
- echo "Usage: enter filenames separated by spaces."
- else
- for file in "$@"; do
- if [ -f $file ]; then
- #quietly compress the file in 4 formats simasldifjeously
- (
- 7z a <"$file" >"file.7Z" & pid_of_7z=$!
- zip <"$file" >"$file.zip" & pid_of_zip=$!
- gzip <"$file" >"$file.gz" & pid_of_gz=$!
- bzip2 <"$file" >"$file.bz2" & pid_of_bz=$!
- #wait on subprocesses to finish, otherwise we'll have a 0byte file
- wait $pid_of_7z $pid_of_zip $pid_of_gz $pid_of_bz
- #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.
- ls -1 -S -s --file-type "$file" "$file.7Z" "$file.zip" "$file.gz" "$file.bz2" | while read line; do
- echo $line
- done
- ) 2>/dev/null &
- #delete the biggest files
- else
- echo "$file is not a valid file" >&2
- fi
- done
- fi
- #wait until everything is done for a clean shell.
- wait
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement