iankelling.org
/
git
/
tee-unique
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
7f06af6
)
fix edge case of empty string argument
author
Ian Kelling
<ian@iankelling.org>
Fri, 25 Jul 2014 09:56:03 +0000
(
02:56
-0700)
committer
Ian Kelling
<ian@iankelling.org>
Fri, 25 Jul 2014 10:07:19 +0000
(
03:07
-0700)
appendu
patch
|
blob
|
history
appendu-function
patch
|
blob
|
history
diff --git
a/appendu
b/appendu
index c18830da8662a9a22972cdf9f8cae13b0aebaaac..feb58d4ee5b283902f189088d2cae823eeaaf556 100755
(executable)
--- 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"
-- 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
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 file="$1"
shift
- local readsudo=false
local file_exists=false
if [[ -e $file ]]; then
file_exists=true
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")"
[[ -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"
# 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.
# 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
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
done
else
for string in "${strings[@]}"; do
diff --git
a/appendu-function
b/appendu-function
index 7749aa47e7fafa3b29c5b158a149ba7e5995e690..6c97f6cb5f57914469c38d54a6445757a34506c0 100644
(file)
--- 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"
-- 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
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 file="$1"
shift
- local readsudo=false
local file_exists=false
if [[ -e $file ]]; then
file_exists=true
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")"
[[ -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"
# 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.
# 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
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
done
else
for string in "${strings[@]}"; do