From 90cca02015d50e54abb8280333c12a1a30e7b343 Mon Sep 17 00:00:00 2001 From: John Meyer <20781397+0x326@users.noreply.github.com> Date: Thu, 16 Jun 2022 12:51:38 -0500 Subject: [PATCH] Add "find | while" syntax --- SC2044.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SC2044.md b/SC2044.md index 2c37053..61f83bb 100644 --- a/SC2044.md +++ b/SC2044.md @@ -30,6 +30,18 @@ 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.