From b2461a7b89317c32356787631d3d6228e50d1adb Mon Sep 17 00:00:00 2001 From: Emily Frost Date: Tue, 29 Jun 2021 10:46:17 -0500 Subject: [PATCH] Added basic error handling to nyan. --- nyan | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/nyan b/nyan index 697a960..ca6a11f 100755 --- a/nyan +++ b/nyan @@ -1,6 +1,8 @@ #!/bin/bash optstring="o:" -input_files="" +declare -i abort=0 + +# TODO: Check for dependencies pv and stat. while getopts ${optstring} args; do case $args in @@ -8,20 +10,28 @@ while getopts ${optstring} args; do :) echo "Output file required." ;; esac done - shift $((OPTIND-1)) -echo $output_file - echo "Concatenating files..." declare -i total_size=0 for fname in "$@"; do - #TODO: Make sure this file exists and isn't a directory! - current_file_size=$(stat -c%s "$fname") - let total_size=$total_size+$current_file_size - echo "${fname} is ${total_size}b and valid." + if [ ! -e "$fname" ]; then + abort=1 + echo "${fname} does not exist." + elif [ -d "$fname" ]; then + abort=1 + echo "${fname} is a directory." + else + current_file_size=$(stat -c%s "$fname") + total_size=$((total_size+current_file_size)) + # TODO: Use something more legible than bytes. + echo "Adding ${fname} at ${total_size}b." + fi done -cat "$@" | pv -epts "$total_size" > "$output_file" - -echo "Done!" +if [ $abort -eq 0 ]; then + cat "$@" | pv -epts "$total_size" > "$output_file" + echo "Done!" +else + echo "Errors occurred, concatenation aborted." +fi