From 1c8870ece41f149133163174d6cec24743838f3a Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 17 May 2014 10:55:32 -0700 Subject: [PATCH] Created SC2143 (markdown) --- SC2143.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 SC2143.md diff --git a/SC2143.md b/SC2143.md new file mode 100644 index 0000000..ad4d167 --- /dev/null +++ b/SC2143.md @@ -0,0 +1,24 @@ +## Instead of [ -n $(foo | grep bar) ], use foo | grep -q bar . +### Problematic code: + + if [ "$(find . | grep 'IMG[0-9]')" ] + then + echo "Images found" + fi + +### Correct code: + + if find . | grep -q 'IMG[0-9]' + then + echo "Images found" + fi + +### Rationale: + +The problematic code has to iterate the entire directory and read all matching lines into memory before making a decision. + +The correct code is cleaner and stops at the first matching line, avoiding both iterating the rest of the directory and reading data into memory. + +### Contraindications + +None. \ No newline at end of file