From 3bdbea3ebfd090b066a563646ced5e3889bbf101 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Tue, 31 Mar 2020 16:11:12 +0800 Subject: [PATCH 1/2] sor: sensible quote and the like * A few quotes are added back to remove the possibility of the shell even wasting time on trying to word-split * read -r for no mangling of backslashes * double-bracketed [[ ]] test for quote-free fun * change echo to printf '%s\n' for robustness against strange names starting with a dash * %q quoting for eval to guard against trivial injections --- sor | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/sor b/sor index abe344d..9fcc065 100755 --- a/sor +++ b/sor @@ -36,23 +36,25 @@ ask_for_help() { read_filename() { local null_terminate=$1 local target_var=$2 - if [ $null_terminate = true ]; then - IFS= read -r -d '' $target_var + if [[ $null_terminate == true ]]; then + IFS= read -r -d '' "$target_var" else - read $target_var + # shellcheck disable=SC2229 + read -r "$target_var" fi } print_filename() { local null_terminate=$1 - local filename="$2" - if [ $null_terminate = true ]; then + local filename=$2 + if [[ $null_terminate == true ]]; then printf '%s\0' "$filename" else - echo "$filename" + printf '%s\n' "$filename" fi } +# This requires util-linux getopt. if ! temp=$(getopt -s bash -n sor -o 0 -l null,help -- "$@"); then ask_for_help >&2 exit 1 @@ -81,10 +83,12 @@ while true; do esac done -while read_filename $null_terminate file; do +while read_filename "$null_terminate" file; do + # shellcheck disable=SC2154 + qfile=$(printf '%q' "$file") for test in "$@"; do - if eval "$test \"$file\""; then - print_filename $null_terminate "$file" + if eval "$test $qfile"; then + print_filename "$null_terminate" "$file" continue 2 fi done From d4473715e12a455efe7df9cd8cd459fe09786a7a Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Fri, 14 Aug 2020 13:55:28 +0800 Subject: [PATCH 2/2] sor: change documentation so people don't call `exit' in scripts --- sor | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sor b/sor index 9fcc065..fd72465 100755 --- a/sor +++ b/sor @@ -21,8 +21,7 @@ help() { short_usage cat <