X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=appendu-function;h=6c97f6cb5f57914469c38d54a6445757a34506c0;hb=27a02f00b4498bf7b1519f36d73a634c81710957;hp=7749aa47e7fafa3b29c5b158a149ba7e5995e690;hpb=7f06af6d08609065f3475d5949046aa17e165813;p=tee-unique 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