From f30491f6faa8b62e45efcf8d38aa5c8ba0865a80 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 5 Jun 2017 16:26:02 -0700 Subject: [PATCH] Updated SC2002 (markdown) --- SC2002.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SC2002.md b/SC2002.md index 420ac82..5c8f3be 100644 --- a/SC2002.md +++ b/SC2002.md @@ -3,15 +3,15 @@ ### Problematic code: ```sh -cat file | tr ' ' _ | grep a_ -cat file | ( while read i; do echo "${i%?}"; done ) +cat file | tr ' ' _ | nl +cat file | while IFS= read -r i; do echo "${i%?}"; done ``` ### Correct code: ```sh -< file tr ' ' _ | grep a_ # **simple commands only, won't work with compounds -( while read i; do echo "${i%?}"; done ) < file # postfix works for everything +< file tr ' ' _ | nl +while IFS= read -r i; do echo "${i%?}"; done < file ``` ### Rationale: @@ -24,4 +24,4 @@ Many tools also accept optional filenames, e.g. `grep -q foo file` instead of `c ### Exceptions -None. +Pointing out UUOC is a long standing shell programming tradition, and removing them from a short-lived pipeline in a loop can speed it up by 2x. However, it's not necessarily a good use of time in practice, and rarely affects correctness. [[Ignore]] as you see fit. \ No newline at end of file