From d560d7ec4bf87a9f9dd2f7135489b26db5f9fb4b Mon Sep 17 00:00:00 2001 From: Lily Chung Date: Fri, 20 Sep 2019 01:38:57 -0700 Subject: Add the -0 option to null-terminate filenames. --- sor | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'sor') 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 -- cgit v1.2.3