aboutsummaryrefslogtreecommitdiff
path: root/sor
diff options
context:
space:
mode:
authorGravatar Lily Chung <lkdc@mit.edu>2019-09-20 01:38:57 -0700
committerGravatar Lily Chung <lkdc@mit.edu>2019-09-20 01:48:41 -0700
commitd560d7ec4bf87a9f9dd2f7135489b26db5f9fb4b (patch)
treef9c9b3fdceb21c017fd39fb3858f9999007ed70d /sor
parentf80b980d4a8893836ba5bc108ecf9068676f4f6a (diff)
Add the -0 option to null-terminate filenames.
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