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:
3a72bad
)
add pipe input
author
Ian Kelling
<ian@iankelling.org>
Tue, 17 Jun 2014 05:36:37 +0000
(22:36 -0700)
committer
Ian Kelling
<ian@iankelling.org>
Tue, 17 Jun 2014 05:36:37 +0000
(22:36 -0700)
appendu
patch
|
blob
|
history
appendu-function
patch
|
blob
|
history
diff --git
a/appendu
b/appendu
index f08a45c13a6248d3becbc2e5782e380163cfb469..c7455dccdbe43be08e1b5f858f8ef2db7f8f0fcc 100755
(executable)
--- a/
appendu
+++ b/
appendu
@@
-3,8
+3,10
@@
# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
appendu() {
# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
appendu() {
- local help="Usage: appendu [OPTION]... FILE LINE...
-Append unique. Append each LINE to FILE if it does not exist in FILE.
+ local help="Usage: appendu [OPTION]... FILE [LINE]...
+Append unique.
+Append each line to FILE if it does not exist in FILE.
+Use LINE if specified, else use lines from stdin.
Appended lines are output to the terminal.
-s don't try to use sudo when it would help us read or write the file
Appended lines are output to the terminal.
-s don't try to use sudo when it would help us read or write the file
@@
-28,8
+30,8
@@
Appended lines are output to the terminal.
fi
done
fi
done
- if
(( ${#@} < 2 ))
; then
- echo "error: need
2
or more arguments"
+ if
[[ ${#@} == 0 ]]
; then
+ echo "error: need
1
or more arguments"
echo "$help"
return 1
fi
echo "$help"
return 1
fi
@@
-54,8
+56,16
@@
Appended lines are output to the terminal.
readsudo=
writesudo=
fi
readsudo=
writesudo=
fi
- for x in "$@"; do
- [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
- done
+ if [[ $@ ]]; then
+ for x in "$@"; do
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ elif [[ ! -t 0 ]]; then
+ unset IFS
+ while read -r x; do
+ # duplicated from above
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ fi
}
appendu "$@"
}
appendu "$@"
diff --git
a/appendu-function
b/appendu-function
index 7a11f28e909ac3846435f6d121b45cddb7f909bf..40a4edfa4d6ef92c33530c77cd643461ffb06ed6 100644
(file)
--- a/
appendu-function
+++ b/
appendu-function
@@
-3,8
+3,10
@@
# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
appendu() {
# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
appendu() {
- local help="Usage: appendu [OPTION]... FILE LINE...
-Append unique. Append each LINE to FILE if it does not exist in FILE.
+ local help="Usage: appendu [OPTION]... FILE [LINE]...
+Append unique.
+Append each line to FILE if it does not exist in FILE.
+Use LINE if specified, else use lines from stdin.
Appended lines are output to the terminal.
-s don't try to use sudo when it would help us read or write the file
Appended lines are output to the terminal.
-s don't try to use sudo when it would help us read or write the file
@@
-28,8
+30,8
@@
Appended lines are output to the terminal.
fi
done
fi
done
- if
(( ${#@} < 2 ))
; then
- echo "error: need
2
or more arguments"
+ if
[[ ${#@} == 0 ]]
; then
+ echo "error: need
1
or more arguments"
echo "$help"
return 1
fi
echo "$help"
return 1
fi
@@
-54,7
+56,15
@@
Appended lines are output to the terminal.
readsudo=
writesudo=
fi
readsudo=
writesudo=
fi
- for x in "$@"; do
- [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
- done
+ if [[ $@ ]]; then
+ for x in "$@"; do
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ elif [[ ! -t 0 ]]; then
+ unset IFS
+ while read -r x; do
+ # duplicated from above
+ [[ -e "$file" ]] && $readsudo grep -q "^$x$" "$file" || $writesudo tee -a "$file"<<<"$x"
+ done
+ fi
}
}