aboutsummaryrefslogtreecommitdiff
path: root/sor
diff options
context:
space:
mode:
Diffstat (limited to 'sor')
-rwxr-xr-xsor32
1 files changed, 29 insertions, 3 deletions
diff --git a/sor b/sor
index 0960db8..f5d18a0 100755
--- a/sor
+++ b/sor
@@ -24,6 +24,7 @@ For each line from standard input, evaluate the specified SNIPPETs under Bash
with the line as the argument. Print the line if any snippet exits with
status 0.
+ -0, --null separate input and output filenames by null
--help display this help and exit
EOF
}
@@ -32,18 +33,43 @@ ask_for_help() {
echo "Try 'sor --help' for more information."
}
-if ! temp=$(getopt -s bash -n sor -o '' -l help -- "$@"); then
+read_filename() {
+ local null_terminate="$1"
+ local target_var="$2"
+ if [ "$null_terminate" = "true" ]; then
+ IFS= read -r -d '' "$target_var"
+ else
+ read "$target_var"
+ fi
+}
+
+print_filename() {
+ local null_terminate="$1"
+ local filename="$2"
+ if [ "$null_terminate" = "true" ]; then
+ printf '%s\0' "$filename"
+ else
+ echo "$filename"
+ fi
+}
+
+if ! temp=$(getopt -s bash -n sor -o '0' -l null,help -- "$@"); then
ask_for_help >&2
exit 1
fi
eval set -- "$temp"
unset temp
+null_terminate="false"
while true; do
case "$1" in
--help)
help
exit 0
;;
+ -0|--null)
+ shift
+ null_terminate="true"
+ ;;
--)
shift
break
@@ -55,10 +81,10 @@ while true; do
esac
done
-while read file; do
+while read_filename "$null_terminate" file; do
for test in "$@"; do
if eval "$test \"$file\""; then
- echo "$file"
+ print_filename "$null_terminate" "$file"
continue 2
fi
done