From: Ian Kelling Date: Fri, 25 Jul 2014 09:56:03 +0000 (-0700) Subject: fix edge case of empty string argument X-Git-Url: https://iankelling.org/git/?p=tee-unique;a=commitdiff_plain;h=27a02f00b4498bf7b1519f36d73a634c81710957 fix edge case of empty string argument --- diff --git a/appendu b/appendu index c18830d..feb58d4 100755 --- a/appendu +++ b/appendu @@ -15,7 +15,7 @@ as a single LINE_SET. Appended text is output to the terminal. -- stop processing arguments --help display this message" - local readsudo writesudo x strings string + local readsudo writesudo x strings string content local dosudo=true while true; do @@ -42,11 +42,10 @@ as a single LINE_SET. Appended text is output to the terminal. local file="$1" shift - local readsudo=false local file_exists=false if [[ -e $file ]]; then file_exists=true - [[ -r $file ]] || readsudo=true + [[ -r $file ]] || readsudo=sudo [[ -w $file ]] || writesudo=sudo else local dir="$(dirname "$file")" @@ -78,16 +77,10 @@ as a single LINE_SET. Appended text is output to the terminal. # so sed only does what it always does when it was supposed to modify a file, # which is append a newline if there was none. sed -ie '$a\' "$file" - # this removes any trailing newline in the var, so we add it back on, - # because we want a consistent ending to match - local file_content - if $readsudo; then - file_content="$(sudo cat "$file") -" - else - file_content="$(<"$file") -" - fi + # command substitution removes any trailing newlines, so we have to add + # a non-newline ending, we randomly chose "b", then remove it. + content=$($readsudo cat "$file"; echo b) content=${content%b} + # we aren't using regex because we want to match strings, # but we also want our match to start at the beginning of a line, # or the beginning of the file, and to end at a line ending. @@ -97,7 +90,7 @@ as a single LINE_SET. Appended text is output to the terminal. local end=" *" for string in "${strings[@]}"; do - [[ $file_content != $start"$string"$end ]] && $writesudo tee -a "$file"<<<"$string" + [[ $content != $start"$string"$end ]] && $writesudo tee -a "$file"<<<"$string" done else for string in "${strings[@]}"; do diff --git a/appendu-function b/appendu-function index 7749aa4..6c97f6c 100644 --- a/appendu-function +++ b/appendu-function @@ -15,7 +15,7 @@ as a single LINE_SET. Appended text is output to the terminal. -- stop processing arguments --help display this message" - local readsudo writesudo x strings string + local readsudo writesudo x strings string content local dosudo=true while true; do @@ -42,11 +42,10 @@ as a single LINE_SET. Appended text is output to the terminal. local file="$1" shift - local readsudo=false local file_exists=false if [[ -e $file ]]; then file_exists=true - [[ -r $file ]] || readsudo=true + [[ -r $file ]] || readsudo=sudo [[ -w $file ]] || writesudo=sudo else local dir="$(dirname "$file")" @@ -78,16 +77,10 @@ as a single LINE_SET. Appended text is output to the terminal. # so sed only does what it always does when it was supposed to modify a file, # which is append a newline if there was none. sed -ie '$a\' "$file" - # this removes any trailing newline in the var, so we add it back on, - # because we want a consistent ending to match - local file_content - if $readsudo; then - file_content="$(sudo cat "$file") -" - else - file_content="$(<"$file") -" - fi + # command substitution removes any trailing newlines, so we have to add + # a non-newline ending, we randomly chose "b", then remove it. + content=$($readsudo cat "$file"; echo b) content=${content%b} + # we aren't using regex because we want to match strings, # but we also want our match to start at the beginning of a line, # or the beginning of the file, and to end at a line ending. @@ -97,7 +90,7 @@ as a single LINE_SET. Appended text is output to the terminal. local end=" *" for string in "${strings[@]}"; do - [[ $file_content != $start"$string"$end ]] && $writesudo tee -a "$file"<<<"$string" + [[ $content != $start"$string"$end ]] && $writesudo tee -a "$file"<<<"$string" done else for string in "${strings[@]}"; do