From d6d74706835d4a8220bda063c9af70793bfe268c Mon Sep 17 00:00:00 2001 From: Jan Vitturi Date: Fri, 20 Sep 2019 15:36:14 +0200 Subject: [PATCH 1/5] Add caveats for unexpanded globs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 42ba1f3..263ff18 100644 --- a/README.md +++ b/README.md @@ -439,6 +439,8 @@ $ lines ~/.bashrc This works by passing the output of the glob to the function and then counting the number of arguments. +**CAVEAT:** When the glob does not match anything (empty directory or no matching files) it is not expanded and the function returns `1`. + **Example Function:** ```sh @@ -575,6 +577,8 @@ done < "file" Don’t use `ls`. +**CAVEAT:** When the glob does not match anything (empty directory or no matching files) the variable will contain the unexpanded glob. + ```shell # Greedy example. for file in *; do From d5a96302f629b526f94745e6f4b431b02f7b6e4e Mon Sep 17 00:00:00 2001 From: Jan Vitturi Date: Fri, 20 Sep 2019 15:48:25 +0200 Subject: [PATCH 2/5] Fix confusing substring examples The placeholders do not work since `sub_string2` contains `sub_string` and give the impression of fall-through statements. --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 42ba1f3..a91f9c3 100644 --- a/README.md +++ b/README.md @@ -193,12 +193,12 @@ John Black is my name. ```shell case $var in - *sub_string*) + *sub_string1*) # Do stuff ;; *sub_string2*) - # Do more stuff + # Do other stuff ;; *) @@ -213,12 +213,12 @@ esac ```shell case $var in - sub_string*) + sub_string1*) # Do stuff ;; sub_string2*) - # Do more stuff + # Do other stuff ;; *) @@ -233,12 +233,12 @@ esac ```shell case $var in - *sub_string) + *sub_string1) # Do stuff ;; *sub_string2) - # Do more stuff + # Do other stuff ;; *) From 02c30875695f0530b42ca497138f9c3f1043c090 Mon Sep 17 00:00:00 2001 From: Jan Vitturi Date: Fri, 20 Sep 2019 18:50:16 +0200 Subject: [PATCH 3/5] Check for unexpanded globs --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 263ff18..979af66 100644 --- a/README.md +++ b/README.md @@ -577,21 +577,24 @@ done < "file" Don’t use `ls`. -**CAVEAT:** When the glob does not match anything (empty directory or no matching files) the variable will contain the unexpanded glob. +**CAVEAT:** When the glob does not match anything (empty directory or no matching files) the variable will contain the unexpanded glob. To avoid working on unexpanded globs check the existence of the file contained in the variable using the appropriate [file conditional](#file-conditionals). Be aware that symbolic links are resolved. ```shell # Greedy example. for file in *; do + [ -e "$file" ] || [ -L "$file" ] || continue printf '%s\n' "$file" done # PNG files in dir. for file in ~/Pictures/*.png; do + [ -f "$file" ] || continue printf '%s\n' "$file" done # Iterate over directories. for dir in ~/Downloads/*/; do + [ -d "$dir" ] || continue printf '%s\n' "$dir" done ``` From 172056a6c7f488d36df1e0cdf1fed709341af845 Mon Sep 17 00:00:00 2001 From: Crestwave Date: Mon, 23 Sep 2019 20:19:13 +0800 Subject: [PATCH 4/5] Add support for lines that end without a newline --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2edfe63..b88936a 100644 --- a/README.md +++ b/README.md @@ -393,10 +393,11 @@ Alternative to the `head` command. head() { # Usage: head "n" "file" while read -r line; do - [ "$i" = "$1" ] && break printf '%s\n' "$line" i=$((i+1)) + [ "$i" = "$1" ] && return done < "$2" + [ -n "$line" ] && printf %s "$line" } ``` @@ -420,7 +421,7 @@ Alternative to `wc -l`. ```sh lines() { # Usage: lines "file" - while read -r _; do + while read -r line || [ -n "$line" ]; do lines=$((lines+1)) done < "$1" @@ -568,7 +569,7 @@ done ## Loop over the contents of a file ```shell -while read -r line; do +while read -r line || [ -n "$line" ]; do printf '%s\n' "$line" done < "file" ``` From 7b09145c268388284d6380cf4b473db66656a479 Mon Sep 17 00:00:00 2001 From: Crestwave Date: Mon, 23 Sep 2019 20:32:26 +0800 Subject: [PATCH 5/5] docs: update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b88936a..1b8c228 100644 --- a/README.md +++ b/README.md @@ -397,6 +397,7 @@ head() { i=$((i+1)) [ "$i" = "$1" ] && return done < "$2" + [ -n "$line" ] && printf %s "$line" } ```