From 581be5878bb9c6e994121a1cc1f3121cbe29c563 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 28 Jul 2018 17:26:59 -0700 Subject: [PATCH] Suggest 'cat' when piping/redirecting to echo (fixes #1292) --- src/ShellCheck/Analytics.hs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 5b8aa36..efc7fdd 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -2856,16 +2856,25 @@ checkPipeToNowhere _ t = name <- getCommandBasename cmd guard $ name `elem` nonReadingCommands guard . not $ hasAdditionalConsumers cmd + -- Confusing echo for cat is so common that it's worth a special case + let suggestion = + if name == "echo" + then "Did you want 'cat' instead?" + else "Wrong command or missing xargs?" return $ warn (getId cmd) 2216 $ - "Piping to '" ++ name ++ "', a command that doesn't read stdin. Wrong command or missing xargs?" + "Piping to '" ++ name ++ "', a command that doesn't read stdin. " ++ suggestion checkRedir cmd = potentially $ do name <- getCommandBasename cmd guard $ name `elem` nonReadingCommands guard . not $ hasAdditionalConsumers cmd guard . not $ name `elem` ["cp", "mv", "rm"] && cmd `hasFlag` "i" + let suggestion = + if name == "echo" + then "Did you want 'cat' instead?" + else "Bad quoting, wrong command or missing xargs?" return $ warn (getId cmd) 2217 $ - "Redirecting to '" ++ name ++ "', a command that doesn't read stdin. Bad quoting or missing xargs?" + "Redirecting to '" ++ name ++ "', a command that doesn't read stdin. " ++ suggestion -- Could any words in a SimpleCommand consume stdin (e.g. echo "$(cat)")? hasAdditionalConsumers t = fromMaybe True $ do