From af678fe7b32ab75c1171ad4309a82b0aae16ee24 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 25 Feb 2022 20:01:02 -0800 Subject: [PATCH] Updated SC2144 (markdown) --- SC2144.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/SC2144.md b/SC2144.md index 9c22b1b..d48231d 100644 --- a/SC2144.md +++ b/SC2144.md @@ -45,9 +45,24 @@ for f in /path/to/your/files*; do done ``` + + ### Exceptions -If filename expansion (globbing) is disabled (`set -f`), then the `[` statement works as expected. +If you are sure there will only ever be exactly 0 or 1 matches -- and `nullglob` is not enabled -- then the test happens to work. + +You may still want to consider making this assumption explicit and failing fast if it's ever violated: + +``` +files=( dir/file* ) +[ "${#files[@]}" -ge 2 ] || exit 1 +if [ -e "${files[0]}" ] +then + echo "The file exists" +else + echo "No such file" +fi +``` ### Related resources: