From e0a4241baa6e6ca72c9eedb8150cc8715240b05b Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 13 Jan 2019 17:32:25 -0800 Subject: [PATCH] Warn if a shebang's interpreter ends in / (fixes #373) --- CHANGELOG.md | 1 + src/ShellCheck/Analytics.hs | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71f1a88..69cb66c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Since previous release ### Added - Preliminary support for fix suggestions +- SC2246: Warn if a shebang's interpreter ends with / - SC2245: Warn that Ksh ignores all but the first glob result in `[` - SC2243/SC2244: Suggest using explicit -n for `[ $foo ]` diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 2631a58..6230f5b 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -474,6 +474,8 @@ prop_checkShebang7 = verifyNotTree checkShebang "#!/usr/bin/env ash\n# shellchec prop_checkShebang8 = verifyTree checkShebang "#!bin/sh\ntrue" prop_checkShebang9 = verifyNotTree checkShebang "# shellcheck shell=sh\ntrue" prop_checkShebang10= verifyNotTree checkShebang "#!foo\n# shellcheck shell=sh ignore=SC2239\ntrue" +prop_checkShebang11= verifyTree checkShebang "#!/bin/sh/\ntrue" +prop_checkShebang12= verifyTree checkShebang "#!/bin/sh/ -xe\ntrue" checkShebang params (T_Annotation _ list t) = if any isOverride list then [] else checkShebang params t where @@ -485,8 +487,13 @@ checkShebang params (T_Script id sb _) = execWriter $ do err id 2148 "Tips depend on target shell and yours is unknown. Add a shebang." when (executableFromShebang sb == "ash") $ warn id 2187 "Ash scripts will be checked as Dash. Add '# shellcheck shell=dash' to silence." - unless (null sb || "/" `isPrefixOf` sb) $ - err id 2239 "Ensure the shebang uses an absolute path to the interpreter." + unless (null sb) $ do + unless ("/" `isPrefixOf` sb) $ + err id 2239 "Ensure the shebang uses an absolute path to the interpreter." + case words sb of + first:_ -> + when ("/" `isSuffixOf` first) $ + err id 2246 "This shebang specifies a directory. Ensure the interpreter is a file." prop_checkForInQuoted = verify checkForInQuoted "for f in \"$(ls)\"; do echo foo; done"