From fec6fe251f8225694a08632b434896ff83d7b993 Mon Sep 17 00:00:00 2001 From: Stephen Checkoway Date: Thu, 15 Jun 2023 12:30:31 -0400 Subject: [PATCH] `let count++` gives a warning suggesting `(( count++ ))` instead. The deleted "Equivalently" code was _not_ equivalent and it didn't work because `count` was modified in a subshell (as shellcheck itself points out) --- SC2044.md | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/SC2044.md b/SC2044.md index 61f83bb..0997fd5 100644 --- a/SC2044.md +++ b/SC2044.md @@ -5,7 +5,7 @@ ```sh for file in $(find mydir -mtime -7 -name '*.mp3') do - let count++ + (( count++ )) echo "Playing file no. $count" play "$file" done @@ -23,25 +23,13 @@ The most general fix (that requires the least amount of thinking to apply) is ha ```sh while IFS= read -r -d '' file do - let count++ + (( count++ )) echo "Playing file no. $count" play "$file" done < <(find mydir -mtime -7 -name '*.mp3' -print0) echo "Played $count files" ``` -> Equivalently: -> -> ```sh -> find mydir -mtime -7 -name '*.mp3' -print0 | while IFS= read -r -d '' file -> do -> let count++ -> echo "Playing file no. $count" -> play "$file" -> done -> echo "Played $count files" -> ``` - In usage it's very similar to the `for` loop: it gets its output from a `find` statement, it executes a shell script body, it allows updating/aggregating variables, and the variables are available when the loop ends. It requires Bash, and works with GNU, Busybox, OS X, FreeBSD and OpenBSD find, but not POSIX find. @@ -54,7 +42,7 @@ If you don't need `find` logic like `-mtime -7` and just use it to match globs r shopt -s globstar nullglob for file in mydir/**/*.mp3 do - let count++ + (( count++ )) echo "Playing file no. $count" play "$file" done