From d0f805be9d8aa8b63329517908d27f8cf6c2a4c5 Mon Sep 17 00:00:00 2001 From: didierC Date: Sat, 15 Feb 2025 09:41:19 +0100 Subject: [PATCH 1/8] Upgrade JDK 17, maven plugins and correct license comment --- pom.xml | 149 ++-- src/etc/header.txt | 2 +- .../plugins/shellcheck/ShellCheckPlugin.java | 4 +- .../shellcheck/checks/CheckRepository.java | 4 +- .../highlighting/HighlightingData.java | 4 +- .../highlighting/ShellHighlighting.java | 4 +- .../highlighting/ShellLocation.java | 4 +- .../shellcheck/languages/ShellLanguage.java | 4 +- .../languages/ShellQualityProfile.java | 4 +- .../shellcheck/lexer/AbstractBashLexer.java | 4 +- .../plugins/shellcheck/lexer/BashLexer.java | 4 +- .../shellcheck/lexer/BashLexerBase.java | 773 +++++++++--------- .../shellcheck/lexer/HeredocLexingState.java | 4 +- .../shellcheck/lexer/HeredocSharedImpl.java | 4 +- .../plugins/shellcheck/lexer/ShellLexer.java | 4 +- .../shellcheck/lexer/StringLexingstate.java | 4 +- .../sonar/plugins/shellcheck/lexer/Token.java | 4 +- .../plugins/shellcheck/lexer/TokenType.java | 4 +- .../shellcheck/measures/LineCountData.java | 4 +- .../shellcheck/measures/LineCountParser.java | 4 +- .../shellcheck/measures/LineCounter.java | 4 +- .../rules/ShellCheckRulesDefinition.java | 4 +- .../shellcheck/rules/ShellCheckSensor.java | 4 +- .../settings/ShellCheckSettings.java | 4 +- .../plugins/shellcheck/util/FileSystem.java | 4 +- .../shellcheck/ShellCheckPluginTest.java | 4 +- .../sonar/plugins/shellcheck/Utils.java | 4 +- .../checks/CheckRepositoryTest.java | 4 +- .../highlighting/HighlightingDataTest.java | 4 +- .../highlighting/ShellHighlightingTest.java | 4 +- .../highlighting/ShellLocationTest.java | 4 +- .../languages/ShellLanguageTest.java | 4 +- .../languages/ShellQualityProfileTest.java | 4 +- .../shellcheck/lexer/BashLexerTest.java | 4 +- .../lexer/HeredocLexingStateTest.java | 4 +- .../lexer/HeredocSharedImplTest.java | 4 +- .../lexer/StringLexingstateTest.java | 4 +- .../plugins/shellcheck/lexer/TokenTest.java | 4 +- .../measures/LineCountDataTest.java | 4 +- .../shellcheck/measures/LineCounterTest.java | 4 +- .../rules/ShellCheckRulesDefinitionTest.java | 4 +- .../rules/ShellCheckSensorNoRuleTest.java | 4 +- .../rules/ShellCheckSensorTest.java | 4 +- .../settings/ShellCheckSettingsTest.java | 4 +- .../shellcheck/util/FileSystemTest.java | 4 +- 45 files changed, 559 insertions(+), 533 deletions(-) diff --git a/pom.xml b/pom.xml index 2703c0a..549aebf 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,11 @@ - + 4.0.0 com.github.sbaudoin sonar-shellcheck-plugin - 2.5.0 + 3.0.0-SNAPSHOT sonar-plugin ShellCheck Analyzer @@ -12,16 +14,18 @@ 2018 - UTF-8 https://oss.sonatype.org/service/local/staging/deploy/maven2 https://oss.sonatype.org/content/repositories/snapshots + 7.1 - 1.8 - 2.7 - 2.19.1 1.7.1 0.8.6 **/lexer/BashLexerBase.java + + UTF-8 + 8 + 8 + 8 @@ -138,21 +142,77 @@ org.apache.maven.plugins maven-javadoc-plugin + 3.11.2 - org.codehaus.mojo - cobertura-maven-plugin - ${cobertura-maven-plugin.version} + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 org.apache.maven.plugins maven-surefire-plugin - ${maven-surefire-plugin.version} + 3.5.2 + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + org.apache.maven.plugins + maven-source-plugin + 3.3.1 + + + com.mycila + license-maven-plugin + 4.6 + + + org.apache.maven.plugins + maven-gpg-plugin + 3.2.7 + + + org.apache.maven.plugins + maven-release-plugin + 3.1.1 + + + com.github.spotbugs + spotbugs-maven-plugin + 4.9.0 + + + de.jflex + jflex-maven-plugin + 1.9.1 + + + org.sonarsource.sonar-packaging-maven-plugin + sonar-packaging-maven-plugin + 1.17 + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.7.0 org.apache.maven.plugins - maven-project-info-reports-plugin - 2.9 + maven-deploy-plugin + 3.1.3 + + + org.apache.maven.plugins + maven-install-plugin + 3.1.3 + + + org.apache.maven.plugins + maven-resources-plugin + 3.3.1 @@ -160,23 +220,12 @@ org.sonarsource.sonar-packaging-maven-plugin sonar-packaging-maven-plugin - 1.17 true shellcheck com.github.sbaudoin.sonar.plugins.shellcheck.ShellCheckPlugin - - org.apache.maven.plugins - maven-compiler-plugin - 3.6.0 - - ${jdk.min.version} - ${jdk.min.version} - ${project.build.sourceEncoding} - - org.apache.maven.plugins maven-surefire-plugin @@ -191,7 +240,6 @@ org.apache.maven.plugins maven-source-plugin - 3.0.1 @@ -201,40 +249,22 @@ - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.4 + com.mycila + license-maven-plugin - - https://docs.oracle.com/javase/8/docs/api/ - - ${jdk.min.version} - - - - attach-javadocs - - jar - - - - - - com.mycila.maven-license-plugin - maven-license-plugin - 1.10.b1 - -
src/etc/header.txt
+ + +
src/etc/header.txt
+ + src/**/*.java + + true +
+
false true - false - - src/**/*.java - - true true true - UTF-8
@@ -248,7 +278,6 @@ org.apache.maven.plugins maven-gpg-plugin - 1.6 sign-artifacts @@ -262,7 +291,6 @@ org.apache.maven.plugins maven-release-plugin - 2.5.3 true false @@ -273,7 +301,6 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.7 true ossrh @@ -283,6 +310,7 @@ + audit @@ -291,7 +319,6 @@ org.jacoco jacoco-maven-plugin - ${jacoco.version} prepare-agent @@ -309,9 +336,8 @@ - org.codehaus.mojo - findbugs-maven-plugin - 3.0.4 + com.github.spotbugs + spotbugs-maven-plugin @@ -323,7 +349,6 @@ de.jflex jflex-maven-plugin - 1.7.0 @@ -343,4 +368,4 @@ -
+
\ No newline at end of file diff --git a/src/etc/header.txt b/src/etc/header.txt index 91d33ac..73a6a9e 100644 --- a/src/etc/header.txt +++ b/src/etc/header.txt @@ -1,4 +1,4 @@ -Copyright (c) 2018-2021, Sylvain Baudoin +Copyright (c) 2018-2025, Sylvain Baudoin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java index 85e5703..c7004a1 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java index e1bd34c..90cd674 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingData.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingData.java index 0085506..4884aa3 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingData.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingData.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java index 9af3103..7404a89 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocation.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocation.java index 630d876..fe85bab 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocation.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocation.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java index 2b767b5..3fb3511 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfile.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfile.java index fd35efa..70bdf39 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfile.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfile.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/AbstractBashLexer.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/AbstractBashLexer.java index 6b0c07f..7218741 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/AbstractBashLexer.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/AbstractBashLexer.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexer.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexer.java index fd3a5bb..76258fe 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexer.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexer.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerBase.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerBase.java index 2239208..a0a88ba 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerBase.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerBase.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + /* The following code was generated by JFlex 1.7.0 */ /* @@ -23,7 +24,7 @@ /** - * This class is a scanner generated by + * This class is a scanner generated by * JFlex 1.7.0 * from the specification file {@code src/main/jflex/bash.jflex} */ @@ -65,16 +66,16 @@ public abstract class BashLexerBase extends AbstractBashLexer { * at the beginning of a line * l is of the form l = 2*k, k a non negative integer */ - private static final int ZZ_LEXSTATE[] = { - 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, - 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, + private static final int ZZ_LEXSTATE[] = { + 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, + 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 18, 19, 19, 20, 20 }; - /** + /** * Translates characters to character classes */ - private static final String ZZ_CMAP_PACKED = + private static final String ZZ_CMAP_PACKED = "\11\0\1\4\1\1\1\0\1\4\1\2\22\0\1\40\1\6\1\10"+ "\1\5\1\7\1\76\1\21\1\11\1\46\1\32\1\22\1\24\1\51"+ "\1\35\1\16\1\75\1\25\7\31\2\20\1\100\1\34\1\73\1\45"+ @@ -208,12 +209,12 @@ public abstract class BashLexerBase extends AbstractBashLexer { "\21\14\u1144\0\ua6d7\14\51\0\u1035\14\13\0\336\14\2\0\u1682\14\u295e\0"+ "\u021e\14\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\u05f0\0"; - /** + /** * Translates characters to character classes */ private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED); - /** + /** * Translates DFA states to action switch labels. */ private static final int [] ZZ_ACTION = zzUnpackAction(); @@ -282,7 +283,7 @@ private static int zzUnpackAction(String packed, int offset, int [] result) { } - /** + /** * Translates a state to a row index in the transition table */ private static final int [] ZZ_ROWMAP = zzUnpackRowMap(); @@ -364,7 +365,7 @@ private static int zzUnpackRowMap(String packed, int offset, int [] result) { return j; } - /** + /** * The transition table of the DFA */ private static final int [] ZZ_TRANS = zzUnpackTrans(); @@ -1097,7 +1098,7 @@ public BashLexerBase(java.io.Reader in) { } - /** + /** * Unpacks the compressed character translation table. * * @param packed the packed character translation table @@ -1388,9 +1389,9 @@ else if (zzAtEOF) zzEndReadL = zzEndRead; zzMarkedPosL = zzMarkedPos; zzBufferL = zzBuffer; - if (eof) + if (eof) zzPeek = false; - else + else zzPeek = zzBufferL[zzMarkedPosL] == '\n'; } if (zzPeek) yyline--; @@ -1461,12 +1462,12 @@ else if (zzAtEOF) { } else { switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) { - case 1: + case 1: { return token(TokenType.BAD_CHARACTER); - } + } // fall through case 186: break; - case 2: + case 2: { if (yystate() == X_HERE_STRING) { closeHereStringIfAvailable(); return token(TokenType.LINE_FEED); @@ -1482,121 +1483,121 @@ else if (zzAtEOF) { } return token(TokenType.LINE_FEED); - } + } // fall through case 187: break; - case 3: + case 3: { return token(TokenType.BACKSLASH); - } + } // fall through case 188: break; - case 4: + case 4: { return token(TokenType.WHITESPACE); - } + } // fall through case 189: break; - case 5: + case 5: { return token(TokenType.COMMENT); - } + } // fall through case 190: break; - case 6: + case 6: { return token(TokenType.BANG_TOKEN); - } + } // fall through case 191: break; - case 7: + case 7: { return token(TokenType.DOLLAR); - } + } // fall through case 192: break; - case 8: + case 8: { stringParsingState().enterString(); if (yystate() == X_HERE_STRING && !isInHereStringContent()) enterHereStringContent(); goToState(X_STRINGMODE); return token(TokenType.STRING_BEGIN); - } + } // fall through case 193: break; - case 9: + case 9: { return token(TokenType.WORD); - } + } // fall through case 194: break; - case 10: + case 10: { return token(TokenType.INTEGER_LITERAL); - } + } // fall through case 195: break; - case 11: + case 11: { closeHereStringIfAvailable(); return token(TokenType.AMP); - } + } // fall through case 196: break; - case 12: + case 12: { return token(TokenType.AT); - } + } // fall through case 197: break; - case 13: + case 13: { return token(TokenType.RIGHT_PAREN); - } + } // fall through case 198: break; - case 14: + case 14: { if (yystate() == S_BACKQUOTE) backToPreviousState(); else goToState(S_BACKQUOTE); return token(TokenType.BACKQUOTE); - } + } // fall through case 199: break; - case 15: + case 15: { closeHereStringIfAvailable(); return token(TokenType.SEMI); - } + } // fall through case 200: break; - case 16: + case 16: { return token(TokenType.PIPE); - } + } // fall through case 201: break; - case 17: + case 17: { if (yystate() == S_DOLLAR_PREFIXED) backToPreviousState(); goToState(S_ARITH_SQUARE_MODE); return token(TokenType.EXPR_ARITH_SQUARE); - } + } // fall through case 202: break; - case 18: + case 18: { return token(TokenType.EQ); - } + } // fall through case 203: break; - case 19: + case 19: { if (yystate() == S_DOLLAR_PREFIXED) backToPreviousState(); stringParsingState().enterSubshell(); goToState(S_SUBSHELL); return token(TokenType.LEFT_PAREN); - } + } // fall through case 204: break; - case 20: + case 20: { return token(TokenType.LEFT_CURLY); - } + } // fall through case 205: break; - case 21: + case 21: { if (yystate() == X_HEREDOC && !heredocState().isExpectingEvaluatingHeredoc()) return token(TokenType.HEREDOC_LINE); return token(TokenType.RIGHT_CURLY); - } + } // fall through case 206: break; - case 22: + case 22: { return token(TokenType.GREATER_THAN); - } + } // fall through case 207: break; - case 23: + case 23: { return token(TokenType.LESS_THAN); - } + } // fall through case 208: break; - case 24: + case 24: { return token(TokenType.COND_OP_NOT); - } + } // fall through case 209: break; - case 25: + case 25: { if (isEmptyConditionalCommand()) { setEmptyConditionalCommand(false); backToPreviousState(); @@ -1605,160 +1606,160 @@ else if (zzAtEOF) { setEmptyConditionalCommand(false); return token(TokenType.WORD); } - } + } // fall through case 210: break; - case 26: + case 26: { return token(TokenType.COND_OP); - } + } // fall through case 211: break; - case 27: + case 27: { return token(TokenType.LEFT_PAREN); - } + } // fall through case 212: break; - case 28: + case 28: { return token(TokenType.ARITH_BASE_CHAR); - } + } // fall through case 213: break; - case 29: + case 29: { return token(TokenType.ARITH_NEGATE); - } + } // fall through case 214: break; - case 30: + case 30: { return token(TokenType.ARITH_BITWISE_XOR); - } + } // fall through case 215: break; - case 31: + case 31: { return token(TokenType.RIGHT_SQUARE); - } + } // fall through case 216: break; - case 32: + case 32: { return token(TokenType.ARITH_NUMBER); - } + } // fall through case 217: break; - case 33: + case 33: { return token(TokenType.ARITH_BITWISE_AND); - } + } // fall through case 218: break; - case 34: + case 34: { return token(TokenType.ARITH_MULT); - } + } // fall through case 219: break; - case 35: + case 35: { return token(TokenType.ARITH_PLUS); - } + } // fall through case 220: break; - case 36: + case 36: { decOpenParenthesisCount(); return token(TokenType.RIGHT_PAREN); - } + } // fall through case 221: break; - case 37: + case 37: { return token(TokenType.ARITH_MINUS); - } + } // fall through case 222: break; - case 38: + case 38: { return token(TokenType.LEFT_SQUARE); - } + } // fall through case 223: break; - case 39: + case 39: { incOpenParenthesisCount(); return token(TokenType.LEFT_PAREN); - } + } // fall through case 224: break; - case 40: + case 40: { return token(TokenType.COMMA); - } + } // fall through case 225: break; - case 41: + case 41: { return token(TokenType.ARITH_BITWISE_NEGATE); - } + } // fall through case 226: break; - case 42: + case 42: { return token(TokenType.ARITH_GT); - } + } // fall through case 227: break; - case 43: + case 43: { return token(TokenType.ARITH_LT); - } + } // fall through case 228: break; - case 44: + case 44: { return token(TokenType.ARITH_DIV); - } + } // fall through case 229: break; - case 45: + case 45: { return token(TokenType.ARITH_MOD); - } + } // fall through case 230: break; - case 46: + case 46: { return token(TokenType.ARITH_QMARK); - } + } // fall through case 231: break; - case 47: + case 47: { return token(TokenType.ARITH_COLON); - } + } // fall through case 232: break; - case 48: + case 48: { backToPreviousState(); return token(TokenType.EXPR_ARITH_SQUARE_END); - } + } // fall through case 233: break; - case 49: + case 49: { return token(TokenType.EXPR_ARITH_SQUARE); - } + } // fall through case 234: break; - case 50: + case 50: { backToPreviousState(); return token(TokenType.RIGHT_SQUARE); - } + } // fall through case 235: break; - case 51: + case 51: { backToPreviousState(); return token(TokenType.RIGHT_PAREN); - } + } // fall through case 236: break; - case 52: + case 52: { backToPreviousState(); if (stringParsingState().isInSubshell()) stringParsingState().leaveSubshell(); return token(TokenType.RIGHT_PAREN); - } + } // fall through case 237: break; - case 53: + case 53: { backToPreviousState(); goToState(S_ARITH_ARRAY_MODE); return token(TokenType.LEFT_SQUARE); - } + } // fall through case 238: break; - case 54: + case 54: { goToState(S_ARITH_ARRAY_MODE); return token(TokenType.LEFT_SQUARE); - } + } // fall through case 239: break; - case 55: + case 55: { return token(TokenType.STRING_DATA); - } + } // fall through case 240: break; - case 56: + case 56: { if (!stringParsingState().isInSubstring() && stringParsingState().isSubstringAllowed()) { stringParsingState().enterString(); goToState(X_STRINGMODE); @@ -1768,10 +1769,10 @@ else if (zzAtEOF) { stringParsingState().leaveString(); backToPreviousState(); return token(TokenType.STRING_END); - } + } // fall through case 241: break; - case 57: + case 57: { if (yystate() == S_BACKQUOTE) { backToPreviousState(); } @@ -1779,142 +1780,142 @@ else if (zzAtEOF) { goToState(S_BACKQUOTE); } return token(TokenType.BACKQUOTE); - } + } // fall through case 242: break; - case 58: + case 58: { setParamExpansionHash(isParamExpansionWord()); return token(TokenType.PARAM_EXPANSION_OP_HASH); - } + } // fall through case 243: break; - case 59: + case 59: { return token(TokenType.PARAM_EXPANSION_OP_EXCL); - } + } // fall through case 244: break; - case 60: + case 60: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_UPPERCASE_FIRST); - } + } // fall through case 245: break; - case 61: + case 61: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_DOT); - } + } // fall through case 246: break; - case 62: + case 62: { setParamExpansionWord(true); return token(TokenType.WORD); - } + } // fall through case 247: break; - case 63: + case 63: { return token(TokenType.PARAM_EXPANSION_OP_STAR); - } + } // fall through case 248: break; - case 64: + case 64: { return token(TokenType.PARAM_EXPANSION_OP_AT); - } + } // fall through case 249: break; - case 65: + case 65: { return token(TokenType.PARAM_EXPANSION_OP_PLUS); - } + } // fall through case 250: break; - case 66: + case 66: { return token(TokenType.PARAM_EXPANSION_OP_MINUS); - } + } // fall through case 251: break; - case 67: + case 67: { if (!isParamExpansionOther() && (!isParamExpansionWord() || !isParamExpansionHash())) { // If we expect an array reference scan the next tokens as arithmetic expression goToState(S_ARITH_ARRAY_MODE); } return token(TokenType.LEFT_SQUARE); - } + } // fall through case 252: break; - case 68: + case 68: { return token(TokenType.PARAM_EXPANSION_OP_EQ); - } + } // fall through case 253: break; - case 69: + case 69: { setParamExpansionWord(false); setParamExpansionHash(false); setParamExpansionOther(false); return token(TokenType.LEFT_CURLY); - } + } // fall through case 254: break; - case 70: + case 70: { setParamExpansionWord(false); setParamExpansionHash(false); setParamExpansionOther(false); backToPreviousState(); closeHereStringIfAvailable(); return token(TokenType.RIGHT_CURLY); - } + } // fall through case 255: break; - case 71: + case 71: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_LOWERCASE_FIRST); - } + } // fall through case 256: break; - case 72: + case 72: { goToState(S_PARAM_EXPANSION_PATTERN); return token(TokenType.PARAM_EXPANSION_OP_SLASH); - } + } // fall through case 257: break; - case 73: + case 73: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_PERCENT); - } + } // fall through case 258: break; - case 74: + case 74: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_QMARK); - } + } // fall through case 259: break; - case 75: + case 75: { return token(TokenType.PARAM_EXPANSION_OP_COLON); - } + } // fall through case 260: break; - case 76: + case 76: { backToPreviousState(); return token(TokenType.PARAM_EXPANSION_PATTERN); - } + } // fall through case 261: break; - case 77: + case 77: { yypushback(1); backToPreviousState(); - } + } // fall through case 262: break; - case 78: + case 78: { backToPreviousState(); goToState(S_PARAM_EXPANSION_REPLACEMENT); return token(TokenType.PARAM_EXPANSION_OP_SLASH); - } + } // fall through case 263: break; - case 79: + case 79: { backToPreviousState(); return token(TokenType.WORD); - } + } // fall through case 264: break; - case 80: + case 80: { heredocState().pushMarker(yytext(), yystate() == X_HEREDOC_MARKER_IGNORE_TABS); backToPreviousState(); return token(TokenType.HEREDOC_MARKER_START); - } + } // fall through case 265: break; - case 81: + case 81: { return token(TokenType.LINE_FEED); - } + } // fall through case 266: break; - case 82: + case 82: { //support end marker followed by a backtick if nested in a backtick command CharSequence markerText = yytext(); boolean dropLastChar = false; @@ -1937,23 +1938,23 @@ else if (zzAtEOF) { } return token(TokenType.HEREDOC_LINE); - } + } // fall through case 267: break; - case 83: + case 83: { if (!heredocState().isEmpty()) { return token(TokenType.HEREDOC_LINE); } return token(TokenType.LINE_FEED); - } + } // fall through case 268: break; - case 84: + case 84: { return token(TokenType.HEREDOC_LINE); - } + } // fall through case 269: break; - case 85: + case 85: { if (heredocState().isNextMarker(yytext())) { boolean ignoreTabs = heredocState().isIgnoringTabs(); @@ -1964,233 +1965,233 @@ else if (zzAtEOF) { } return token(TokenType.HEREDOC_LINE); - } + } // fall through case 270: break; - case 86: + case 86: { if (isInHereStringContent()) { leaveHereStringContent(); backToPreviousState(); } return token(TokenType.WHITESPACE); - } + } // fall through case 271: break; - case 87: + case 87: { if (!isInHereStringContent()) enterHereStringContent(); return token(TokenType.WORD); - } + } // fall through case 272: break; - case 88: + case 88: { return token(TokenType.LINE_CONTINUATION); - } + } // fall through case 273: break; - case 89: + case 89: { return token(TokenType.SHEBANG); - } + } // fall through case 274: break; - case 90: + case 90: { return token(TokenType.VARIABLE); - } + } // fall through case 275: break; - case 91: + case 91: { yypushback(1); goToState(S_ARITH_SQUARE_MODE); return token(TokenType.DOLLAR); - } + } // fall through case 276: break; - case 92: + case 92: { if (yystate() == X_HEREDOC && !heredocState().isExpectingEvaluatingHeredoc()) return token(TokenType.HEREDOC_LINE); goToState(S_PARAM_EXPANSION); yypushback(1); return token(TokenType.DOLLAR); - } + } // fall through case 277: break; - case 93: + case 93: { if (yystate() == X_HERE_STRING && !isInHereStringContent()) enterHereStringContent(); return token(TokenType.STRING2); - } + } // fall through case 278: break; - case 94: + case 94: // lookahead expression with fixed lookahead length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzMarkedPos, -1); { return token(TokenType.ASSIGNMENT_WORD); - } + } // fall through case 279: break; - case 95: + case 95: { return token(TokenType.FILEDESCRIPTOR); - } + } // fall through case 280: break; - case 96: + case 96: { closeHereStringIfAvailable(); return token(TokenType.AND_AND); - } + } // fall through case 281: break; - case 97: + case 97: { if (isBash4()) { return token(TokenType.REDIRECT_AMP_GREATER); } else { yypushback(1); return token(TokenType.AMP); } - } + } // fall through case 282: break; - case 98: + case 98: { return token(TokenType.ADD_EQ); - } + } // fall through case 283: break; - case 99: + case 99: { if (isBash4()) { return token(TokenType.PIPE_AMP); } else { yypushback(1); return token(TokenType.PIPE); } - } + } // fall through case 284: break; - case 100: + case 100: { closeHereStringIfAvailable(); return token(TokenType.OR_OR); - } + } // fall through case 285: break; - case 101: + case 101: { goToState(S_TEST); setEmptyConditionalCommand(false); return token(TokenType.EXPR_CONDITIONAL); - } + } // fall through case 286: break; - case 102: + case 102: { return token(TokenType.IF_KEYWORD); - } + } // fall through case 287: break; - case 103: + case 103: { if (yystate() == S_DOLLAR_PREFIXED) backToPreviousState(); goToState(S_ARITH); return token(TokenType.EXPR_ARITH); - } + } // fall through case 288: break; - case 104: + case 104: { return token(TokenType.DO_KEYWORD); - } + } // fall through case 289: break; - case 105: + case 105: { return token(TokenType.FI_KEYWORD); - } + } // fall through case 290: break; - case 106: + case 106: { return token(TokenType.REDIRECT_GREATER_BAR); - } + } // fall through case 291: break; - case 107: + case 107: { return token(TokenType.SHIFT_RIGHT); - } + } // fall through case 292: break; - case 108: + case 108: { return token(TokenType.REDIRECT_LESS_GREATER); - } + } // fall through case 293: break; - case 109: + case 109: { goToState(X_HEREDOC_MARKER); return token(TokenType.HEREDOC_MARKER_TAG); - } + } // fall through case 294: break; - case 110: + case 110: { backToPreviousState(); setEmptyConditionalCommand(false); return token(TokenType.EXPR_CONDITIONAL_END); - } + } // fall through case 295: break; - case 111: + case 111: { return token(TokenType.COND_OP_EQ_EQ); - } + } // fall through case 296: break; - case 112: + case 112: { return token(TokenType.COND_OP_REGEX); - } + } // fall through case 297: break; - case 113: + case 113: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 1); { goToState(S_DOLLAR_PREFIXED); return token(TokenType.DOLLAR); - } + } // fall through case 298: break; - case 114: + case 114: { return token(TokenType.AND_AND); - } + } // fall through case 299: break; - case 115: + case 115: { return token(TokenType.OR_OR); - } + } // fall through case 300: break; - case 116: + case 116: { return token(TokenType.ARITH_NE); - } + } // fall through case 301: break; - case 117: + case 117: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 1); { if (yystate() == X_HEREDOC && !heredocState().isExpectingEvaluatingHeredoc()) return token(TokenType.HEREDOC_LINE); goToState(S_DOLLAR_PREFIXED); return token(TokenType.DOLLAR); - } + } // fall through case 302: break; - case 118: + case 118: { return token(TokenType.ARITH_ASS_BIT_XOR); - } + } // fall through case 303: break; - case 119: + case 119: // lookahead expression with fixed lookahead length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzMarkedPos, -1); { goToState(S_ARRAY); return token(TokenType.ASSIGNMENT_WORD); - } + } // fall through case 304: break; - case 120: + case 120: { return token(TokenType.ARITH_ASS_BIT_AND); - } + } // fall through case 305: break; - case 121: + case 121: { return token(TokenType.ARITH_EXPONENT); - } + } // fall through case 306: break; - case 122: + case 122: { return token(TokenType.ARITH_ASS_MUL); - } + } // fall through case 307: break; - case 123: + case 123: { return token(TokenType.ARITH_PLUS_PLUS); - } + } // fall through case 308: break; - case 124: + case 124: { return token(TokenType.ARITH_ASS_PLUS); - } + } // fall through case 309: break; - case 125: + case 125: { return token(TokenType.ARITH_OCTAL_NUMBER); - } + } // fall through case 310: break; - case 126: + case 126: { if (openParenthesisCount() > 0) { decOpenParenthesisCount(); yypushback(1); @@ -2201,60 +2202,60 @@ else if (zzAtEOF) { return token(TokenType.EXPR_ARITH_END); } - } + } // fall through case 311: break; - case 127: + case 127: { return token(TokenType.ARITH_MINUS_MINUS); - } + } // fall through case 312: break; - case 128: + case 128: { return token(TokenType.ARITH_ASS_MINUS); - } + } // fall through case 313: break; - case 129: + case 129: { return token(TokenType.ARITH_ASS_BIT_OR); - } + } // fall through case 314: break; - case 130: + case 130: { return token(TokenType.ARITH_EQ); - } + } // fall through case 315: break; - case 131: + case 131: { return token(TokenType.ARITH_GE); - } + } // fall through case 316: break; - case 132: + case 132: { return token(TokenType.ARITH_SHIFT_RIGHT); - } + } // fall through case 317: break; - case 133: + case 133: { return token(TokenType.ARITH_LE); - } + } // fall through case 318: break; - case 134: + case 134: { return token(TokenType.ARITH_SHIFT_LEFT); - } + } // fall through case 319: break; - case 135: + case 135: { return token(TokenType.ARITH_ASS_DIV); - } + } // fall through case 320: break; - case 136: + case 136: { return token(TokenType.ARITH_ASS_MOD); - } + } // fall through case 321: break; - case 137: + case 137: { goToState(S_CASE_PATTERN); if (isBash4()) { return token(TokenType.CASE_END); @@ -2263,76 +2264,76 @@ else if (zzAtEOF) { yypushback(1); return token(TokenType.SEMI); } - } + } // fall through case 322: break; - case 138: + case 138: { goToState(S_CASE_PATTERN); return token(TokenType.CASE_END); - } + } // fall through case 323: break; - case 139: + case 139: { if (!isInCaseBody()) { setInCaseBody(true); goToState(S_CASE_PATTERN); }; return token(TokenType.WORD); - } + } // fall through case 324: break; - case 140: + case 140: { setParamExpansionHash(isParamExpansionWord()); return token(TokenType.PARAM_EXPANSION_OP_HASH_HASH); - } + } // fall through case 325: break; - case 141: + case 141: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_UPPERCASE_ALL); - } + } // fall through case 326: break; - case 142: + case 142: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 1); { return token(TokenType.LEFT_SQUARE); - } + } // fall through case 327: break; - case 143: + case 143: { setParamExpansionOther(true); return token(TokenType.PARAM_EXPANSION_OP_LOWERCASE_ALL); - } + } // fall through case 328: break; - case 144: + case 144: { goToState(S_PARAM_EXPANSION_PATTERN); return token(TokenType.PARAM_EXPANSION_OP_SLASH_SLASH); - } + } // fall through case 329: break; - case 145: + case 145: { return token(TokenType.PARAM_EXPANSION_OP_COLON_PLUS); - } + } // fall through case 330: break; - case 146: + case 146: { return token(TokenType.PARAM_EXPANSION_OP_COLON_MINUS); - } + } // fall through case 331: break; - case 147: + case 147: { return token(TokenType.PARAM_EXPANSION_OP_COLON_EQ); - } + } // fall through case 332: break; - case 148: + case 148: { return token(TokenType.PARAM_EXPANSION_OP_COLON_QMARK); - } + } // fall through case 333: break; - case 149: + case 149: // lookahead expression with fixed lookahead length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzMarkedPos, -1); { backToPreviousState(); goToState(S_PARAM_EXPANSION_DELIMITER); return token(TokenType.PARAM_EXPANSION_PATTERN); - } + } // fall through case 334: break; - case 150: + case 150: { if (heredocState().isNextMarker(yytext())) { boolean ignoreTabs = heredocState().isIgnoringTabs(); @@ -2345,146 +2346,146 @@ else if (zzAtEOF) { return token(yystate() == X_HEREDOC && heredocState().isExpectingEvaluatingHeredoc() && !"$".equals(yytext().toString()) ? TokenType.VARIABLE : TokenType.HEREDOC_LINE); - } + } // fall through case 335: break; - case 151: + case 151: { if (!isInHereStringContent()) enterHereStringContent(); return token(TokenType.VARIABLE); - } + } // fall through case 336: break; - case 152: + case 152: // lookahead expression with fixed lookahead length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzMarkedPos, -2); { return token(TokenType.ASSIGNMENT_WORD); - } + } // fall through case 337: break; - case 153: + case 153: // lookahead expression with fixed lookahead length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzMarkedPos, -2); { goToState(S_ASSIGNMENT_LIST); return token(TokenType.ASSIGNMENT_WORD); - } + } // fall through case 338: break; - case 154: + case 154: { if (isBash4()) { return token(TokenType.REDIRECT_AMP_GREATER_GREATER); } else { yypushback(2); return token(TokenType.AMP); } - } + } // fall through case 339: break; - case 155: + case 155: { goToState(S_TEST_COMMAND); return token(TokenType.BRACKET_KEYWORD); - } + } // fall through case 340: break; - case 156: + case 156: { yypushback(1); goToState(S_TEST); setEmptyConditionalCommand(true); return token(TokenType.EXPR_CONDITIONAL); - } + } // fall through case 341: break; - case 157: + case 157: { if (yystate() == S_DOLLAR_PREFIXED) backToPreviousState(); yypushback(2); goToState(S_SUBSHELL); return token(TokenType.LEFT_PAREN); - } + } // fall through case 342: break; - case 158: + case 158: { return token(TokenType.LET_KEYWORD); - } + } // fall through case 343: break; - case 159: + case 159: { return token(TokenType.FOR_KEYWORD); - } + } // fall through case 344: break; - case 160: + case 160: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 2); { return token(TokenType.REDIRECT_GREATER_AMP); - } + } // fall through case 345: break; - case 161: + case 161: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 2); { return token(TokenType.REDIRECT_LESS_AMP); - } + } // fall through case 346: break; - case 162: + case 162: { goToState(X_HEREDOC_MARKER_IGNORE_TABS); return token(TokenType.HEREDOC_MARKER_TAG); - } + } // fall through case 347: break; - case 163: + case 163: { goToState(X_HERE_STRING); return token(TokenType.REDIRECT_HERE_STRING); - } + } // fall through case 348: break; - case 164: + case 164: { backToPreviousState(); return token(TokenType.BRACKET_KEYWORD_END); - } + } // fall through case 349: break; - case 165: + case 165: { return token(TokenType.ARITH_HEX_NUMBER); - } + } // fall through case 350: break; - case 166: + case 166: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 2); { yypushback(1); return token(TokenType.ARITH_MINUS); - } + } // fall through case 351: break; - case 167: + case 167: { return token(TokenType.ARITH_ASS_SHIFT_RIGHT); - } + } // fall through case 352: break; - case 168: + case 168: { return token(TokenType.ARITH_ASS_SHIFT_LEFT); - } + } // fall through case 353: break; - case 169: + case 169: // lookahead expression with fixed base length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzStartRead, 1); { backToPreviousState(); goToState(S_ASSIGNMENT_LIST); return token(TokenType.RIGHT_SQUARE); - } + } // fall through case 354: break; - case 170: + case 170: { goToState(S_CASE_PATTERN); if (!isBash4()) { yypushback(1); } return token(TokenType.CASE_END); - } + } // fall through case 355: break; - case 171: + case 171: // lookahead expression with fixed lookahead length zzMarkedPos = Character.offsetByCodePoints (zzBufferL, zzStartRead, zzEndRead - zzStartRead, zzMarkedPos, -3); { goToState(S_ASSIGNMENT_LIST); return token(TokenType.ASSIGNMENT_WORD); - } + } // fall through case 356: break; - case 172: + case 172: // general lookahead, find correct zzMarkedPos { int zzFState = 21; int zzFPos = zzStartRead; @@ -2496,7 +2497,7 @@ else if (zzAtEOF) { zzFPos += Character.charCount(zzInput); zzFState = zzTransL[ zzRowMapL[zzFState] + zzCMapL[zzInput] ]; } - if (zzFState != -1) { zzFinL[zzFPos++] = ((zzAttrL[zzFState] & 1) == 1); } + if (zzFState != -1) { zzFinL[zzFPos++] = ((zzAttrL[zzFState] & 1) == 1); } while (zzFPos <= zzMarkedPos) { zzFinL[zzFPos++] = false; } @@ -2511,72 +2512,72 @@ else if (zzAtEOF) { zzMarkedPos = zzFPos; } { goToState(S_ARRAY); return token(TokenType.ASSIGNMENT_WORD); - } + } // fall through case 357: break; - case 173: + case 173: { return token(TokenType.TIME_KEYWORD); - } + } // fall through case 358: break; - case 174: + case 174: { return token(TokenType.TRAP_KEYWORD); - } + } // fall through case 359: break; - case 175: + case 175: { return token(TokenType.THEN_KEYWORD); - } + } // fall through case 360: break; - case 176: + case 176: { return token(TokenType.ELIF_KEYWORD); - } + } // fall through case 361: break; - case 177: + case 177: { return token(TokenType.ELSE_KEYWORD); - } + } // fall through case 362: break; - case 178: + case 178: { setInCaseBody(false); goToState(S_CASE); return token(TokenType.CASE_KEYWORD); - } + } // fall through case 363: break; - case 179: + case 179: { return token(TokenType.DONE_KEYWORD); - } + } // fall through case 364: break; - case 180: + case 180: { backToPreviousState(); return token(TokenType.ESAC_KEYWORD); - } + } // fall through case 365: break; - case 181: + case 181: { backToPreviousState(); yypushback(yylength()); - } + } // fall through case 366: break; - case 182: + case 182: { return token(TokenType.UNTIL_KEYWORD); - } + } // fall through case 367: break; - case 183: + case 183: { return token(TokenType.WHILE_KEYWORD); - } + } // fall through case 368: break; - case 184: + case 184: { return token(TokenType.SELECT_KEYWORD); - } + } // fall through case 369: break; - case 185: + case 185: { return token(TokenType.FUNCTION_KEYWORD); - } + } // fall through case 370: break; default: diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingState.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingState.java index af8a3f7..3725f30 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingState.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingState.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java index ebc1924..3b187e0 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/ShellLexer.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/ShellLexer.java index 352915b..0b84ec1 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/ShellLexer.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/ShellLexer.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstate.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstate.java index 4aee3ce..248778f 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstate.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstate.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java index 784c54b..786e4b3 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenType.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenType.java index dd5da69..405aec9 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenType.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenType.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountData.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountData.java index 967bebe..e658913 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountData.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountData.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java index 58e3572..2645518 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java index bf96bea..3dab46b 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java index 42d366e..ba0dbda 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java index d647a27..0996d2f 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java index 6cddb32..cf89a82 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java index b57c4f7..ca125d4 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java index d549877..f9351ed 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/Utils.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/Utils.java index 22e8888..880685e 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/Utils.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/Utils.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java index 33c3534..bf37351 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java index c9674b9..a76e632 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java index b6a12b5..b5cad09 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocationTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocationTest.java index 030854b..cf92f7f 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocationTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellLocationTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguageTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguageTest.java index ed346b2..4474cd7 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguageTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguageTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfileTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfileTest.java index ca5c6bc..ca03a17 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfileTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellQualityProfileTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerTest.java index 3d18b77..1c829f7 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/BashLexerTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingStateTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingStateTest.java index f399afb..e20ad67 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingStateTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocLexingStateTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImplTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImplTest.java index 2a46301..8322378 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImplTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImplTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstateTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstateTest.java index 66b637b..0c59a6e 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstateTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/StringLexingstateTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenTest.java index 29152a5..da0fbbe 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/TokenTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java index d561668..4c1bf02 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java index 810941a..dd563d4 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java index 1f790f2..0ae19c3 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java index a43a904..bf2e35a 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java index 3188c76..93e96ae 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettingsTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettingsTest.java index 0aa0cb5..0c946ac 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettingsTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettingsTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java index 1c368d2..3f9677d 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2018-2021, Sylvain Baudoin +/* + * Copyright (c) 2018-2025, Sylvain Baudoin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 2e8fe01a30e676451ce20c1d6edfb52e95d4303d Mon Sep 17 00:00:00 2001 From: didierC Date: Sat, 15 Feb 2025 09:52:32 +0100 Subject: [PATCH 2/8] update commons-lang dependencies to commons-lang3 --- pom.xml | 6 +++--- .../sonar/plugins/shellcheck/languages/ShellLanguage.java | 3 ++- .../sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java | 2 +- .../sonar/plugins/shellcheck/measures/LineCountParser.java | 4 +++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 549aebf..7b0a51c 100644 --- a/pom.xml +++ b/pom.xml @@ -69,9 +69,9 @@ 1.6.0.219 - commons-lang - commons-lang - 2.6 + org.apache.commons + commons-lang3 + 3.17.0 org.json diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java index 3fb3511..4933438 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/languages/ShellLanguage.java @@ -16,7 +16,8 @@ package com.github.sbaudoin.sonar.plugins.shellcheck.languages; import com.github.sbaudoin.sonar.plugins.shellcheck.settings.ShellCheckSettings; -import org.apache.commons.lang.StringUtils; + +import org.apache.commons.lang3.StringUtils; import org.sonar.api.config.Configuration; import org.sonar.api.resources.AbstractLanguage; diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java index 3b187e0..ec0c999 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/HeredocSharedImpl.java @@ -15,7 +15,7 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.lexer; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; /** * Shared code for the Heredoc handling. diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java index 2645518..2fd3b82 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountParser.java @@ -18,7 +18,7 @@ import com.github.sbaudoin.sonar.plugins.shellcheck.highlighting.ShellLocation; import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.BashLexer; import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.TokenType; -import org.apache.commons.lang.StringUtils; + import java.io.BufferedReader; import java.io.IOException; @@ -28,6 +28,8 @@ import java.util.List; import java.util.Set; +import org.apache.commons.lang3.StringUtils; + /** * Counting lines, comment lines and blank lines in Shell scripts */ From 5747bff48209584511a12f4ae8db398f1d645f7c Mon Sep 17 00:00:00 2001 From: didierC Date: Sat, 15 Feb 2025 10:32:08 +0100 Subject: [PATCH 3/8] migrate from powermock to mockito No more compatible with SonarQube 8.0 --- pom.xml | 16 ++-- .../plugins/shellcheck/ShellCheckPlugin.java | 13 +-- .../shellcheck/ShellCheckPluginTest.java | 47 +---------- .../highlighting/ShellHighlightingTest.java | 80 ++++++++++--------- .../shellcheck/measures/LineCounterTest.java | 10 +-- .../rules/ShellCheckSensorNoRuleTest.java | 6 +- .../rules/ShellCheckSensorTest.java | 8 +- 7 files changed, 70 insertions(+), 110 deletions(-) diff --git a/pom.xml b/pom.xml index 7b0a51c..093d9ea 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,6 @@ https://oss.sonatype.org/content/repositories/snapshots 7.1 - 1.7.1 0.8.6 **/lexer/BashLexerBase.java @@ -91,15 +90,9 @@ test - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - org.powermock - powermock-api-mockito - ${powermock.version} + org.mockito + mockito-core + 5.15.2 test @@ -230,8 +223,11 @@ org.apache.maven.plugins maven-surefire-plugin + --add-opens java.base/java.util=ALL-UNNAMED --add-opens + java.base/java.lang=ALL-UNNAMED 1 false + **/*Test.java diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java index c7004a1..c4e4edd 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java @@ -33,16 +33,9 @@ public class ShellCheckPlugin implements Plugin { @Override public void define(Context context) { Optional addLanguage = Optional.empty(); - try { - if (System.getenv().get(ADD_SHELL_LANGUAGE_ENV_VAR) != null) { - addLanguage = Optional.of(Boolean.parseBoolean(System.getenv().get(ADD_SHELL_LANGUAGE_ENV_VAR))); - } - } catch (AccessControlException exc) { - // Do nothing on purpose: in SQ 8.0, we do not have access to system env variables - // and we deal with that with a new SQ configuration property read below - } - // If no env variable found, read from SQ conf - if (!addLanguage.isPresent()) { + if (System.getenv().get(ADD_SHELL_LANGUAGE_ENV_VAR) != null) { + addLanguage = Optional.of(Boolean.parseBoolean(System.getenv().get(ADD_SHELL_LANGUAGE_ENV_VAR))); + } else { addLanguage = context.getBootConfiguration().getBoolean(ADD_SHELL_LANGUAGE_CONF_PROP); } // Install language unless instructed otherwise diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java index f9351ed..fea98b6 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java @@ -15,30 +15,17 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.EnvironmentVariables; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import org.sonar.api.Plugin; -import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.internal.PluginContextImpl; -import org.sonar.api.internal.SonarRuntimeImpl; -import org.sonar.api.utils.Version; - -import java.security.AccessControlException; -import static org.junit.Assert.assertEquals; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ShellCheckPlugin.class}) -@PowerMockIgnore("jdk.internal.reflect.*") public class ShellCheckPluginTest { @Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); @@ -99,32 +86,6 @@ public void testExtensionCounts7() { assertEquals(6, context.getExtensions().size()); } - // Special test for SQ 8.0 - // @see https://community.sonarsource.com/t/cannot-access-environment-variables-from-a-plugin-in-sonarqube-8/15743/6 - @Test - public void testExtensionCounts8() { - mockStatic(System.class); - when(System.getenv()).thenThrow(new AccessControlException("Forbidden access")); - - MapSettings settings = new MapSettings().setProperty(ShellCheckPlugin.ADD_SHELL_LANGUAGE_CONF_PROP, "false"); - Plugin.Context context = getContext(settings); - new ShellCheckPlugin().define(context); - assertEquals(6, context.getExtensions().size()); - } - - // Special test for SQ 8.0 - // @see https://community.sonarsource.com/t/cannot-access-environment-variables-from-a-plugin-in-sonarqube-8/15743/6 - @Test - public void testExtensionCounts9() { - mockStatic(System.class); - when(System.getenv()).thenThrow(new AccessControlException("Forbidden access")); - - MapSettings settings = new MapSettings().setProperty(ShellCheckPlugin.ADD_SHELL_LANGUAGE_CONF_PROP, "true"); - Plugin.Context context = getContext(settings); - new ShellCheckPlugin().define(context); - assertEquals(7, context.getExtensions().size()); - } - @Test public void testExtensionCounts10() { environmentVariables.set(ShellCheckPlugin.ADD_SHELL_LANGUAGE_ENV_VAR, "True"); @@ -163,7 +124,7 @@ public void testExtensionCounts13() { private Plugin.Context getContext(MapSettings settings) { - SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(7, 1), SonarQubeSide.SERVER); + SonarRuntime runtime = mock(SonarRuntime.class); PluginContextImpl.Builder contextBuilder = new PluginContextImpl.Builder(); contextBuilder.setSonarRuntime(runtime); if (settings != null) { diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java index b5cad09..61d4174 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java @@ -15,30 +15,28 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.highlighting; -import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; -import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.BashLexer; -import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.Token; -import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.TokenType; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; import org.sonar.api.batch.sensor.highlighting.TypeOfText; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; -import java.io.IOException; -import java.util.List; - -import static org.junit.Assert.*; -import static org.powermock.api.mockito.PowerMockito.*; +import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; +import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.BashLexer; +import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.Token; +import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.TokenType; -@RunWith(PowerMockRunner.class) -@PrepareForTest(ShellHighlighting.class) -@PowerMockIgnore("jdk.internal.reflect.*") public class ShellHighlightingTest { @Rule public LogTester logTester = new LogTester(); @@ -55,22 +53,31 @@ public void testConstructors() throws Exception { ShellHighlighting sh = new ShellHighlighting(Utils.getInputFile("test1.sh").contents()); assertEquals(5, sh.getHighlightingData().size()); - whenNew(BashLexer.class).withAnyArguments().thenThrow(new IOException("Boom!")); - logTester.clear(); - new ShellHighlighting("foo"); - assertTrue(logTester.logs(LoggerLevel.WARN).size() > 0); - assertEquals("Could not scan Shell script and highlight code", logTester.logs(LoggerLevel.WARN).get(0)); + try (MockedConstruction mockBashLexer = Mockito.mockConstruction(BashLexer.class, + (mock, context) -> { + when(mock.scan()).thenThrow(new IOException("Boom!")); + })) { + logTester.clear(); + new ShellHighlighting("foo"); + assertTrue(logTester.logs(LoggerLevel.WARN).size() > 0); + assertEquals("Could not scan Shell script and highlight code", logTester.logs(LoggerLevel.WARN).get(0)); + + } } @Test public void test0LengthToken() throws Exception { - ShellHighlighting sh = new ShellHighlighting(""); - Token token = new Token(TokenType.HEREDOC_LINE, 14, 0, 2, 5); - try { - Whitebox.invokeMethod(sh, "addHighlighting", token, null); - fail("Expected exception not thrown"); - } catch (IllegalArgumentException e) { - assertEquals("Cannot highlight an empty token", e.getMessage()); + Token badToken = new Token(TokenType.HEREDOC_LINE, 14, 0, 2, 5); + try (MockedConstruction mockBashLexer = Mockito.mockConstruction(BashLexer.class, + (mock, context) -> { + when(mock.scan()).thenReturn(Arrays.asList(badToken)); + })) { + try { + new ShellHighlighting(""); + fail("Expected exception not thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Cannot highlight an empty token", e.getMessage()); + } } } @@ -101,13 +108,15 @@ public void testHighlightAllTokenTypes() throws IOException { assertHighlightingData(sh.getHighlightingData().get(9), 8, 7, 8, 11, TypeOfText.CONSTANT); assertHighlightingData(sh.getHighlightingData().get(10), 8, 11, 8, 12, TypeOfText.STRING); - // WORD + HEREDOC_MARKER_TAG + HEREDOC_MARKER_START + HEREDOC_CONTENT + HEREDOC_MARKER_END + // WORD + HEREDOC_MARKER_TAG + HEREDOC_MARKER_START + HEREDOC_CONTENT + + // HEREDOC_MARKER_END assertHighlightingData(sh.getHighlightingData().get(11), 10, 6, 10, 8, TypeOfText.STRUCTURED_COMMENT); assertHighlightingData(sh.getHighlightingData().get(12), 10, 9, 10, 12, TypeOfText.STRUCTURED_COMMENT); assertHighlightingData(sh.getHighlightingData().get(13), 11, 1, 13, 1, TypeOfText.STRUCTURED_COMMENT); assertHighlightingData(sh.getHighlightingData().get(14), 13, 1, 13, 4, TypeOfText.STRUCTURED_COMMENT); - // IF_KEYWORD + EXPR_CONDITIONAL + VARIABLE + COND_OP + WORD + EXPR_CONDITIONAL_END + // IF_KEYWORD + EXPR_CONDITIONAL + VARIABLE + COND_OP + WORD + + // EXPR_CONDITIONAL_END assertHighlightingData(sh.getHighlightingData().get(15), 15, 1, 15, 3, TypeOfText.KEYWORD); assertHighlightingData(sh.getHighlightingData().get(16), 15, 6, 15, 8, TypeOfText.CONSTANT); assertHighlightingData(sh.getHighlightingData().get(17), 15, 9, 15, 12, TypeOfText.KEYWORD_LIGHT); @@ -140,7 +149,7 @@ public void testHighlightLineBreaks() { ShellHighlighting sh = new ShellHighlighting(script); List data = sh.getHighlightingData(); assertEquals(4, data.size()); - assertHighlightingData(data.get(0), 1, 1, (i < 2)?2:3, 1, TypeOfText.COMMENT); + assertHighlightingData(data.get(0), 1, 1, (i < 2) ? 2 : 3, 1, TypeOfText.COMMENT); assertHighlightingData(data.get(1), 3, 6, 3, 7, TypeOfText.STRING); assertHighlightingData(data.get(2), 3, 7, 3, 19, TypeOfText.STRING); assertHighlightingData(data.get(3), 3, 19, 3, 20, TypeOfText.STRING); @@ -148,8 +157,7 @@ public void testHighlightLineBreaks() { ShellHighlighting sh = new ShellHighlighting("#!/bin/bash\n" + "\n" + - "echo \"Hello\rworld!\"\n" - ); + "echo \"Hello\rworld!\"\n"); List data = sh.getHighlightingData(); assertEquals(4, data.size()); assertHighlightingData(data.get(0), 1, 1, 2, 1, TypeOfText.COMMENT); @@ -158,8 +166,8 @@ public void testHighlightLineBreaks() { assertHighlightingData(data.get(3), 4, 7, 4, 8, TypeOfText.STRING); } - - private void assertHighlightingData(HighlightingData hd, int startLine, int startColumnIndex, int endLine, int endColumnIndex, TypeOfText typeOfText) { + private void assertHighlightingData(HighlightingData hd, int startLine, int startColumnIndex, int endLine, + int endColumnIndex, TypeOfText typeOfText) { assertEquals(startLine, hd.getStartLine()); assertEquals(startColumnIndex, hd.getStartColumnIndex()); assertEquals(endLine, hd.getEndLine()); diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java index dd563d4..c667ea8 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java @@ -19,6 +19,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.mockito.Mockito; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.internal.SonarRuntimeImpl; @@ -35,10 +36,9 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.spy; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class LineCounterTest { private FileLinesContextFactory fileLinesContextFactory; @@ -67,7 +67,7 @@ public void testNormal() throws IOException { public void testIOException() throws IOException { SensorContextTester context = Utils.getSensorContext(); InputFile inputFile = Utils.getInputFile("test1.sh"); - InputFile spy = spy(inputFile); + InputFile spy = Mockito.spy(inputFile); when(spy.contents()).thenThrow(new IOException("Cannot read file")); LineCounter.analyse(context, fileLinesContextFactory, spy); diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java index bf2e35a..31e88d3 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java @@ -32,9 +32,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ShellCheckSensorNoRuleTest { @Rule diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java index 93e96ae..d346841 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java @@ -49,9 +49,11 @@ import static com.github.sbaudoin.sonar.plugins.shellcheck.Utils.issueExists; import static com.github.sbaudoin.sonar.plugins.shellcheck.Utils.setShellRights; import static org.junit.Assert.*; -import static org.mockito.Matchers.any; -import static org.powermock.api.mockito.PowerMockito.*; -import static org.powermock.api.mockito.PowerMockito.doThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; public class ShellCheckSensorTest { private static final String RULE_ID1 = "SC2037"; From 99672bc13ce956bf27e48bbf93b34415e33aeab1 Mon Sep 17 00:00:00 2001 From: didierC Date: Sat, 15 Feb 2025 12:18:19 +0100 Subject: [PATCH 4/8] Make jacoco works --- pom.xml | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 093d9ea..fff3cdb 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,6 @@ https://oss.sonatype.org/content/repositories/snapshots 7.1 - 0.8.6 **/lexer/BashLexerBase.java UTF-8 @@ -101,12 +100,6 @@ 2.0.1 test - - org.jacoco - org.jacoco.agent - ${jacoco.version} - test - com.github.stefanbirkner system-rules @@ -223,11 +216,8 @@ org.apache.maven.plugins maven-surefire-plugin - --add-opens java.base/java.util=ALL-UNNAMED --add-opens - java.base/java.lang=ALL-UNNAMED 1 false - **/*Test.java @@ -317,19 +307,40 @@ jacoco-maven-plugin - prepare-agent + pre-unit-test prepare-agent + + true + surefireArgLine + - default-report + post-unit-test test report + + + *.jar + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${surefireArgLine} + 1 + false + + **/*Test.java + + com.github.spotbugs From cecb7844fc333944fc048680789e80a23f8d2bd8 Mon Sep 17 00:00:00 2001 From: didierC Date: Sat, 15 Feb 2025 12:47:55 +0100 Subject: [PATCH 5/8] Migrate from com.github.stefanbirkner.system-rules to uk.org.webcompere.system-stubs-junit4 --- pom.xml | 6 +++--- .../sonar/plugins/shellcheck/ShellCheckPluginTest.java | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index fff3cdb..d4fef3d 100644 --- a/pom.xml +++ b/pom.xml @@ -101,9 +101,9 @@ test - com.github.stefanbirkner - system-rules - 1.19.0 + uk.org.webcompere + system-stubs-junit4 + 2.1.7 test diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java index fea98b6..6af7020 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java @@ -20,15 +20,16 @@ import org.junit.Rule; import org.junit.Test; -import org.junit.contrib.java.lang.system.EnvironmentVariables; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.internal.PluginContextImpl; +import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule; + public class ShellCheckPluginTest { @Rule - public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); + public final EnvironmentVariablesRule environmentVariables = new EnvironmentVariablesRule(); @Test From a35fe10b35582c248ad2a80a21f8eda5ba41531e Mon Sep 17 00:00:00 2001 From: didierC Date: Sat, 15 Feb 2025 20:05:50 +0100 Subject: [PATCH 6/8] Upgrade Sonar dependencies Corrects tests Change Logger Delete code for older SonarQube version https://sonarsource.atlassian.net/browse/SONAR-11064 --- pom.xml | 36 ++++-- .../plugins/shellcheck/ShellCheckPlugin.java | 8 +- .../shellcheck/checks/CheckRepository.java | 7 +- .../highlighting/ShellHighlighting.java | 15 +-- .../sonar/plugins/shellcheck/lexer/Token.java | 4 +- .../shellcheck/measures/LineCounter.java | 16 +-- .../rules/ShellCheckRulesDefinition.java | 15 ++- .../shellcheck/rules/ShellCheckSensor.java | 6 +- .../settings/ShellCheckSettings.java | 13 +- .../plugins/shellcheck/util/FileSystem.java | 14 ++- .../shellcheck/ShellCheckPluginTest.java | 2 +- .../checks/CheckRepositoryTest.java | 6 +- .../highlighting/HighlightingDataTest.java | 5 - .../highlighting/ShellHighlightingTest.java | 8 +- .../measures/LineCountDataTest.java | 14 +-- .../shellcheck/measures/LineCounterTest.java | 68 ++++------- .../rules/ShellCheckRulesDefinitionTest.java | 19 ++- .../rules/ShellCheckSensorNoRuleTest.java | 12 +- .../rules/ShellCheckSensorTest.java | 113 +++++++++--------- .../shellcheck/util/FileSystemTest.java | 27 +++-- 20 files changed, 205 insertions(+), 203 deletions(-) diff --git a/pom.xml b/pom.xml index d4fef3d..64229c4 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,9 @@ https://oss.sonatype.org/service/local/staging/deploy/maven2 https://oss.sonatype.org/content/repositories/snapshots - 7.1 + 11.0.0.2664 + 25.2.0.102705 + **/lexer/BashLexerBase.java UTF-8 @@ -56,15 +58,15 @@ - org.sonarsource.sonarqube + org.sonarsource.api.plugin sonar-plugin-api - ${sonar.apiVersion} + ${sonar.plugin.api.version} provided org.sonarsource.analyzer-commons sonar-analyzer-commons - 1.6.0.219 + 2.16.0.3141 org.apache.commons @@ -74,13 +76,7 @@ org.json json - 20180813 - - - org.sonarsource.sonarqube - sonar-testing-harness - ${sonar.apiVersion} - test + 20250107 junit @@ -106,6 +102,24 @@ 2.1.7 test + + org.sonarsource.sonarqube + sonar-testing-harness + ${sonar.version} + test + + + org.sonarsource.sonarqube + sonar-plugin-api-impl + ${sonar.version} + test + + + org.sonarsource.api.plugin + sonar-plugin-api-test-fixtures + ${sonar.plugin.api.version} + test + diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java index c4e4edd..80d3ac6 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPlugin.java @@ -15,15 +15,15 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck; +import java.util.Optional; + +import org.sonar.api.Plugin; + import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellLanguage; import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellQualityProfile; import com.github.sbaudoin.sonar.plugins.shellcheck.rules.ShellCheckRulesDefinition; import com.github.sbaudoin.sonar.plugins.shellcheck.rules.ShellCheckSensor; import com.github.sbaudoin.sonar.plugins.shellcheck.settings.ShellCheckSettings; -import org.sonar.api.Plugin; - -import java.security.AccessControlException; -import java.util.Optional; public class ShellCheckPlugin implements Plugin { public static final String ADD_SHELL_LANGUAGE_ENV_VAR = "SHELLCHECK_LANGUAGE_ADD"; diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java index 90cd674..e1493da 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepository.java @@ -16,9 +16,10 @@ package com.github.sbaudoin.sonar.plugins.shellcheck.checks; import com.github.sbaudoin.sonar.plugins.shellcheck.util.FileSystem; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; import java.io.IOException; import java.net.URISyntaxException; @@ -33,7 +34,7 @@ public class CheckRepository { public static final String REPOSITORY_NAME = "ShellCheck"; public static final String RULES_DEFINITION_FOLDER = "org/sonar/l10n/shellcheck/rules/shellcheck"; - private static final Logger LOGGER = Loggers.get(CheckRepository.class); + private static final Logger LOGGER = LoggerFactory.getLogger(CheckRepository.class); private static final List RULE_KEYS = new ArrayList<>(); static { diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java index 7404a89..0d01f4a 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlighting.java @@ -15,22 +15,23 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.highlighting; -import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.BashLexer; -import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.Token; -import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.sensor.highlighting.TypeOfText; + +import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.BashLexer; +import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.Token; + /** * Class in charge of Shell code highlighting in SonarQube */ public class ShellHighlighting { - private static final Logger LOGGER = Loggers.get(ShellHighlighting.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ShellHighlighting.class); private List highlighting = new ArrayList<>(); private String code; diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java index 786e4b3..bbdaada 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/lexer/Token.java @@ -20,7 +20,7 @@ /** * This class represents the tokens identified by Shell lexers */ -public class Token implements Serializable, Comparable { +public class Token implements Serializable, Comparable { /** * The token type */ @@ -88,7 +88,7 @@ public String toString() { } @Override - public int compareTo(Object o) { + public int compareTo(Token o) { Token t = (Token) o; if (this.start != t.start) { return (this.start - t.start); diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java index 3dab46b..c0bb245 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounter.java @@ -15,25 +15,24 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.measures; +import java.io.IOException; +import java.io.Serializable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.FileLinesContext; import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.measures.Metric; -import org.sonar.api.utils.Version; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import java.io.IOException; -import java.io.Serializable; /** * Class used to count the code and comment lines of a Shell script and save these "facts" * into SonarQube as measures */ public class LineCounter { - private static final Logger LOGGER = Loggers.get(LineCounter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(LineCounter.class); /** * Hide constructor @@ -73,9 +72,6 @@ public static void analyse(SensorContext context, FileLinesContextFactory fileLi private static void saveMeasures(InputFile script, LineCountData data, FileLinesContext fileLinesContext, SensorContext context) { for (int line = 1; line <= data.linesNumber(); line++) { fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, data.linesOfCodeLines().contains(line) ? 1 : 0); - if (Version.create(7, 3).isGreaterThanOrEqual(context.getSonarQubeVersion())) { - fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, data.effectiveCommentLines().contains(line) ? 1 : 0); - } } fileLinesContext.save(); diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java index ba0dbda..bee6c54 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinition.java @@ -17,6 +17,8 @@ import com.github.sbaudoin.sonar.plugins.shellcheck.checks.CheckRepository; import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellLanguage; + +import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; import org.sonarsource.analyzer.commons.RuleMetadataLoader; @@ -24,11 +26,20 @@ import java.util.List; public class ShellCheckRulesDefinition implements RulesDefinition { + + private final SonarRuntime sonarRuntime; + + public ShellCheckRulesDefinition(SonarRuntime sonarRuntime) { + this.sonarRuntime = sonarRuntime; + } + @Override public void define(RulesDefinition.Context context) { - RulesDefinition.NewRepository repository = context.createRepository(CheckRepository.REPOSITORY_KEY, ShellLanguage.KEY).setName(CheckRepository.REPOSITORY_NAME); + RulesDefinition.NewRepository repository = context + .createRepository(CheckRepository.REPOSITORY_KEY, ShellLanguage.KEY) + .setName(CheckRepository.REPOSITORY_NAME); - RuleMetadataLoader metadataLoader = new RuleMetadataLoader(CheckRepository.RULES_DEFINITION_FOLDER); + RuleMetadataLoader metadataLoader = new RuleMetadataLoader(CheckRepository.RULES_DEFINITION_FOLDER, this.sonarRuntime); List keys = new ArrayList<>(); CheckRepository.getRuleKeys().forEach(keys::add); diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java index 0996d2f..34e1c07 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensor.java @@ -24,6 +24,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.FilePredicate; import org.sonar.api.batch.fs.InputFile; @@ -35,8 +37,6 @@ import org.sonar.api.batch.sensor.issue.NewIssueLocation; import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; import java.io.*; import java.util.*; @@ -45,7 +45,7 @@ * SonarQube sensor class responsible for analyzing Shell scripts with ShellCheck */ public class ShellCheckSensor implements Sensor { - private static final Logger LOGGER = Loggers.get(ShellCheckSensor.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ShellCheckSensor.class); /** * The underlying file system that will give access to the files to be analyzed diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java index cf89a82..12d1756 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/settings/ShellCheckSettings.java @@ -15,13 +15,12 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.settings; -import org.sonar.api.PropertyType; -import org.sonar.api.config.PropertyDefinition; -import org.sonar.api.resources.Qualifiers; +import static java.util.Arrays.asList; import java.util.List; -import static java.util.Arrays.asList; +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; public class ShellCheckSettings { public static final String SHELLCHECK_PATH_KEY = "sonar.shellcheck.shellcheck.path"; @@ -45,14 +44,14 @@ public static List getProperties() { .defaultValue(FILE_SUFFIXES_DEFAULT_VALUE) .multiValues(true) .category("Shell") - .onQualifiers(Qualifiers.PROJECT) + .onConfigScopes(PropertyDefinition.ConfigScope.PROJECT) .build(), PropertyDefinition.builder(SHELLCHECK_PATH_KEY) .name("Path to shellcheck") .description("Path to the shellcheck executable. Leave it empty if the command is in the system path.") .defaultValue(SHELLCHECK_PATH_DEFAULT_VALUE) .category(SHELLCHECK_CATEGORY) - .onQualifiers(Qualifiers.PROJECT) + .onConfigScopes(PropertyDefinition.ConfigScope.PROJECT) .build(), PropertyDefinition.builder(SKIP_KEY) .type(PropertyType.BOOLEAN) @@ -60,7 +59,7 @@ public static List getProperties() { .description("If set to true, ShellCheck will not be executed for this project.") .defaultValue(SKIP_DEFAULT_VALUE) .category(SHELLCHECK_CATEGORY) - .onQualifiers(Qualifiers.PROJECT) + .onConfigScopes(PropertyDefinition.ConfigScope.PROJECT) .build() ); } diff --git a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java index ca125d4..f6cabe1 100644 --- a/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java +++ b/src/main/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystem.java @@ -15,23 +15,29 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.util; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; import java.io.Closeable; import java.io.IOException; import java.net.URI; -import java.nio.file.*; +import java.nio.file.DirectoryStream; +import java.nio.file.FileSystemAlreadyExistsException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.NotDirectoryException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Stream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * A class used to make the default {@code FileSystem} be closable like the other file systems without raising an exception */ public final class FileSystem implements Closeable { - private static final Logger LOGGER = Loggers.get(FileSystem.class); + private static final Logger LOGGER = LoggerFactory.getLogger(FileSystem.class); private boolean defaultFileSystem = false; private java.nio.file.FileSystem fs; diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java index 6af7020..a2a242e 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/ShellCheckPluginTest.java @@ -129,7 +129,7 @@ private Plugin.Context getContext(MapSettings settings) { PluginContextImpl.Builder contextBuilder = new PluginContextImpl.Builder(); contextBuilder.setSonarRuntime(runtime); if (settings != null) { - contextBuilder.setBootConfiguration(settings.asConfig()); + contextBuilder.setBootConfiguration(settings.asConfig()); } return contextBuilder.build(); } diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java index bf37351..a14be48 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/checks/CheckRepositoryTest.java @@ -17,9 +17,9 @@ import org.junit.Rule; import org.junit.Test; +import org.slf4j.event.Level; import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.api.testfixtures.log.LogTester; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -36,6 +36,6 @@ public void testGetRuleKey() { @Test public void testGetRuleKeys() { assertEquals(1, CheckRepository.getRuleKeys().size()); - assertTrue(logTester.logs(LoggerLevel.WARN).contains("Rule rule2 defined but not described (.html file missing)")); + assertTrue(logTester.logs(Level.WARN).contains("Rule rule2 defined but not described (.html file missing)")); } } diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java index a76e632..1b3d47d 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/HighlightingDataTest.java @@ -51,11 +51,6 @@ public NewHighlighting onFile(InputFile inputFile) { return null; } - @Override - public NewHighlighting highlight(int startOffset, int endOffset, TypeOfText typeOfText) { - return null; - } - @Override public NewHighlighting highlight(TextRange range, TypeOfText typeOfText) { return null; diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java index 61d4174..efa62cc 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/highlighting/ShellHighlightingTest.java @@ -28,9 +28,9 @@ import org.junit.Test; import org.mockito.MockedConstruction; import org.mockito.Mockito; +import org.slf4j.event.Level; import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.api.testfixtures.log.LogTester; import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; import com.github.sbaudoin.sonar.plugins.shellcheck.lexer.BashLexer; @@ -59,8 +59,8 @@ public void testConstructors() throws Exception { })) { logTester.clear(); new ShellHighlighting("foo"); - assertTrue(logTester.logs(LoggerLevel.WARN).size() > 0); - assertEquals("Could not scan Shell script and highlight code", logTester.logs(LoggerLevel.WARN).get(0)); + assertTrue(logTester.logs(Level.WARN).size() > 0); + assertEquals("Could not scan Shell script and highlight code", logTester.logs(Level.WARN).get(0)); } } diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java index 4c1bf02..aea3510 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCountDataTest.java @@ -15,21 +15,21 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.measures; -import junit.framework.TestCase; - import java.util.Arrays; import java.util.HashSet; +import junit.framework.TestCase; + public class LineCountDataTest extends TestCase { public void testAll() { LineCountData lcd = new LineCountData(1, null, null); - assertEquals(new Integer(1), lcd.linesNumber()); + assertEquals(Integer.valueOf(1), lcd.linesNumber()); assertNull(lcd.effectiveCommentLines()); assertNull(lcd.linesOfCodeLines()); - lcd = new LineCountData(5, new HashSet(Arrays.asList(1, 2)), new HashSet(Arrays.asList(3, 4))); - assertEquals(new Integer(5), lcd.linesNumber()); - assertEquals(new HashSet(Arrays.asList(3, 4)), lcd.effectiveCommentLines()); - assertEquals(new HashSet(Arrays.asList(1, 2)), lcd.linesOfCodeLines()); + lcd = new LineCountData(5, new HashSet(Arrays.asList(1, 2)), new HashSet(Arrays.asList(3, 4))); + assertEquals(Integer.valueOf(5), lcd.linesNumber()); + assertEquals(new HashSet(Arrays.asList(3, 4)), lcd.effectiveCommentLines()); + assertEquals(new HashSet(Arrays.asList(1, 2)), lcd.linesOfCodeLines()); } } diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java index c667ea8..0ef907b 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/measures/LineCounterTest.java @@ -15,30 +15,28 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.measures; -import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; +import org.slf4j.event.Level; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.sensor.internal.SensorContextTester; -import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.FileLinesContext; import org.sonar.api.measures.FileLinesContextFactory; -import org.sonar.api.utils.Version; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.api.testfixtures.log.LogTester; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; public class LineCounterTest { private FileLinesContextFactory fileLinesContextFactory; @@ -59,8 +57,12 @@ public void testNormal() throws IOException { SensorContextTester context = Utils.getSensorContext(); String filePath = "test4.sh"; LineCounter.analyse(context, fileLinesContextFactory, Utils.getInputFile(filePath)); - assertEquals(new Integer(13), context.measure(getComponentKey(filePath), CoreMetrics.NCLOC).value()); - assertEquals(new Integer(2), context.measure(getComponentKey(filePath), CoreMetrics.COMMENT_LINES).value()); + // Check the number 4 line in script containing code. be careful +1 is smowhere + assertEquals(Integer.valueOf(1), ((MyFileLinesContext)fileLinesContext).getIntValue(CoreMetrics.NCLOC_DATA_KEY, 5)); + // Check the number 4 line in script containing comment. be careful +1 is smowhere + assertEquals(Integer.valueOf(0), ((MyFileLinesContext)fileLinesContext).getIntValue(CoreMetrics.NCLOC_DATA_KEY, 1)); + assertEquals(Integer.valueOf(13), context.measure(getComponentKey(filePath), CoreMetrics.NCLOC).value()); + assertEquals(Integer.valueOf(2), context.measure(getComponentKey(filePath), CoreMetrics.COMMENT_LINES).value()); } @Test @@ -71,34 +73,10 @@ public void testIOException() throws IOException { when(spy.contents()).thenThrow(new IOException("Cannot read file")); LineCounter.analyse(context, fileLinesContextFactory, spy); - assertEquals(1, logTester.logs(LoggerLevel.WARN).size()); - assertEquals("Unable to count lines for file " + inputFile.filename() + ", ignoring measures", logTester.logs(LoggerLevel.WARN).get(0)); + assertEquals(1, logTester.logs(Level.WARN).size()); + assertEquals("Unable to count lines for file " + inputFile.filename() + ", ignoring measures", logTester.logs(Level.WARN).get(0)); } - @Test - public void testAnalyseWithOldVersion() throws IOException { - SensorContextTester context = Utils.getSensorContext(); - String filePath = "test4.sh"; - InputFile inputFile = Utils.getInputFile(filePath); - - // Version taken from the Maven dependency - LineCounter.analyse(context, fileLinesContextFactory, Utils.getInputFile(filePath)); - assertEquals(new Integer(1), fileLinesContext.getIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, 2)); - } - - @Test - public void testAnalyseWithNewVersion() throws IOException { - SensorContextTester context = Utils.getSensorContext(); - context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.create(7, 5), context.runtime().getSonarQubeSide())); - String filePath = "test4.sh"; - InputFile inputFile = Utils.getInputFile(filePath); - - // With version 7.5, the COMMENT_LINES_DATA metrics should not be set - LineCounter.analyse(context, fileLinesContextFactory, Utils.getInputFile(filePath)); - assertEquals(new Integer(-1), fileLinesContext.getIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, 2)); - } - - private String getComponentKey(String filePath) { return Utils.MODULE_KEY + ":src/test/resources/" + filePath; } @@ -119,7 +97,6 @@ public void setIntValue(String metricKey, int line, int value) { } } - @Override public Integer getIntValue(String metricKey, int line) { if (intValues.containsKey(metricKey)) { return intValues.get(metricKey).getOrDefault(line, -1); @@ -133,11 +110,6 @@ public void setStringValue(String metricKey, int line, String value) { } - @Override - public String getStringValue(String metricKey, int line) { - return null; - } - @Override public void save() { diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java index 0ae19c3..025f343 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckRulesDefinitionTest.java @@ -15,17 +15,26 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.rules; -import com.github.sbaudoin.sonar.plugins.shellcheck.checks.CheckRepository; -import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellLanguage; -import junit.framework.TestCase; +import static org.junit.Assert.assertNotEquals; + +import org.sonar.api.SonarRuntime; +import org.sonar.api.batch.sensor.internal.SensorContextTester; +import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.server.rule.RulesDefinition; import org.sonar.api.server.rule.RulesDefinition.Rule; +import org.sonar.api.utils.Version; -import static org.junit.Assert.assertNotEquals; +import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; +import com.github.sbaudoin.sonar.plugins.shellcheck.checks.CheckRepository; +import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellLanguage; + +import junit.framework.TestCase; public class ShellCheckRulesDefinitionTest extends TestCase { public void testDefine() { - ShellCheckRulesDefinition rulesDefinition = new ShellCheckRulesDefinition(); + SensorContextTester sensorContext = Utils.getSensorContext(); + SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(7, 5), sensorContext.runtime().getSonarQubeSide(), sensorContext.runtime().getEdition()); + ShellCheckRulesDefinition rulesDefinition = new ShellCheckRulesDefinition(sonarRuntime); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); RulesDefinition.Repository repository = context.repository(CheckRepository.REPOSITORY_KEY); diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java index 31e88d3..9486abb 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorNoRuleTest.java @@ -20,13 +20,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.slf4j.event.Level; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.measures.FileLinesContext; import org.sonar.api.measures.FileLinesContextFactory; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.api.testfixtures.log.LogTester; import java.io.IOException; @@ -65,10 +65,10 @@ public void testNoActiveRule() throws IOException { ShellCheckSensor sensor = new ShellCheckSensor(fs, fileLinesContextFactory); sensor.execute(context); - assertEquals(3, logTester.logs(LoggerLevel.INFO).size()); - assertTrue(logTester.logs(LoggerLevel.INFO).get(0).startsWith("No active rules found for this plugin, skipping analysis of test")); - assertTrue(logTester.logs(LoggerLevel.INFO).get(1).startsWith("No active rules found for this plugin, skipping analysis of test")); - assertTrue(logTester.logs(LoggerLevel.INFO).get(2).startsWith("No active rules found for this plugin, skipping analysis of test")); + assertEquals(3, logTester.logs(Level.INFO).size()); + assertTrue(logTester.logs(Level.INFO).get(0).startsWith("No active rules found for this plugin, skipping analysis of test")); + assertTrue(logTester.logs(Level.INFO).get(1).startsWith("No active rules found for this plugin, skipping analysis of test")); + assertTrue(logTester.logs(Level.INFO).get(2).startsWith("No active rules found for this plugin, skipping analysis of test")); assertEquals(0, context.allIssues().size()); } } diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java index d346841..f77fc7c 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/rules/ShellCheckSensorTest.java @@ -15,18 +15,35 @@ */ package com.github.sbaudoin.sonar.plugins.shellcheck.rules; -import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; -import com.github.sbaudoin.sonar.plugins.shellcheck.checks.CheckRepository; -import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellLanguage; -import com.github.sbaudoin.sonar.plugins.shellcheck.settings.ShellCheckSettings; +import static com.github.sbaudoin.sonar.plugins.shellcheck.Utils.issueExists; +import static com.github.sbaudoin.sonar.plugins.shellcheck.Utils.setShellRights; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.function.Predicate; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.slf4j.event.Level; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; +import org.sonar.api.batch.rule.internal.NewActiveRule; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.batch.sensor.issue.Issue; @@ -35,31 +52,18 @@ import org.sonar.api.measures.FileLinesContext; import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.api.testfixtures.log.LogTester; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.function.Predicate; - -import static com.github.sbaudoin.sonar.plugins.shellcheck.Utils.issueExists; -import static com.github.sbaudoin.sonar.plugins.shellcheck.Utils.setShellRights; -import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; +import com.github.sbaudoin.sonar.plugins.shellcheck.Utils; +import com.github.sbaudoin.sonar.plugins.shellcheck.checks.CheckRepository; +import com.github.sbaudoin.sonar.plugins.shellcheck.languages.ShellLanguage; +import com.github.sbaudoin.sonar.plugins.shellcheck.settings.ShellCheckSettings; public class ShellCheckSensorTest { private static final String RULE_ID1 = "SC2037"; private static final String RULE_ID2 = "SC2086"; - private final RuleKey ruleKey1 = RuleKey.of(CheckRepository.REPOSITORY_KEY, RULE_ID1); - private final RuleKey ruleKey2 = RuleKey.of(CheckRepository.REPOSITORY_KEY, RULE_ID2); + private final NewActiveRule ruleKey1 = new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckRepository.REPOSITORY_KEY, RULE_ID1)).build(); + private final NewActiveRule ruleKey2 = new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckRepository.REPOSITORY_KEY, RULE_ID2)).build(); private SensorContextTester context; private ShellCheckSensor sensor; @@ -68,7 +72,7 @@ public class ShellCheckSensorTest { public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Rule - public LogTester logTester = new LogTester(); + public LogTester logTester = new LogTester().setLevel(Level.DEBUG); @Before public void init() throws Exception { @@ -79,10 +83,8 @@ public void init() throws Exception { context.setFileSystem(fs); ActiveRules activeRules = new ActiveRulesBuilder() - .create(ruleKey1) - .activate() - .create(ruleKey2) - .activate() + .addRule(ruleKey1) + .addRule(ruleKey2) .build(); context.setActiveRules(activeRules); @@ -106,8 +108,8 @@ public void testshellCheckVersion() throws IOException { // Have to set an invalid path because Travis' build container embeds ShellCheck context.settings().appendProperty(ShellCheckSettings.SHELLCHECK_PATH_KEY, "invalid-command"); sensor.execute(context); - assertEquals(1, logTester.logs(LoggerLevel.WARN).size()); - assertEquals("Cannot get ShellCheck version", logTester.logs(LoggerLevel.WARN).get(0)); + assertEquals(1, logTester.logs(Level.WARN).size()); + assertEquals("Cannot get ShellCheck version", logTester.logs(Level.WARN).get(0)); if (System.getProperty("os.name").toLowerCase().contains("windows")) { context.settings().setProperty(ShellCheckSettings.SHELLCHECK_PATH_KEY, "src\\test\\resources\\scripts\\shellcheck-version.cmd"); @@ -116,12 +118,12 @@ public void testshellCheckVersion() throws IOException { setShellRights("src/test/resources/scripts/shellcheck-version.sh"); } - List l = logTester.logs(LoggerLevel.INFO); + logTester.logs(Level.INFO); sensor.execute(context); - assertEquals(3, logTester.logs(LoggerLevel.INFO).size()); - assertEquals("ShellCheck version:", logTester.logs(LoggerLevel.INFO).get(0)); - assertEquals("ShellCheck - shell script analysis tool", logTester.logs(LoggerLevel.INFO).get(1)); - assertEquals("version: my-version", logTester.logs(LoggerLevel.INFO).get(2)); + assertEquals(3, logTester.logs(Level.INFO).size()); + assertEquals("ShellCheck version:", logTester.logs(Level.INFO).get(0)); + assertEquals("ShellCheck - shell script analysis tool", logTester.logs(Level.INFO).get(1)); + assertEquals("version: my-version", logTester.logs(Level.INFO).get(2)); } @Test @@ -158,8 +160,8 @@ public void testExecuteWithShellCheckErrorOutput() throws IOException { sensor.execute(context); - assertEquals(3, logTester.logs(LoggerLevel.WARN).size()); - logTester.logs(LoggerLevel.WARN).stream().forEach(log -> assertTrue(log.startsWith("Errors happened during analysis:"))); + assertEquals(3, logTester.logs(Level.WARN).size()); + logTester.logs(Level.WARN).stream().forEach(log -> assertTrue(log.startsWith("Errors happened during analysis:"))); } @Test @@ -230,9 +232,9 @@ public void testExecuteWithShellCheckLongOutput() throws IOException { Collection issues = context.allIssues(); assertEquals(3, issues.size()); - assertTrue(issueExists(issues, ruleKey1, script1, 3, "To assign the output of a command, use var=\\$\\(cmd\\) \\.")); - assertTrue(issueExists(issues, ruleKey2, script1, 5, "Double quote to prevent globbing and word splitting.")); - assertTrue(issueExists(issues, ruleKey2, script1, 6, "Double quote to prevent globbing and word splitting.")); + assertTrue(issueExists(issues, ruleKey1.ruleKey(), script1, 3, "To assign the output of a command, use var=\\$\\(cmd\\) \\.")); + assertTrue(issueExists(issues, ruleKey2.ruleKey(), script1, 5, "Double quote to prevent globbing and word splitting.")); + assertTrue(issueExists(issues, ruleKey2.ruleKey(), script1, 6, "Double quote to prevent globbing and word splitting.")); } @Test @@ -244,8 +246,8 @@ public void testGetShellCheckPath() { @Test public void testExecuteCommand() { - ArrayList stdOut = new ArrayList(); - ArrayList stdErr = new ArrayList(); + ArrayList stdOut = new ArrayList(); + ArrayList stdErr = new ArrayList(); try { sensor.executeCommand(Arrays.asList("invalid-command", "bar"), stdOut, stdErr); @@ -292,9 +294,9 @@ public void testSaveIssues() throws IOException { Collection issues = context.allIssues(); assertEquals(3, issues.size()); - assertTrue(issueExists(issues, ruleKey1, shellScript, 3, "To assign the output of a command, use var=\\$\\(cmd\\) \\.")); - assertTrue(issueExists(issues, ruleKey2, shellScript, 5, "Double quote to prevent globbing and word splitting.")); - assertTrue(issueExists(issues, ruleKey2, shellScript, 6, "Double quote to prevent globbing and word splitting.")); + assertTrue(issueExists(issues, ruleKey1.ruleKey(), shellScript, 3, "To assign the output of a command, use var=\\$\\(cmd\\) \\.")); + assertTrue(issueExists(issues, ruleKey2.ruleKey(), shellScript, 5, "Double quote to prevent globbing and word splitting.")); + assertTrue(issueExists(issues, ruleKey2.ruleKey(), shellScript, 6, "Double quote to prevent globbing and word splitting.")); } @Test(expected = IllegalArgumentException.class) @@ -312,14 +314,14 @@ public void testSaveIssue() throws IOException { // Try to save issue for an unknown rule logTester.clear(); sensor.saveIssue(context, shellScript, 2, "foo", "An error here"); - assertTrue(logTester.logs(LoggerLevel.DEBUG).contains("Rule foo ignored, not found in repository")); + assertTrue(logTester.logs(Level.DEBUG).contains("Rule foo ignored, not found in repository")); // Save issue for a known rule logTester.clear(); sensor.saveIssue(context, shellScript, 2, RULE_ID1, "An error here"); assertEquals(1, context.allIssues().size()); Issue issue = (Issue)context.allIssues().toArray()[0]; - assertEquals(ruleKey1, issue.ruleKey()); + assertEquals(ruleKey1.ruleKey(), issue.ruleKey()); IssueLocation location = issue.primaryLocation(); assertEquals(shellScript.key(), location.inputComponent().key()); assertEquals(2, location.textRange().start().line()); @@ -350,8 +352,8 @@ public void testSkip() throws IOException { Collection issues = context.allIssues(); assertEquals(0, issues.size()); - assertEquals(1, logTester.logs(LoggerLevel.INFO).size()); - assertEquals("Plugin disabled by configuration for this project: the code will not be analyzed but will be highlighted.", logTester.logs(LoggerLevel.INFO).get(0)); + assertEquals(1, logTester.logs(Level.INFO).size()); + assertEquals("Plugin disabled by configuration for this project: the code will not be analyzed but will be highlighted.", logTester.logs(Level.INFO).get(0)); } @@ -392,22 +394,17 @@ public SensorDescriptor createIssuesForRuleRepositories(String... repositoryKeys } @Override - public SensorDescriptor requireProperty(String... propertyKey) { - return this; - } - - @Override - public SensorDescriptor requireProperties(String... propertyKeys) { + public SensorDescriptor global() { return this; } @Override - public SensorDescriptor global() { + public SensorDescriptor onlyWhenConfiguration(Predicate predicate) { return this; } @Override - public SensorDescriptor onlyWhenConfiguration(Predicate predicate) { + public SensorDescriptor processesFilesIndependently() { return this; } } diff --git a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java index 3f9677d..299ce40 100644 --- a/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java +++ b/src/test/java/com/github/sbaudoin/sonar/plugins/shellcheck/util/FileSystemTest.java @@ -17,8 +17,8 @@ import org.junit.Rule; import org.junit.Test; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; +import org.slf4j.event.Level; +import org.sonar.api.testfixtures.log.LogTester; import java.io.IOException; import java.lang.reflect.Field; @@ -54,26 +54,27 @@ public void testConstructor() throws NoSuchFieldException, IllegalAccessExceptio // Test with an invalid URI fs = new FileSystem(Paths.get("src").toUri()); - assertEquals(1, logTester.logs(LoggerLevel.WARN).size()); - assertTrue(logTester.logs(LoggerLevel.WARN).get(0).startsWith("Using default FS because of an error: ")); + assertEquals(1, logTester.logs(Level.WARN).size()); + assertTrue(logTester.logs(Level.WARN).get(0).startsWith("Using default FS because of an error: ")); assertTrue((Boolean)defaultFS.get(fs)); assertEquals(FileSystems.getDefault(), fsField.get(fs)); logTester.clear(); FileSystem fs2 = new FileSystem(new URI("file:///")); - assertEquals(1, logTester.logs(LoggerLevel.WARN).size()); - assertTrue(logTester.logs(LoggerLevel.WARN).get(0).startsWith("FS already exists for URI: ")); + assertEquals(1, logTester.logs(Level.WARN).size()); + assertTrue(logTester.logs(Level.WARN).get(0).startsWith("FS already exists for URI: ")); assertEquals(fsField.get(fs), fsField.get(fs2)); } @Test public void testReadDirectory() throws URISyntaxException, IOException { - FileSystem fs = new FileSystem(new URI("file:///")); - Path root = Paths.get("src", "test", "resources", "org", "sonar", "l10n", "shellcheck", "rules", "shellcheck"); - List content = fs.readDirectory(root.toUri()).collect(Collectors.toList()); - assertEquals(3, content.size()); - assertTrue(content.contains(root.resolve("rule1.html").toAbsolutePath())); - assertTrue(content.contains(root.resolve("rule1.json").toAbsolutePath())); - assertTrue(content.contains(root.resolve("rule2.json").toAbsolutePath())); + try (FileSystem fs = new FileSystem(new URI("file:///"))) { + Path root = Paths.get("src", "test", "resources", "org", "sonar", "l10n", "shellcheck", "rules", "shellcheck"); + List content = fs.readDirectory(root.toUri()).collect(Collectors.toList()); + assertEquals(3, content.size()); + assertTrue(content.contains(root.resolve("rule1.html").toAbsolutePath())); + assertTrue(content.contains(root.resolve("rule1.json").toAbsolutePath())); + assertTrue(content.contains(root.resolve("rule2.json").toAbsolutePath())); + } } } From e4c3a17b4853b86d4261a6500e00edbb94908c17 Mon Sep 17 00:00:00 2001 From: didierC Date: Sun, 16 Feb 2025 00:23:02 +0100 Subject: [PATCH 7/8] Upgrade it test --- it/Dockerfile-auditor | 40 +++++++++++++++++++++++++--- it/audit.sh | 53 ++++++++++--------------------------- it/docker-compose.yml | 11 ++++---- it/it.sh | 48 +++++++++++++++++++++++++-------- it/sonar-project.properties | 2 -- 5 files changed, 93 insertions(+), 61 deletions(-) mode change 100644 => 100755 it/it.sh diff --git a/it/Dockerfile-auditor b/it/Dockerfile-auditor index fd17dfa..e969c74 100644 --- a/it/Dockerfile-auditor +++ b/it/Dockerfile-auditor @@ -1,4 +1,38 @@ -ARG JDK_VERSION=8 -FROM openjdk:${JDK_VERSION} -COPY it /usr/src/myapp/it +ARG SCANNER_VERSION=latest +FROM sonarsource/sonar-scanner-cli:${SCANNER_VERSION} AS builder + +FROM eclipse-temurin:21-jre-alpine + +ARG SONAR_SCANNER_HOME=/opt/sonar-scanner +ENV HOME=/tmp \ + SONAR_SCANNER_HOME=${SONAR_SCANNER_HOME} \ + XDG_CONFIG_HOME=/tmp \ + SONAR_USER_HOME=${SONAR_SCANNER_HOME}/.sonar \ + PATH=${SONAR_SCANNER_HOME}/bin:${PATH} \ + SRC_PATH=/usr/src \ + SCANNER_WORKDIR_PATH=/tmp/.scannerwork \ + LANG=C.UTF-8 \ + LC_ALL=C.UTF-8 \ + PYTHONUNBUFFERED=1 + WORKDIR /usr/src/myapp/it + +USER root +# Copy Scanner installation from builder image +COPY --from=builder /opt/sonar-scanner /opt/sonar-scanner + + +RUN apk update --no-cache && \ + apk add --update --no-cache -q curl gcc jq libffi-dev musl-dev openssl-dev python3 py3-requests shellcheck + +RUN set -eux && \ + addgroup --gid 1000 scanner-cli && \ + adduser --uid 1000 --ingroup scanner-cli --disabled-password --no-create-home --gecos "" scanner-cli && \ + chown -R scanner-cli:scanner-cli "${SONAR_SCANNER_HOME}" "${SRC_PATH}" && \ + mkdir -p "${SRC_PATH}" "${SONAR_USER_HOME}" "${SONAR_USER_HOME}/cache" "${SCANNER_WORKDIR_PATH}" && \ + chown -R scanner-cli:scanner-cli "${SONAR_SCANNER_HOME}" "${SRC_PATH}" "${SCANNER_WORKDIR_PATH}" && \ + chmod -R 555 "${SONAR_SCANNER_HOME}" && \ + chmod -R 754 "${SRC_PATH}" "${SONAR_USER_HOME}" "${SCANNER_WORKDIR_PATH}" + +USER scanner-cli +COPY it /usr/src/myapp/it diff --git a/it/audit.sh b/it/audit.sh index 06bac8e..43cac0b 100644 --- a/it/audit.sh +++ b/it/audit.sh @@ -1,26 +1,12 @@ -#!/bin/bash -e - -# Install requirements -echo "Installing ShellCheck..." -if grep -q Debian /etc/issue -then - apt-get -qq update - apt-get -qq install -y shellcheck > /dev/null -else - apk update - apk add -q shellcheck -fi - -# Install sonar-runner -echo "Installing Sonar scanner..." -cd /tmp -wget -q https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SCANNER_VERSION.zip -unzip -q sonar-scanner-cli-$SCANNER_VERSION.zip -export PATH=/tmp/sonar-scanner-$SCANNER_VERSION/bin:$PATH +#!/bin/sh -e # Configure sonar-runner -echo "sonar.host.url=http://sonarqube:9000" > /tmp/sonar-scanner-$SCANNER_VERSION/conf/sonar-scanner.properties +export SONAR_HOST_URL="http://sonarqube:9000" +# Generate Analysis token +echo "Generating analysis token..." +export SONAR_TOKEN=$(curl -su "admin:admin" -XPOST "$SONAR_HOST_URL/api/user_tokens/generate?name=analysis_token&type=GLOBAL_ANALYSIS_TOKEN" | jq -r '.token') +echo $SONAR_TOKEN # Audit code echo "Launching scanner..." cd /usr/src/myapp/it @@ -43,20 +29,14 @@ sleep 10 # Check audit result echo "Checking result..." -if grep -q Debian /etc/issue -then - apt-get -qq install -y python3-pip > /dev/null -else - apk add -q curl gcc musl-dev libffi-dev openssl-dev py3 py3-dev -fi -pip3 install -q requests python3 << EOF from __future__ import print_function import requests import sys -r = requests.get('http://sonarqube:9000/api/measures/component?component=my:project&metricKeys=ncloc,comment_lines,lines,files,directories,violations', auth=('admin', 'admin')) +r = requests.get('http://sonarqube:9000/api/measures/component?component=my:project&metricKeys=ncloc,comment_lines,lines,files,violations', auth=('admin', 'admin')) if r.status_code != 200: + print('Invalid server response: ' + str(r.status_code), file=sys.stderr) sys.exit(1) data = r.json() @@ -70,20 +50,15 @@ for measure in data['component']['measures']: if measure['metric'] == 'lines' and measure['value'] == '8': print('lines metrics OK') lines = True -# if measure['metric'] == 'ncloc' and measure['value'] == '87': -# print('ncloc metrics OK') -# ncloc = True - ncloc = True + if measure['metric'] == 'ncloc' and measure['value'] == '3': + print('ncloc metrics OK') + ncloc = True if measure['metric'] == 'files' and measure['value'] == '1': print('files metrics OK') files = True - if measure['metric'] == 'directories' and measure['value'] == '1': - print('directories metrics OK') - directories = True -# if measure['metric'] == 'comment_lines' and measure['value'] == '1': -# print('comment_lines metrics OK') -# comment_lines = True - comment_lines = True + if measure['metric'] == 'comment_lines' and measure['value'] == '1': + print('comment_lines metrics OK') + comment_lines = True if measure['metric'] == 'violations' and measure['value'] == '3': print('violations metrics OK') violations = True diff --git a/it/docker-compose.yml b/it/docker-compose.yml index fa0b117..c7d3785 100644 --- a/it/docker-compose.yml +++ b/it/docker-compose.yml @@ -1,8 +1,7 @@ --- -version: '2.2' services: sonarqube: - image: sonarqube:${SONARQUBE_VERSION:-6.6} + image: sonarqube:${SONARQUBE_VERSION:-community} ports: - "9000:9000" environment: @@ -10,14 +9,14 @@ services: security_opt: - seccomp:unconfined auditor: - image: auditor:${SCANNER_VERSION}-jdk${JAVA_VERSION:-8} + image: auditor:${SCANNER_VERSION:-latest} build: context: .. dockerfile: it/Dockerfile-auditor args: - JDK_VERSION: ${JAVA_VERSION:-8} + SCANNER_VERSION: ${SCANNER_VERSION:-latest} links: - sonarqube - command: /bin/bash -e /usr/src/myapp/it/audit.sh + command: /bin/sh -e /usr/src/myapp/it/audit.sh environment: - SCANNER_VERSION: + SCANNER_VERSION: ${SCANNER_VERSION:-latest} diff --git a/it/it.sh b/it/it.sh old mode 100644 new mode 100755 index 38a14b8..eb2ae9a --- a/it/it.sh +++ b/it/it.sh @@ -1,13 +1,39 @@ #!/bin/bash -export SONARQUBE_VERSION="$1" -export SCANNER_VERSION="$2" -export JAVA_VERSION="$3" -if [ -z "$SCANNER_VERSION" ] -then - echo "Missing parameters: " >&2 - exit 1 -fi +usage(){ + echo -e "\nUsage: $0 [sSh] \n" + echo "-h : Display help" + echo "-s [SONAR-SCANNER] : Take Sonar-scanner tag image from https://hub.docker.com/r/sonarsource/sonar-scanner-cli" + echo "-S [SONARQUBE] : Take SonarQube tag image from https://hub.docker.com/_/sonarqube" +} + +OPTSTRING=":s:S:h" + +while getopts ${OPTSTRING} opt; do + case ${opt} in + s) + export SCANNER_VERSION=$OPTARG + ;; + S) + export SONARQUBE_VERSION=$OPTARG + ;; + h) + usage + exit 0 + ;; + :) + echo "Option -${OPTARG} requires an argument." + usage + exit 1 + ;; + ?) + echo "Invalid option: -${OPTARG}." + usage + exit 1 + ;; + esac +done + export SCRIPT_DIR=`dirname $0` @@ -18,9 +44,9 @@ docker-compose -f $SCRIPT_DIR/docker-compose.yml down # Start containers echo "Starting SonarQube..." docker-compose -f $SCRIPT_DIR/docker-compose.yml up -d sonarqube -CONTAINER_NAME=$(docker ps --format "{{.Names}}" | grep 'it_sonarqube_1.*' | head -1) +CONTAINER_NAME=$(docker ps --format "{{.Names}}" | grep 'it-sonarqube-1.*' | head -1) # Wait for SonarQube to be up -grep -q "SonarQube is up" <(docker logs --follow --tail 0 $CONTAINER_NAME) +grep -q "SonarQube is operational" <(docker logs --follow --tail 0 $CONTAINER_NAME) echo "SonarQube started!" # Copy the plugin @@ -30,7 +56,7 @@ docker cp $SCRIPT_DIR/../target/sonar-shellcheck-plugin-$MAVEN_VERSION.jar $CONT # Restart SonarQube docker-compose -f $SCRIPT_DIR/docker-compose.yml restart sonarqube # Wait for SonarQube to be up -grep -q "SonarQube is up" <(docker logs --follow --tail 0 $CONTAINER_NAME) +grep -q "SonarQube is operational" <(docker logs --follow --tail 0 $CONTAINER_NAME) # Check plug-in installation docker exec -u root $CONTAINER_NAME bash -c "if grep -q Alpine /etc/issue; then apk update && apk add -q curl; fi" if ! docker exec $CONTAINER_NAME curl -su admin:admin http://localhost:9000/api/plugins/installed | python -c ' diff --git a/it/sonar-project.properties b/it/sonar-project.properties index d6204ea..3bdeb2a 100644 --- a/it/sonar-project.properties +++ b/it/sonar-project.properties @@ -3,5 +3,3 @@ sonar.projectName=ShellCheck sonar.projectVersion=1.0 sonar.sources=src sonar.scm.disabled=True -sonar.login=admin -sonar.password=admin From 958677261b2c0e7b8707f1409da5839e4fb497f8 Mon Sep 17 00:00:00 2001 From: didierC Date: Mon, 24 Feb 2025 19:29:28 +0100 Subject: [PATCH 8/8] Upgrade shellcheck rules and scripts to update shellcheck rules --- .../shellcheck/rules/shellcheck/SC1000.html | 12 +- .../shellcheck/rules/shellcheck/SC1001.html | 14 +- .../shellcheck/rules/shellcheck/SC1003.html | 13 +- .../shellcheck/rules/shellcheck/SC1004.html | 14 +- .../shellcheck/rules/shellcheck/SC1007.html | 19 ++- .../shellcheck/rules/shellcheck/SC1007.json | 2 +- .../shellcheck/rules/shellcheck/SC1008.html | 10 +- .../shellcheck/rules/shellcheck/SC1009.html | 2 +- .../shellcheck/rules/shellcheck/SC1010.html | 16 +- .../shellcheck/rules/shellcheck/SC1010.json | 2 +- .../shellcheck/rules/shellcheck/SC1011.html | 17 +- .../shellcheck/rules/shellcheck/SC1012.html | 57 +++++-- .../shellcheck/rules/shellcheck/SC1012.json | 2 +- .../shellcheck/rules/shellcheck/SC1014.html | 52 +++++-- .../shellcheck/rules/shellcheck/SC1014.json | 2 +- .../shellcheck/rules/shellcheck/SC1015.html | 16 ++ .../shellcheck/rules/shellcheck/SC1015.json | 14 ++ .../shellcheck/rules/shellcheck/SC1016.html | 15 ++ .../shellcheck/rules/shellcheck/SC1016.json | 14 ++ .../shellcheck/rules/shellcheck/SC1017.html | 16 +- .../shellcheck/rules/shellcheck/SC1018.html | 9 +- .../shellcheck/rules/shellcheck/SC1018.json | 2 +- .../shellcheck/rules/shellcheck/SC1019.html | 4 +- .../shellcheck/rules/shellcheck/SC1020.html | 13 +- .../shellcheck/rules/shellcheck/SC1020.json | 2 +- .../shellcheck/rules/shellcheck/SC1026.html | 6 +- .../shellcheck/rules/shellcheck/SC1027.html | 16 +- .../shellcheck/rules/shellcheck/SC1028.html | 8 +- .../shellcheck/rules/shellcheck/SC1029.html | 4 +- .../shellcheck/rules/shellcheck/SC1033.html | 10 +- .../shellcheck/rules/shellcheck/SC1035.html | 4 +- .../shellcheck/rules/shellcheck/SC1036.html | 10 +- .../shellcheck/rules/shellcheck/SC1037.html | 14 +- .../shellcheck/rules/shellcheck/SC1038.html | 6 +- .../shellcheck/rules/shellcheck/SC1038.json | 2 +- .../shellcheck/rules/shellcheck/SC1039.html | 12 +- .../shellcheck/rules/shellcheck/SC1040.html | 8 +- .../shellcheck/rules/shellcheck/SC1041.html | 15 +- .../shellcheck/rules/shellcheck/SC1041.json | 2 +- .../shellcheck/rules/shellcheck/SC1043.html | 20 ++- .../shellcheck/rules/shellcheck/SC1043.json | 2 +- .../shellcheck/rules/shellcheck/SC1044.html | 10 +- .../shellcheck/rules/shellcheck/SC1044.json | 2 +- .../shellcheck/rules/shellcheck/SC1045.html | 6 +- .../shellcheck/rules/shellcheck/SC1045.json | 2 +- .../shellcheck/rules/shellcheck/SC1046.html | 6 +- .../shellcheck/rules/shellcheck/SC1046.json | 2 +- .../shellcheck/rules/shellcheck/SC1047.html | 2 +- .../shellcheck/rules/shellcheck/SC1047.json | 2 +- .../shellcheck/rules/shellcheck/SC1048.html | 6 +- .../shellcheck/rules/shellcheck/SC1048.json | 2 +- .../shellcheck/rules/shellcheck/SC1049.html | 6 +- .../shellcheck/rules/shellcheck/SC1049.json | 2 +- .../shellcheck/rules/shellcheck/SC1050.html | 6 +- .../shellcheck/rules/shellcheck/SC1050.json | 2 +- .../shellcheck/rules/shellcheck/SC1051.html | 6 +- .../shellcheck/rules/shellcheck/SC1051.json | 2 +- .../shellcheck/rules/shellcheck/SC1052.html | 6 +- .../shellcheck/rules/shellcheck/SC1052.json | 2 +- .../shellcheck/rules/shellcheck/SC1053.html | 6 +- .../shellcheck/rules/shellcheck/SC1053.json | 2 +- .../shellcheck/rules/shellcheck/SC1054.html | 8 +- .../shellcheck/rules/shellcheck/SC1054.json | 2 +- .../shellcheck/rules/shellcheck/SC1055.html | 22 ++- .../shellcheck/rules/shellcheck/SC1055.json | 2 +- .../shellcheck/rules/shellcheck/SC1056.html | 8 +- .../shellcheck/rules/shellcheck/SC1056.json | 2 +- .../shellcheck/rules/shellcheck/SC1057.html | 21 ++- .../shellcheck/rules/shellcheck/SC1057.json | 2 +- .../shellcheck/rules/shellcheck/SC1058.html | 4 +- .../shellcheck/rules/shellcheck/SC1059.html | 26 +++- .../shellcheck/rules/shellcheck/SC1059.json | 2 +- .../shellcheck/rules/shellcheck/SC1060.html | 20 ++- .../shellcheck/rules/shellcheck/SC1060.json | 2 +- .../shellcheck/rules/shellcheck/SC1061.html | 6 +- .../shellcheck/rules/shellcheck/SC1061.json | 2 +- .../shellcheck/rules/shellcheck/SC1062.html | 2 +- .../shellcheck/rules/shellcheck/SC1062.json | 2 +- .../shellcheck/rules/shellcheck/SC1063.html | 32 +++- .../shellcheck/rules/shellcheck/SC1063.json | 2 +- .../shellcheck/rules/shellcheck/SC1064.html | 8 +- .../shellcheck/rules/shellcheck/SC1065.html | 6 +- .../shellcheck/rules/shellcheck/SC1065.json | 2 +- .../shellcheck/rules/shellcheck/SC1066.html | 12 +- .../shellcheck/rules/shellcheck/SC1067.html | 16 +- .../shellcheck/rules/shellcheck/SC1067.json | 2 +- .../shellcheck/rules/shellcheck/SC1068.html | 8 +- .../shellcheck/rules/shellcheck/SC1069.html | 6 +- .../shellcheck/rules/shellcheck/SC1070.html | 2 +- .../shellcheck/rules/shellcheck/SC1071.html | 2 +- .../shellcheck/rules/shellcheck/SC1072.html | 10 ++ .../shellcheck/rules/shellcheck/SC1074.html | 6 +- .../shellcheck/rules/shellcheck/SC1075.html | 9 +- .../shellcheck/rules/shellcheck/SC1075.json | 2 +- .../shellcheck/rules/shellcheck/SC1076.html | 17 +- .../shellcheck/rules/shellcheck/SC1076.json | 2 +- .../shellcheck/rules/shellcheck/SC1077.html | 10 +- .../shellcheck/rules/shellcheck/SC1078.html | 14 +- .../shellcheck/rules/shellcheck/SC1078.json | 2 +- .../shellcheck/rules/shellcheck/SC1080.html | 18 ++- .../shellcheck/rules/shellcheck/SC1081.html | 6 +- .../shellcheck/rules/shellcheck/SC1081.json | 2 +- .../shellcheck/rules/shellcheck/SC1082.html | 4 +- .../shellcheck/rules/shellcheck/SC1082.json | 2 +- .../shellcheck/rules/shellcheck/SC1083.html | 8 +- .../shellcheck/rules/shellcheck/SC1084.html | 6 +- .../shellcheck/rules/shellcheck/SC1086.html | 6 +- .../shellcheck/rules/shellcheck/SC1087.html | 6 +- .../shellcheck/rules/shellcheck/SC1087.json | 2 +- .../shellcheck/rules/shellcheck/SC1088.html | 17 +- .../shellcheck/rules/shellcheck/SC1089.html | 6 +- .../shellcheck/rules/shellcheck/SC1090.html | 8 +- .../shellcheck/rules/shellcheck/SC1090.json | 2 +- .../shellcheck/rules/shellcheck/SC1091.html | 16 +- .../shellcheck/rules/shellcheck/SC1092.html | 14 +- .../shellcheck/rules/shellcheck/SC1092.json | 2 +- .../shellcheck/rules/shellcheck/SC1094.html | 4 +- .../shellcheck/rules/shellcheck/SC1095.html | 6 +- .../shellcheck/rules/shellcheck/SC1097.html | 10 +- .../shellcheck/rules/shellcheck/SC1098.html | 6 +- .../shellcheck/rules/shellcheck/SC1098.json | 2 +- .../shellcheck/rules/shellcheck/SC1099.html | 6 +- .../shellcheck/rules/shellcheck/SC1100.html | 6 +- .../shellcheck/rules/shellcheck/SC1100.json | 2 +- .../shellcheck/rules/shellcheck/SC1101.html | 6 +- .../shellcheck/rules/shellcheck/SC1102.html | 6 +- .../shellcheck/rules/shellcheck/SC1102.json | 2 +- .../shellcheck/rules/shellcheck/SC1103.html | 31 +++- .../shellcheck/rules/shellcheck/SC1104.html | 8 +- .../shellcheck/rules/shellcheck/SC1105.html | 2 +- .../shellcheck/rules/shellcheck/SC1106.html | 6 +- .../shellcheck/rules/shellcheck/SC1107.html | 4 +- .../shellcheck/rules/shellcheck/SC1108.html | 8 +- .../shellcheck/rules/shellcheck/SC1109.html | 4 +- .../shellcheck/rules/shellcheck/SC1110.html | 8 +- .../shellcheck/rules/shellcheck/SC1110.json | 2 +- .../shellcheck/rules/shellcheck/SC1111.html | 10 +- .../shellcheck/rules/shellcheck/SC1111.json | 2 +- .../shellcheck/rules/shellcheck/SC1112.html | 8 +- .../shellcheck/rules/shellcheck/SC1112.json | 2 +- .../shellcheck/rules/shellcheck/SC1113.html | 6 +- .../shellcheck/rules/shellcheck/SC1114.html | 4 +- .../shellcheck/rules/shellcheck/SC1115.html | 6 +- .../shellcheck/rules/shellcheck/SC1116.html | 9 +- .../shellcheck/rules/shellcheck/SC1117.html | 20 +++ .../shellcheck/rules/shellcheck/SC1117.json | 14 ++ .../shellcheck/rules/shellcheck/SC1118.html | 8 +- .../shellcheck/rules/shellcheck/SC1119.html | 10 +- .../shellcheck/rules/shellcheck/SC1119.json | 2 +- .../shellcheck/rules/shellcheck/SC1120.html | 4 +- .../shellcheck/rules/shellcheck/SC1121.html | 9 +- .../shellcheck/rules/shellcheck/SC1122.html | 6 +- .../shellcheck/rules/shellcheck/SC1123.html | 6 +- .../shellcheck/rules/shellcheck/SC1124.html | 9 +- .../shellcheck/rules/shellcheck/SC1124.json | 2 +- .../shellcheck/rules/shellcheck/SC1125.html | 6 +- .../shellcheck/rules/shellcheck/SC1126.html | 9 +- .../shellcheck/rules/shellcheck/SC1127.html | 4 +- .../shellcheck/rules/shellcheck/SC1128.html | 4 +- .../shellcheck/rules/shellcheck/SC1129.html | 6 +- .../shellcheck/rules/shellcheck/SC1130.html | 6 +- .../shellcheck/rules/shellcheck/SC1131.html | 4 +- .../shellcheck/rules/shellcheck/SC1132.html | 4 +- .../shellcheck/rules/shellcheck/SC1133.html | 6 +- .../shellcheck/rules/shellcheck/SC1134.html | 5 +- .../shellcheck/rules/shellcheck/SC1134.json | 2 +- .../shellcheck/rules/shellcheck/SC1135.html | 6 +- .../shellcheck/rules/shellcheck/SC1135.json | 2 +- .../shellcheck/rules/shellcheck/SC1136.html | 7 +- .../shellcheck/rules/shellcheck/SC1137.html | 22 ++- .../shellcheck/rules/shellcheck/SC1137.json | 2 +- .../shellcheck/rules/shellcheck/SC1138.html | 16 +- .../shellcheck/rules/shellcheck/SC1138.json | 2 +- .../shellcheck/rules/shellcheck/SC1139.html | 10 +- .../shellcheck/rules/shellcheck/SC1139.json | 2 +- .../shellcheck/rules/shellcheck/SC1140.html | 6 +- .../shellcheck/rules/shellcheck/SC1141.html | 6 +- .../shellcheck/rules/shellcheck/SC1142.html | 10 +- .../shellcheck/rules/shellcheck/SC1142.json | 2 +- .../shellcheck/rules/shellcheck/SC1143.html | 6 +- .../shellcheck/rules/shellcheck/SC1144.html | 17 +- .../shellcheck/rules/shellcheck/SC1145.html | 17 +- .../shellcheck/rules/shellcheck/SC1146.html | 1 + .../shellcheck/rules/shellcheck/SC1146.json | 14 ++ .../shellcheck/rules/shellcheck/SC2000.html | 10 +- .../shellcheck/rules/shellcheck/SC2001.html | 67 +++++++- .../shellcheck/rules/shellcheck/SC2001.json | 2 +- .../shellcheck/rules/shellcheck/SC2002.html | 10 +- .../shellcheck/rules/shellcheck/SC2002.json | 2 +- .../shellcheck/rules/shellcheck/SC2003.html | 13 +- .../shellcheck/rules/shellcheck/SC2003.json | 2 +- .../shellcheck/rules/shellcheck/SC2004.html | 10 +- .../shellcheck/rules/shellcheck/SC2005.html | 22 +-- .../shellcheck/rules/shellcheck/SC2006.html | 10 +- .../shellcheck/rules/shellcheck/SC2007.html | 8 +- .../shellcheck/rules/shellcheck/SC2007.json | 2 +- .../shellcheck/rules/shellcheck/SC2008.html | 7 +- .../shellcheck/rules/shellcheck/SC2009.html | 11 +- .../shellcheck/rules/shellcheck/SC2009.json | 2 +- .../shellcheck/rules/shellcheck/SC2010.html | 13 +- .../shellcheck/rules/shellcheck/SC2011.html | 6 +- .../shellcheck/rules/shellcheck/SC2012.html | 22 +-- .../shellcheck/rules/shellcheck/SC2013.html | 27 ++-- .../shellcheck/rules/shellcheck/SC2013.json | 2 +- .../shellcheck/rules/shellcheck/SC2014.html | 6 +- .../shellcheck/rules/shellcheck/SC2015.html | 8 +- .../shellcheck/rules/shellcheck/SC2016.html | 19 ++- .../shellcheck/rules/shellcheck/SC2017.html | 6 +- .../shellcheck/rules/shellcheck/SC2018.html | 18 +-- .../shellcheck/rules/shellcheck/SC2018.json | 2 +- .../shellcheck/rules/shellcheck/SC2019.html | 2 +- .../shellcheck/rules/shellcheck/SC2019.json | 2 +- .../shellcheck/rules/shellcheck/SC2020.html | 8 +- .../shellcheck/rules/shellcheck/SC2021.html | 8 +- .../shellcheck/rules/shellcheck/SC2022.html | 10 +- .../shellcheck/rules/shellcheck/SC2022.json | 2 +- .../shellcheck/rules/shellcheck/SC2023.html | 13 +- .../shellcheck/rules/shellcheck/SC2023.json | 2 +- .../shellcheck/rules/shellcheck/SC2024.html | 12 +- .../shellcheck/rules/shellcheck/SC2025.html | 6 +- .../shellcheck/rules/shellcheck/SC2025.json | 2 +- .../shellcheck/rules/shellcheck/SC2026.html | 18 +-- .../shellcheck/rules/shellcheck/SC2026.json | 2 +- .../shellcheck/rules/shellcheck/SC2027.html | 6 +- .../shellcheck/rules/shellcheck/SC2028.html | 8 +- .../shellcheck/rules/shellcheck/SC2029.html | 8 +- .../shellcheck/rules/shellcheck/SC2031.html | 44 ++++-- .../shellcheck/rules/shellcheck/SC2032.html | 4 +- .../shellcheck/rules/shellcheck/SC2032.json | 2 +- .../shellcheck/rules/shellcheck/SC2033.html | 9 +- .../shellcheck/rules/shellcheck/SC2033.json | 2 +- .../shellcheck/rules/shellcheck/SC2034.html | 25 ++- .../shellcheck/rules/shellcheck/SC2035.html | 15 +- .../shellcheck/rules/shellcheck/SC2035.json | 2 +- .../shellcheck/rules/shellcheck/SC2036.html | 6 +- .../shellcheck/rules/shellcheck/SC2037.html | 6 +- .../shellcheck/rules/shellcheck/SC2038.html | 6 +- .../shellcheck/rules/shellcheck/SC2039.html | 84 +++++----- .../shellcheck/rules/shellcheck/SC2040.html | 3 +- .../shellcheck/rules/shellcheck/SC2040.json | 2 +- .../shellcheck/rules/shellcheck/SC2041.html | 6 +- .../shellcheck/rules/shellcheck/SC2042.html | 22 ++- .../shellcheck/rules/shellcheck/SC2043.html | 14 +- .../shellcheck/rules/shellcheck/SC2043.json | 2 +- .../shellcheck/rules/shellcheck/SC2044.html | 30 ++-- .../shellcheck/rules/shellcheck/SC2045.html | 14 +- .../shellcheck/rules/shellcheck/SC2046.html | 18 ++- .../shellcheck/rules/shellcheck/SC2048.html | 10 +- .../shellcheck/rules/shellcheck/SC2048.json | 2 +- .../shellcheck/rules/shellcheck/SC2049.html | 8 +- .../shellcheck/rules/shellcheck/SC2050.html | 11 +- .../shellcheck/rules/shellcheck/SC2051.html | 12 +- .../shellcheck/rules/shellcheck/SC2053.html | 6 +- .../shellcheck/rules/shellcheck/SC2054.html | 4 +- .../shellcheck/rules/shellcheck/SC2055.html | 14 +- .../shellcheck/rules/shellcheck/SC2056.html | 6 +- .../shellcheck/rules/shellcheck/SC2057.html | 7 +- .../shellcheck/rules/shellcheck/SC2058.html | 7 +- .../shellcheck/rules/shellcheck/SC2059.html | 25 +-- .../shellcheck/rules/shellcheck/SC2059.json | 2 +- .../shellcheck/rules/shellcheck/SC2060.html | 8 +- .../shellcheck/rules/shellcheck/SC2061.html | 6 +- .../shellcheck/rules/shellcheck/SC2062.html | 8 +- .../shellcheck/rules/shellcheck/SC2063.html | 10 +- .../shellcheck/rules/shellcheck/SC2064.html | 8 +- .../shellcheck/rules/shellcheck/SC2065.html | 4 +- .../shellcheck/rules/shellcheck/SC2066.html | 16 +- .../shellcheck/rules/shellcheck/SC2066.json | 2 +- .../shellcheck/rules/shellcheck/SC2067.html | 12 +- .../shellcheck/rules/shellcheck/SC2067.json | 2 +- .../shellcheck/rules/shellcheck/SC2068.html | 6 +- .../shellcheck/rules/shellcheck/SC2069.html | 13 +- .../shellcheck/rules/shellcheck/SC2069.json | 2 +- .../shellcheck/rules/shellcheck/SC2070.html | 8 +- .../shellcheck/rules/shellcheck/SC2071.html | 7 +- .../shellcheck/rules/shellcheck/SC2071.json | 2 +- .../shellcheck/rules/shellcheck/SC2072.html | 6 +- .../shellcheck/rules/shellcheck/SC2073.html | 8 +- .../shellcheck/rules/shellcheck/SC2074.html | 4 +- .../shellcheck/rules/shellcheck/SC2075.html | 17 +- .../shellcheck/rules/shellcheck/SC2075.json | 2 +- .../shellcheck/rules/shellcheck/SC2076.html | 12 +- .../shellcheck/rules/shellcheck/SC2077.html | 8 +- .../shellcheck/rules/shellcheck/SC2078.html | 7 +- .../shellcheck/rules/shellcheck/SC2079.html | 4 +- .../shellcheck/rules/shellcheck/SC2080.html | 9 +- .../shellcheck/rules/shellcheck/SC2081.html | 10 +- .../shellcheck/rules/shellcheck/SC2082.html | 12 +- .../shellcheck/rules/shellcheck/SC2082.json | 2 +- .../shellcheck/rules/shellcheck/SC2083.html | 20 ++- .../shellcheck/rules/shellcheck/SC2083.json | 2 +- .../shellcheck/rules/shellcheck/SC2084.html | 14 +- .../shellcheck/rules/shellcheck/SC2084.json | 2 +- .../shellcheck/rules/shellcheck/SC2086.html | 52 +++---- .../shellcheck/rules/shellcheck/SC2087.html | 16 +- .../shellcheck/rules/shellcheck/SC2087.json | 2 +- .../shellcheck/rules/shellcheck/SC2088.html | 8 +- .../shellcheck/rules/shellcheck/SC2089.html | 35 +++-- .../shellcheck/rules/shellcheck/SC2091.html | 21 ++- .../shellcheck/rules/shellcheck/SC2093.html | 8 +- .../shellcheck/rules/shellcheck/SC2093.json | 2 +- .../shellcheck/rules/shellcheck/SC2094.html | 6 +- .../shellcheck/rules/shellcheck/SC2094.json | 2 +- .../shellcheck/rules/shellcheck/SC2095.html | 23 ++- .../shellcheck/rules/shellcheck/SC2096.html | 4 +- .../shellcheck/rules/shellcheck/SC2097.html | 14 +- .../shellcheck/rules/shellcheck/SC2099.html | 13 +- .../shellcheck/rules/shellcheck/SC2100.html | 7 +- .../shellcheck/rules/shellcheck/SC2101.html | 8 +- .../shellcheck/rules/shellcheck/SC2102.html | 10 +- .../shellcheck/rules/shellcheck/SC2103.html | 8 +- .../shellcheck/rules/shellcheck/SC2104.html | 10 +- .../shellcheck/rules/shellcheck/SC2105.html | 8 +- .../shellcheck/rules/shellcheck/SC2106.html | 13 +- .../shellcheck/rules/shellcheck/SC2106.json | 2 +- .../shellcheck/rules/shellcheck/SC2107.html | 11 +- .../shellcheck/rules/shellcheck/SC2108.html | 6 +- .../shellcheck/rules/shellcheck/SC2109.html | 6 +- .../shellcheck/rules/shellcheck/SC2110.html | 6 +- .../shellcheck/rules/shellcheck/SC2111.html | 35 ++++- .../shellcheck/rules/shellcheck/SC2111.json | 2 +- .../shellcheck/rules/shellcheck/SC2112.html | 15 +- .../shellcheck/rules/shellcheck/SC2112.json | 2 +- .../shellcheck/rules/shellcheck/SC2113.html | 4 +- .../shellcheck/rules/shellcheck/SC2114.html | 8 +- .../shellcheck/rules/shellcheck/SC2115.html | 8 +- .../shellcheck/rules/shellcheck/SC2115.json | 2 +- .../shellcheck/rules/shellcheck/SC2116.html | 16 +- .../shellcheck/rules/shellcheck/SC2116.json | 2 +- .../shellcheck/rules/shellcheck/SC2117.html | 6 +- .../shellcheck/rules/shellcheck/SC2118.html | 18 ++- .../shellcheck/rules/shellcheck/SC2118.json | 2 +- .../shellcheck/rules/shellcheck/SC2119.html | 2 +- .../shellcheck/rules/shellcheck/SC2119.json | 2 +- .../shellcheck/rules/shellcheck/SC2120.html | 14 +- .../shellcheck/rules/shellcheck/SC2121.html | 8 +- .../shellcheck/rules/shellcheck/SC2121.json | 2 +- .../shellcheck/rules/shellcheck/SC2122.html | 9 +- .../shellcheck/rules/shellcheck/SC2122.json | 2 +- .../shellcheck/rules/shellcheck/SC2123.html | 8 +- .../shellcheck/rules/shellcheck/SC2124.html | 22 +-- .../shellcheck/rules/shellcheck/SC2125.html | 12 +- .../shellcheck/rules/shellcheck/SC2126.html | 10 +- .../shellcheck/rules/shellcheck/SC2127.html | 35 ++++- .../shellcheck/rules/shellcheck/SC2127.json | 2 +- .../shellcheck/rules/shellcheck/SC2128.html | 14 +- .../shellcheck/rules/shellcheck/SC2128.json | 2 +- .../shellcheck/rules/shellcheck/SC2129.html | 8 +- .../shellcheck/rules/shellcheck/SC2130.html | 7 +- .../shellcheck/rules/shellcheck/SC2139.html | 11 +- .../shellcheck/rules/shellcheck/SC2140.html | 16 +- .../shellcheck/rules/shellcheck/SC2140.json | 2 +- .../shellcheck/rules/shellcheck/SC2141.html | 14 +- .../shellcheck/rules/shellcheck/SC2142.html | 9 +- .../shellcheck/rules/shellcheck/SC2143.html | 6 +- .../shellcheck/rules/shellcheck/SC2144.html | 20 ++- .../shellcheck/rules/shellcheck/SC2145.html | 49 +++--- .../shellcheck/rules/shellcheck/SC2146.html | 13 +- .../shellcheck/rules/shellcheck/SC2147.html | 4 +- .../shellcheck/rules/shellcheck/SC2148.html | 6 +- .../shellcheck/rules/shellcheck/SC2149.html | 13 +- .../shellcheck/rules/shellcheck/SC2150.html | 10 +- .../shellcheck/rules/shellcheck/SC2151.html | 4 +- .../shellcheck/rules/shellcheck/SC2152.html | 10 +- .../shellcheck/rules/shellcheck/SC2153.html | 4 +- .../shellcheck/rules/shellcheck/SC2154.html | 22 +-- .../shellcheck/rules/shellcheck/SC2155.html | 25 ++- .../shellcheck/rules/shellcheck/SC2156.html | 6 +- .../shellcheck/rules/shellcheck/SC2157.html | 12 +- .../shellcheck/rules/shellcheck/SC2158.html | 6 +- .../shellcheck/rules/shellcheck/SC2159.html | 6 +- .../shellcheck/rules/shellcheck/SC2159.json | 2 +- .../shellcheck/rules/shellcheck/SC2160.html | 37 ++--- .../shellcheck/rules/shellcheck/SC2160.json | 2 +- .../shellcheck/rules/shellcheck/SC2161.html | 8 +- .../shellcheck/rules/shellcheck/SC2161.json | 2 +- .../shellcheck/rules/shellcheck/SC2162.html | 20 ++- .../shellcheck/rules/shellcheck/SC2162.json | 2 +- .../shellcheck/rules/shellcheck/SC2163.html | 10 +- .../shellcheck/rules/shellcheck/SC2163.json | 2 +- .../shellcheck/rules/shellcheck/SC2164.html | 18 +-- .../shellcheck/rules/shellcheck/SC2165.html | 6 +- .../shellcheck/rules/shellcheck/SC2166.html | 13 +- .../shellcheck/rules/shellcheck/SC2166.json | 2 +- .../shellcheck/rules/shellcheck/SC2168.html | 6 +- .../shellcheck/rules/shellcheck/SC2168.json | 2 +- .../shellcheck/rules/shellcheck/SC2169.html | 1 + .../shellcheck/rules/shellcheck/SC2170.html | 8 +- .../shellcheck/rules/shellcheck/SC2171.html | 10 +- .../shellcheck/rules/shellcheck/SC2172.html | 6 +- .../shellcheck/rules/shellcheck/SC2172.json | 2 +- .../shellcheck/rules/shellcheck/SC2173.html | 2 +- .../shellcheck/rules/shellcheck/SC2174.html | 8 +- .../shellcheck/rules/shellcheck/SC2175.html | 4 +- .../shellcheck/rules/shellcheck/SC2176.html | 8 +- .../shellcheck/rules/shellcheck/SC2176.json | 2 +- .../shellcheck/rules/shellcheck/SC2177.html | 6 +- .../shellcheck/rules/shellcheck/SC2177.json | 2 +- .../shellcheck/rules/shellcheck/SC2178.html | 11 +- .../shellcheck/rules/shellcheck/SC2179.html | 6 +- .../shellcheck/rules/shellcheck/SC2179.json | 2 +- .../shellcheck/rules/shellcheck/SC2180.html | 8 +- .../shellcheck/rules/shellcheck/SC2181.html | 23 ++- .../shellcheck/rules/shellcheck/SC2181.json | 2 +- .../shellcheck/rules/shellcheck/SC2182.html | 13 +- .../shellcheck/rules/shellcheck/SC2183.html | 6 +- .../shellcheck/rules/shellcheck/SC2183.json | 2 +- .../shellcheck/rules/shellcheck/SC2184.html | 9 +- .../shellcheck/rules/shellcheck/SC2185.html | 9 +- .../shellcheck/rules/shellcheck/SC2185.json | 2 +- .../shellcheck/rules/shellcheck/SC2186.html | 7 +- .../shellcheck/rules/shellcheck/SC2187.html | 6 +- .../shellcheck/rules/shellcheck/SC2187.json | 2 +- .../shellcheck/rules/shellcheck/SC2188.html | 6 +- .../shellcheck/rules/shellcheck/SC2188.json | 2 +- .../shellcheck/rules/shellcheck/SC2189.html | 8 +- .../shellcheck/rules/shellcheck/SC2190.html | 6 +- .../shellcheck/rules/shellcheck/SC2191.html | 8 +- .../shellcheck/rules/shellcheck/SC2192.html | 10 +- .../shellcheck/rules/shellcheck/SC2192.json | 2 +- .../shellcheck/rules/shellcheck/SC2193.html | 4 +- .../shellcheck/rules/shellcheck/SC2194.html | 6 +- .../shellcheck/rules/shellcheck/SC2195.html | 8 +- .../shellcheck/rules/shellcheck/SC2196.html | 12 +- .../shellcheck/rules/shellcheck/SC2197.html | 12 +- .../shellcheck/rules/shellcheck/SC2198.html | 12 +- .../shellcheck/rules/shellcheck/SC2199.html | 10 +- .../shellcheck/rules/shellcheck/SC2200.html | 9 +- .../shellcheck/rules/shellcheck/SC2201.html | 4 +- .../shellcheck/rules/shellcheck/SC2202.html | 11 +- .../shellcheck/rules/shellcheck/SC2203.html | 7 +- .../shellcheck/rules/shellcheck/SC2204.html | 10 +- .../shellcheck/rules/shellcheck/SC2205.html | 8 +- .../shellcheck/rules/shellcheck/SC2206.html | 14 +- .../shellcheck/rules/shellcheck/SC2207.html | 17 +- .../shellcheck/rules/shellcheck/SC2208.html | 6 +- .../shellcheck/rules/shellcheck/SC2209.html | 8 +- .../shellcheck/rules/shellcheck/SC2210.html | 12 +- .../shellcheck/rules/shellcheck/SC2211.html | 8 +- .../shellcheck/rules/shellcheck/SC2212.html | 6 +- .../shellcheck/rules/shellcheck/SC2212.json | 2 +- .../shellcheck/rules/shellcheck/SC2213.html | 20 +-- .../shellcheck/rules/shellcheck/SC2213.json | 2 +- .../shellcheck/rules/shellcheck/SC2214.html | 4 +- .../shellcheck/rules/shellcheck/SC2215.html | 10 +- .../shellcheck/rules/shellcheck/SC2216.html | 8 +- .../shellcheck/rules/shellcheck/SC2216.json | 2 +- .../shellcheck/rules/shellcheck/SC2217.html | 6 +- .../shellcheck/rules/shellcheck/SC2217.json | 2 +- .../shellcheck/rules/shellcheck/SC2218.html | 14 +- .../shellcheck/rules/shellcheck/SC2219.html | 11 +- .../shellcheck/rules/shellcheck/SC2220.html | 4 +- .../shellcheck/rules/shellcheck/SC2221.html | 15 +- .../shellcheck/rules/shellcheck/SC2223.html | 7 +- .../shellcheck/rules/shellcheck/SC2224.html | 6 +- .../shellcheck/rules/shellcheck/SC2225.html | 6 +- .../shellcheck/rules/shellcheck/SC2226.html | 10 +- .../shellcheck/rules/shellcheck/SC2226.json | 2 +- .../shellcheck/rules/shellcheck/SC2227.html | 10 +- .../shellcheck/rules/shellcheck/SC2229.html | 13 +- .../shellcheck/rules/shellcheck/SC2229.json | 2 +- .../shellcheck/rules/shellcheck/SC2230.html | 8 +- .../shellcheck/rules/shellcheck/SC2230.json | 2 +- .../shellcheck/rules/shellcheck/SC2231.html | 14 +- .../shellcheck/rules/shellcheck/SC2231.json | 2 +- .../shellcheck/rules/shellcheck/SC2232.html | 9 +- .../shellcheck/rules/shellcheck/SC2233.html | 10 +- .../shellcheck/rules/shellcheck/SC2234.html | 8 +- .../shellcheck/rules/shellcheck/SC2235.html | 8 +- .../shellcheck/rules/shellcheck/SC2236.html | 10 +- .../shellcheck/rules/shellcheck/SC2237.html | 9 +- .../shellcheck/rules/shellcheck/SC2238.html | 10 +- .../shellcheck/rules/shellcheck/SC2239.html | 6 +- .../shellcheck/rules/shellcheck/SC2240.html | 6 +- .../shellcheck/rules/shellcheck/SC2241.html | 4 +- .../shellcheck/rules/shellcheck/SC2242.html | 8 +- .../shellcheck/rules/shellcheck/SC2243.html | 10 +- .../shellcheck/rules/shellcheck/SC2243.json | 2 +- .../shellcheck/rules/shellcheck/SC2244.html | 15 +- .../shellcheck/rules/shellcheck/SC2244.json | 2 +- .../shellcheck/rules/shellcheck/SC2245.html | 17 +- .../shellcheck/rules/shellcheck/SC2246.html | 4 +- .../shellcheck/rules/shellcheck/SC2247.html | 17 +- .../shellcheck/rules/shellcheck/SC2247.json | 2 +- .../shellcheck/rules/shellcheck/SC2248.html | 8 +- .../shellcheck/rules/shellcheck/SC2248.json | 2 +- .../shellcheck/rules/shellcheck/SC2249.html | 12 +- .../shellcheck/rules/shellcheck/SC2250.html | 8 +- .../shellcheck/rules/shellcheck/SC2250.json | 2 +- .../shellcheck/rules/shellcheck/SC2251.html | 12 +- .../shellcheck/rules/shellcheck/SC2251.json | 2 +- .../shellcheck/rules/shellcheck/SC2252.html | 10 +- .../shellcheck/rules/shellcheck/SC2253.html | 6 +- .../shellcheck/rules/shellcheck/SC2254.html | 4 +- .../shellcheck/rules/shellcheck/SC2255.html | 4 +- .../shellcheck/rules/shellcheck/SC2256.html | 10 +- .../shellcheck/rules/shellcheck/SC2256.json | 2 +- .../shellcheck/rules/shellcheck/SC2257.html | 8 +- .../shellcheck/rules/shellcheck/SC2258.html | 28 +++- .../shellcheck/rules/shellcheck/SC2259.html | 4 +- .../shellcheck/rules/shellcheck/SC2260.html | 6 +- .../shellcheck/rules/shellcheck/SC2260.json | 2 +- .../shellcheck/rules/shellcheck/SC2261.html | 6 +- .../shellcheck/rules/shellcheck/SC2262.html | 10 +- .../shellcheck/rules/shellcheck/SC2264.html | 7 +- .../shellcheck/rules/shellcheck/SC2264.json | 2 +- .../shellcheck/rules/shellcheck/SC2265.html | 22 ++- .../shellcheck/rules/shellcheck/SC2265.json | 4 +- .../shellcheck/rules/shellcheck/SC2266.html | 22 ++- .../shellcheck/rules/shellcheck/SC2266.json | 2 +- .../shellcheck/rules/shellcheck/SC2267.html | 6 +- .../shellcheck/rules/shellcheck/SC2268.html | 14 +- .../shellcheck/rules/shellcheck/SC2268.json | 2 +- .../shellcheck/rules/shellcheck/SC2269.html | 4 +- .../shellcheck/rules/shellcheck/SC2270.html | 14 +- .../shellcheck/rules/shellcheck/SC2270.json | 2 +- .../shellcheck/rules/shellcheck/SC2271.html | 16 +- .../shellcheck/rules/shellcheck/SC2271.json | 2 +- .../shellcheck/rules/shellcheck/SC2272.html | 6 +- .../shellcheck/rules/shellcheck/SC2272.json | 2 +- .../shellcheck/rules/shellcheck/SC2273.html | 6 +- .../shellcheck/rules/shellcheck/SC2274.html | 6 +- .../shellcheck/rules/shellcheck/SC2275.html | 8 +- .../shellcheck/rules/shellcheck/SC2276.html | 12 +- .../shellcheck/rules/shellcheck/SC2276.json | 2 +- .../shellcheck/rules/shellcheck/SC2277.html | 8 +- .../shellcheck/rules/shellcheck/SC2277.json | 2 +- .../shellcheck/rules/shellcheck/SC2278.html | 8 +- .../shellcheck/rules/shellcheck/SC2279.html | 6 +- .../shellcheck/rules/shellcheck/SC2280.html | 6 +- .../shellcheck/rules/shellcheck/SC2281.html | 10 +- .../shellcheck/rules/shellcheck/SC2281.json | 2 +- .../shellcheck/rules/shellcheck/SC2282.html | 4 +- .../shellcheck/rules/shellcheck/SC2283.html | 11 +- .../shellcheck/rules/shellcheck/SC2284.html | 9 +- .../shellcheck/rules/shellcheck/SC2285.html | 9 +- .../shellcheck/rules/shellcheck/SC2286.html | 8 +- .../shellcheck/rules/shellcheck/SC2287.html | 4 +- .../shellcheck/rules/shellcheck/SC2288.html | 7 +- .../shellcheck/rules/shellcheck/SC2289.html | 6 +- .../shellcheck/rules/shellcheck/SC2290.html | 7 +- .../shellcheck/rules/shellcheck/SC2291.html | 6 +- .../shellcheck/rules/shellcheck/SC2292.html | 8 +- .../shellcheck/rules/shellcheck/SC2292.json | 2 +- .../shellcheck/rules/shellcheck/SC2293.html | 4 +- .../shellcheck/rules/shellcheck/SC2294.html | 40 ++--- .../shellcheck/rules/shellcheck/SC2295.html | 13 +- .../shellcheck/rules/shellcheck/SC2295.json | 2 +- .../shellcheck/rules/shellcheck/SC2296.html | 6 +- .../shellcheck/rules/shellcheck/SC2297.html | 10 +- .../shellcheck/rules/shellcheck/SC2297.json | 2 +- .../shellcheck/rules/shellcheck/SC2298.html | 11 +- .../shellcheck/rules/shellcheck/SC2299.html | 4 +- .../shellcheck/rules/shellcheck/SC2300.html | 4 +- .../shellcheck/rules/shellcheck/SC2301.html | 8 +- .../shellcheck/rules/shellcheck/SC2302.html | 28 ++-- .../shellcheck/rules/shellcheck/SC2302.json | 2 +- .../shellcheck/rules/shellcheck/SC2304.html | 4 +- .../shellcheck/rules/shellcheck/SC2305.html | 4 +- .../shellcheck/rules/shellcheck/SC2306.html | 21 ++- .../shellcheck/rules/shellcheck/SC2307.html | 4 +- .../shellcheck/rules/shellcheck/SC2308.html | 30 ++-- .../shellcheck/rules/shellcheck/SC2309.html | 11 +- .../shellcheck/rules/shellcheck/SC2310.html | 4 +- .../shellcheck/rules/shellcheck/SC2311.html | 4 +- .../shellcheck/rules/shellcheck/SC2312.html | 21 ++- .../shellcheck/rules/shellcheck/SC2313.html | 4 +- .../shellcheck/rules/shellcheck/SC2314.html | 9 +- .../shellcheck/rules/shellcheck/SC2314.json | 4 +- .../shellcheck/rules/shellcheck/SC2315.html | 9 +- .../shellcheck/rules/shellcheck/SC2315.json | 2 +- .../shellcheck/rules/shellcheck/SC2316.html | 26 ++++ .../shellcheck/rules/shellcheck/SC2316.json | 14 ++ .../shellcheck/rules/shellcheck/SC2317.html | 57 +++++++ .../shellcheck/rules/shellcheck/SC2317.json | 14 ++ .../shellcheck/rules/shellcheck/SC2318.html | 20 +++ .../shellcheck/rules/shellcheck/SC2318.json | 14 ++ .../shellcheck/rules/shellcheck/SC2319.html | 34 ++++ .../shellcheck/rules/shellcheck/SC2319.json | 14 ++ .../shellcheck/rules/shellcheck/SC2320.html | 28 ++++ .../shellcheck/rules/shellcheck/SC2320.json | 14 ++ .../shellcheck/rules/shellcheck/SC2321.html | 15 ++ .../shellcheck/rules/shellcheck/SC2321.json | 14 ++ .../shellcheck/rules/shellcheck/SC2322.html | 15 ++ .../shellcheck/rules/shellcheck/SC2322.json | 14 ++ .../shellcheck/rules/shellcheck/SC2323.html | 19 +++ .../shellcheck/rules/shellcheck/SC2323.json | 14 ++ .../shellcheck/rules/shellcheck/SC2324.html | 28 ++++ .../shellcheck/rules/shellcheck/SC2324.json | 14 ++ .../shellcheck/rules/shellcheck/SC2325.html | 21 +++ .../shellcheck/rules/shellcheck/SC2325.json | 14 ++ .../shellcheck/rules/shellcheck/SC2326.html | 21 +++ .../shellcheck/rules/shellcheck/SC2326.json | 14 ++ .../shellcheck/rules/shellcheck/SC2327.html | 2 + .../shellcheck/rules/shellcheck/SC2327.json | 14 ++ .../shellcheck/rules/shellcheck/SC2328.html | 24 +++ .../shellcheck/rules/shellcheck/SC2328.json | 14 ++ .../shellcheck/rules/shellcheck/SC2329.html | 25 +++ .../shellcheck/rules/shellcheck/SC2329.json | 14 ++ .../shellcheck/rules/shellcheck/SC2330.html | 25 +++ .../shellcheck/rules/shellcheck/SC2330.json | 14 ++ .../shellcheck/rules/shellcheck/SC3001.html | 10 +- .../shellcheck/rules/shellcheck/SC3002.html | 6 +- .../shellcheck/rules/shellcheck/SC3003.html | 20 +-- .../shellcheck/rules/shellcheck/SC3004.html | 14 +- .../shellcheck/rules/shellcheck/SC3004.json | 2 +- .../shellcheck/rules/shellcheck/SC3005.html | 24 ++- .../shellcheck/rules/shellcheck/SC3005.json | 2 +- .../shellcheck/rules/shellcheck/SC3006.html | 6 +- .../shellcheck/rules/shellcheck/SC3007.html | 16 +- .../shellcheck/rules/shellcheck/SC3007.json | 2 +- .../shellcheck/rules/shellcheck/SC3008.html | 28 +++- .../shellcheck/rules/shellcheck/SC3008.json | 2 +- .../shellcheck/rules/shellcheck/SC3009.html | 23 ++- .../shellcheck/rules/shellcheck/SC3009.json | 2 +- .../shellcheck/rules/shellcheck/SC3010.html | 5 +- .../shellcheck/rules/shellcheck/SC3011.html | 20 ++- .../shellcheck/rules/shellcheck/SC3011.json | 2 +- .../shellcheck/rules/shellcheck/SC3012.html | 11 +- .../shellcheck/rules/shellcheck/SC3013.html | 14 +- .../shellcheck/rules/shellcheck/SC3014.html | 12 +- .../shellcheck/rules/shellcheck/SC3015.html | 29 +++- .../shellcheck/rules/shellcheck/SC3016.html | 26 +++- .../shellcheck/rules/shellcheck/SC3016.json | 2 +- .../shellcheck/rules/shellcheck/SC3017.html | 6 +- .../shellcheck/rules/shellcheck/SC3018.html | 8 +- .../shellcheck/rules/shellcheck/SC3019.html | 10 +- .../shellcheck/rules/shellcheck/SC3020.html | 6 +- .../shellcheck/rules/shellcheck/SC3021.html | 29 +++- .../shellcheck/rules/shellcheck/SC3021.json | 2 +- .../shellcheck/rules/shellcheck/SC3022.html | 4 +- .../shellcheck/rules/shellcheck/SC3023.html | 6 +- .../shellcheck/rules/shellcheck/SC3023.json | 2 +- .../shellcheck/rules/shellcheck/SC3024.html | 8 +- .../shellcheck/rules/shellcheck/SC3025.html | 8 +- .../shellcheck/rules/shellcheck/SC3026.html | 17 +- .../shellcheck/rules/shellcheck/SC3028.html | 12 +- .../shellcheck/rules/shellcheck/SC3029.html | 20 ++- .../shellcheck/rules/shellcheck/SC3029.json | 2 +- .../shellcheck/rules/shellcheck/SC3030.html | 40 ++++- .../shellcheck/rules/shellcheck/SC3030.json | 2 +- .../shellcheck/rules/shellcheck/SC3031.html | 6 +- .../shellcheck/rules/shellcheck/SC3032.html | 21 ++- .../shellcheck/rules/shellcheck/SC3032.json | 2 +- .../shellcheck/rules/shellcheck/SC3033.html | 23 ++- .../shellcheck/rules/shellcheck/SC3033.json | 2 +- .../shellcheck/rules/shellcheck/SC3034.html | 8 +- .../shellcheck/rules/shellcheck/SC3036.html | 19 ++- .../shellcheck/rules/shellcheck/SC3036.json | 2 +- .../shellcheck/rules/shellcheck/SC3037.html | 8 +- .../shellcheck/rules/shellcheck/SC3038.html | 6 +- .../shellcheck/rules/shellcheck/SC3039.html | 8 +- .../shellcheck/rules/shellcheck/SC3039.json | 2 +- .../shellcheck/rules/shellcheck/SC3040.html | 35 +++-- .../shellcheck/rules/shellcheck/SC3040.json | 2 +- .../shellcheck/rules/shellcheck/SC3041.html | 21 ++- .../shellcheck/rules/shellcheck/SC3041.json | 2 +- .../shellcheck/rules/shellcheck/SC3042.html | 21 ++- .../shellcheck/rules/shellcheck/SC3042.json | 2 +- .../shellcheck/rules/shellcheck/SC3043.html | 6 +- .../shellcheck/rules/shellcheck/SC3044.html | 10 +- .../shellcheck/rules/shellcheck/SC3045.html | 6 +- .../shellcheck/rules/shellcheck/SC3046.html | 8 +- .../shellcheck/rules/shellcheck/SC3047.html | 10 +- .../shellcheck/rules/shellcheck/SC3048.html | 10 +- .../shellcheck/rules/shellcheck/SC3049.html | 7 +- .../shellcheck/rules/shellcheck/SC3050.html | 6 +- .../shellcheck/rules/shellcheck/SC3051.html | 20 ++- .../shellcheck/rules/shellcheck/SC3051.json | 2 +- .../shellcheck/rules/shellcheck/SC3052.html | 19 ++- .../shellcheck/rules/shellcheck/SC3052.json | 2 +- .../shellcheck/rules/shellcheck/SC3053.html | 8 +- .../shellcheck/rules/shellcheck/SC3054.html | 8 +- .../shellcheck/rules/shellcheck/SC3056.html | 6 +- .../shellcheck/rules/shellcheck/SC3057.html | 15 +- .../shellcheck/rules/shellcheck/SC3059.html | 2 +- .../shellcheck/rules/shellcheck/SC3060.html | 8 +- .../shellcheck/rules/shellcheck/SC3061.html | 1 + .../shellcheck/rules/shellcheck/SC3061.json | 14 ++ src/main/scripts/build_checks.py | 143 +++++++++-------- src/main/scripts/build_checks.sh | 4 + src/main/scripts/build_checks.yml | 145 ++++++------------ src/main/scripts/get_checks.sh | 2 +- 683 files changed, 4241 insertions(+), 2403 deletions(-) create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.json create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.html create mode 100644 src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.json diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1000.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1000.html index 8760d0b..713637d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1000.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1000.html @@ -1,15 +1,15 @@ -

$ is not used specially and should therefore be escaped.

+

$ is not used specially and should therefore be escaped.

+

Note: Removed in v0.3.3 - 2014-05-29

Problematic code

-
echo "$"
+
echo "$"
 

Correct code

-
echo "\$"
+
echo "\$"
 

Rationale

$ is special in double quotes, but there are some cases where it's interpreted literally:

    -
  1. Following a backslash: echo "\$"
  2. -
  3. In a context where the shell can't make sense of it, such as at the end of the string, ("foo$") or before some constructs ("$'foo'").
  4. +
  5. Following a backslash: echo "\$"
  6. +
  7. In a context where the shell can't make sense of it, such as at the end of the string, ("foo$") or before some constructs ("$'foo'").

To avoid relying on strange and shell-specific behavior, any $ intended to be literal should be escaped with a backslash.

-

Removed in v0.3.3 - 2014-05-29

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1001.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1001.html index a97a3a3..c0f4bd6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1001.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1001.html @@ -1,19 +1,25 @@

This \o will be a regular 'o' in this context.

Problematic code

-
# Want literal backslash
+
# Want literal backslash
 echo Yay \o/
 
 # Want other characters
 bell=\a
 

Correct code

-
echo 'Yay \o/'
+
echo 'Yay \o/'
 
-bell="$(printf '\a')"
+bell="$(printf '\a')"
 

Rationale

You have escaped something that has no special meaning when escaped. The backslash will be simply be ignored.

If the backslash was supposed to be literal, single quote or escape it.

If you wanted it to expand to something, rewrite the expression to use printf (or in bash, $'\t'). If the sequence in question is \n, \t or \r, you instead get a [[SC1012]] that describes this.

Exceptions

-

None. ShellCheck (as of 2017-07-03, commit 31bb02d6) will not warn when the first letter of a command is unnecessarily escaped, as this is frequently used to suppress aliases interactively.

+

None. ShellCheck (as of 2017-07-03, commit 31bb02d6) will not warn when the first letter of a command is unnecessarily escaped, as this is frequently used to suppress aliases interactively.

+

Related resources

+
    +
  • https://www.gnu.org/software/bash/manual/html_node/Aliases.html
  • +
  • https://www.gnu.org/software/bash/manual/html_node/Quoting.html
  • +
  • https://tiswww.case.edu/php/chet/bash/bash.html
  • +
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1003.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1003.html index 54ad62b..172dc29 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1003.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1003.html @@ -1,15 +1,16 @@ -

Want to escape a single quote? echo 'This is how it'\''s done'.

+

Want to escape a single quote? echo 'This is how it'\''s done'.

(Note: in v0.4.6, the error message was accidentally missing the backslash)

Problematic code

-
echo 'This is not how it\'s done'.
+
echo 'This is not how it\'s done'.
 

Correct code

-
echo 'This is how it'\''s done'.
+
echo 'This is how it'\''s done'.
 

Rationale

-

In POSIX shell, the shell cares about nothing but another single quote to terminate the quoted segment. Not even backslashes are interpreted.

+

In POSIX shell, the shell cares about nothing but another single-quote to terminate the quoted segment. Not even backslashes are interpreted.

POSIX.1 Shell Command Language § 2.2.2 Single Quotes:

-

Enclosing characters in single-quotes ( '' ) shall preserve the literal value of each character within the single-quotes. A single-quote cannot occur within single-quotes.

+
+

Enclosing characters in single-quotes ( '' ) shall preserve the literal value of each character within the single-quotes. A single-quote cannot occur within single-quotes.

Exceptions

-

If you want your single quoted string to end in a backslash, you can rewrite as 'string'\\ or [[ignore]] this warning.

+

If you want your single-quoted string to end in a backslash, you can rewrite as 'string'\\ or [[ignore]] this warning.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1004.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1004.html index 9b65b00..7270b14 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1004.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1004.html @@ -1,18 +1,18 @@

This backslash+linefeed is literal. Break outside single quotes if you just want to break the line.

-

(This warning was retired after 0.7.2 due to low signal-to-noise ratio)

+

(This warning was retired after v0.7.2 due to low signal-to-noise ratio)

Problematic code

-
var='This is long \
-piece of text'
+
var='This is long \
+piece of text'
 

Correct code

-
var='This is a long '\
-'piece of text'
+
var='This is a long '\
+'piece of text'
 

Rationale

-

You have a single quoted string containing a backslash followed by a linefeed (newline). Unlike double quotes or unquoted strings, this has no special meaning. The string will contain a literal backslash and a linefeed.

+

You have a single-quoted string containing a backslash followed by a linefeed (newline). Unlike double-quotes or unquoted strings, this has no special meaning. The string will contain a literal backslash and a linefeed.

If you wanted to break the line but not add a linefeed to the string, stop the single quote, break the line, and reopen it. This is demonstrated in the correct code.

If you wanted to break the line and also include the linefeed as a literal, you don't need a backslash:

-
var='This is a multi-line string
+
var='This is a multi-line string
 with an embedded linefeed'
 

Exceptions

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.html index dd103e9..22e5f5c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.html @@ -1,21 +1,24 @@ -

Remove space after = if trying to assign a value (or for empty string, use var='' ... ).

+

Remove space after = if trying to assign a value (or for empty string, use var="" ... ).

Problematic code

-
# I want programs to show text in dutch!
+
# I want programs to show text in Dutch!
 LANGUAGE= nl
 
-
# I want to run the nl command with English error messages!
+
# I want to run the nl command with English error messages!
 LANGUAGE= nl
 

Correct code

-
# I want programs to show text in dutch!
+
# I want programs to show text in Dutch!
 LANGUAGE=nl
 
-
# I want to run the nl command with English error messages!
-LANGUAGE='' nl
+
# I want to run the nl command with English error messages!
+LANGUAGE='' nl
 

Rationale

-

It's easy to think that LANGUAGE= nl would assign "nl" to the variable LANGUAGE. It doesn't.

-

Instead, it runs nl (the "number lines" command) and sets LANGUAGE to an empty string in its environment.

+

It's easy to think that LANGUAGE= nl would assign "nl" to the variable LANGUAGE. It doesn't.

+

Instead, it runs nl (the "number lines" command) and sets LANGUAGE to an empty string in its environment.

Since trying to assign values this way is a common mistake, ShellCheck warns about it and asks you to be explicit when assigning empty strings (except for IFS, due to the common IFS= read .. idiom).

Exceptions

If you're familiar with this behavior and feel that the explicit version is unnecessary, you can [[ignore]] it.

+

Related Resources

+

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html +https://mywiki.wooledge.org/BashPitfalls#foo_.3D_bar

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.json index 71cc093..9840713 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1007.json @@ -1,5 +1,5 @@ { - "title": "Remove space after = if trying to assign a value (or for empty string, use var='' ... ).", + "title": "Remove space after = if trying to assign a value (or for empty string, use var="" ... ).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1008.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1008.html index ca01a2c..e117fc5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1008.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1008.html @@ -1,10 +1,10 @@

This shebang was unrecognized. ShellCheck only supports sh/bash/dash/ksh. Add a 'shell' directive to specify.

Problematic code

-
#!/bin/mywrapper
+
#!/bin/mywrapper
 echo "Hello World"
 

Correct code

-
#!/bin/mywrapper
+
#!/bin/mywrapper
 # shellcheck shell=bash
 echo "Hello World"
 
@@ -14,7 +14,11 @@

Rationale

If this is a script in some other language, like #!/bin/sed for a sed script, then sorry -- ShellCheck does not support sed, awk, expect scripts. It only supports Bourne style shell scripts.

Exceptions

None.

-

Related resources

+

Related Resources

+

https://www.gnu.org/software/bash/manual/html_node/Shell-Scripts.html +https://mywiki.wooledge.org/BashProgramming?highlight=%28shebang%29#Shebang +https://www.gnu.org/software/gawk/manual/html_node/Executable-Scripts.html +https://mywiki.wooledge.org/BashPitfalls#On_UTF-8_and_Byte-Order_Marks_.28BOM.29

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1009.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1009.html index 5cbe31d..72e8116 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1009.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1009.html @@ -1,2 +1,2 @@

The mentioned parser error was in ...

-

This info warning points to the start of what ShellCheck was parsing when it failed. See [[Parser error]] for example and information.

+

This info warning points to the start of what ShellCheck was parsing when it failed. See [[Parser error]] for example and information.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.html index 0e7a036..39b59cf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.html @@ -1,19 +1,21 @@ -

Use semicolon or linefeed before 'done' (or quote to make it literal).

+

Use semicolon or linefeed before done (or quote to make it literal).

(or do then, fi, esac)

Problematic code

-
for f in *; do echo "$f" done
+
for f in *; do echo "$f" done
 

or

-
echo $f is done
+
echo $f is done
 

Correct code

-
for f in *; do echo "$f"; done
+
for f in *; do echo "$f"; done
 

or

-
echo "$f is done"
+
echo "$f is done"
 

Rationale

ShellCheck found a keyword like done, then, fi, esac, etc used as the argument of a command. This means that the shell will interpret it as a literal string rather than a shell keyword. To be interpreted as a keyword, it must be the first word in the line (i.e. after ;, & or a linefeed).

-

In the example, echo "$f" done is the same as echo "$f" "done", and the done does not terminate the loop. This is fixed by terminating the echo command with a ; so that the done is the first word in the next line.

+

In the example, echo "$f" done is the same as echo "$f" "done", and the done does not terminate the loop. This is fixed by terminating the echo command with a ; so that the done is the first word in the next line.

Exceptions

-

If you're intentionally using done as a literal, you can quote it to make this clear to shellcheck (and also human readers), e.g. instead of echo Task is done, use echo "Task is done". This makes no difference to the shell, but it will silence this warning.

+

If you're intentionally using done as a literal, you can quote it to make this clear to ShellCheck (and also human readers), e.g. instead of echo Task is done, use echo "Task is done". This makes no difference to the shell, but it will silence this warning.

+

From POSIX-2018, section "C.2.10 Shell Grammar," regarding the syntax, if (false) then (echo x) else (echo y) fi: +https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_10

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.json index e904c09..9f3cc76 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1010.json @@ -1,5 +1,5 @@ { - "title": "Use semicolon or linefeed before 'done' (or quote to make it literal).", + "title": "Use semicolon or linefeed before done (or quote to make it literal).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1011.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1011.html index b39629b..ef9be63 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1011.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1011.html @@ -1,15 +1,24 @@

This apostrophe terminated the single quoted string!

Problematic code

-
echo 'Nothing so needs reforming as other people's habits.'
+
echo 'Nothing so needs reforming as other peoples' habits.'
 

Correct code

-
echo 'Nothing so needs reforming as other people'\''s habits.'
+
echo 'Nothing so needs reforming as other peoples'\'' habits.'
 

or

-
echo "Nothing so needs reforming as other people's habits."
+
echo "Nothing so needs reforming as other peoples' habits."
 

Rationale

-

When writing a string in single quotes, you have to make sure that any apostrophes in the text don't accidentally terminate the single quoted string prematurely.

+

When writing a string in single-quotes, you have to make sure that any apostrophes in the text don't accidentally terminate the single-quoted string prematurely.

Escape them properly (see the correct code) or switch quotes to avoid the problem.

+

Additional options

+
echo '...peoples\ habits.'
+...peoples\ habits.
+
+
$ echo $'...peoples\x27 habits.'
+...peoples' habits.
+

Exceptions

None.

+

Related Resources

+

https://www.gnu.org/software/bash/manual/html_node/Quoting.html

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.html index 6bb9b5c..a74fcb7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.html @@ -1,28 +1,59 @@ -

\t is just literal t here. For tab, use "$(printf '\t')" instead.

+

\t is just literal t here. For tab, use "$(printf '\t')" instead.

Problematic code

-
# Want tab
-var=foo\tbar
+
# Want tab
+$ var=foo\tbar
+$ printf '<%s>\n' "$var"
+<footbar>
+
+$ var=foo\\tbar
+$ printf '<%s>\n' "$var"
+<foo\tbar>
+
 

or

-
# Want linefeed
-var=foo\nbar
+
# Want newline
+$ var=foo\nbar
+$ printf '<%s>\n' "$var"
+<foonbar>
+
+$ var=foo\\nbar
+$ printf '<%s>\n' "$var"
+<foo\nbar>
 

Correct code

-
var="foo$(printf '\t')bar"  # As suggested in warning
-var="$(printf 'foo\tbar')"  # Equivalent alternative
+
$ var="foo$(printf '\t')bar"  # As suggested in warning
+$ printf '<%s>\n' "$var"
+<foo	bar>
+
+$ var="$(printf 'foo\tbar')"  # Equivalent alternative
+$ printf '<%s>\n' "$var"
+<foo	bar>
 

or

-
# Literal, quoted linefeed
-line="foo
-bar"
+
$ # Literal, quoted newline
+$ line="foo
+> bar"
+$ printf '<%s>\n' "$line"
+<foo
+bar>
+
+

or

+
$ # Newline using ANSI-C quoting
+$ line=$'foo\nbar'
+$ printf '<%s>\n' "$line"
+<foo
+bar>
 

Rationale

-

ShellCheck has found a \t, \n or \r in a context where they just become regular letter t, n or r. Most likely, it was intended as a tab, linefeed or carriage return.

-

To generate such characters (plus other less common ones including \a, \f and octal escapes) , use printf as in the example. The exception is for linefeeds that would be stripped by command substitution; in these cases, use a literal quoted linefeed instead.

+

ShellCheck has found a \t, \n or \r in a context where they just become regular letters t, n or r. Most likely, it was intended as a tab, newline or carriage return.

+

To generate such characters (plus other less common ones including \a, \f and octal escapes) , use printf as in the example. The exception is for newliness that would be stripped by command substitution; in these cases, use a literal quoted newline instead.

Other characters like \z generate a [[SC1001]] info message, as the intent is less certain.

Exceptions

None.

-

Related resources

+

Related Resources

+

https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-printf +https://pubs.opengroup.org/onlinepubs/9799919799/utilities/printf.html +https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.json index 2f207db..d3694b5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1012.json @@ -1,5 +1,5 @@ { - "title": "\\t is just literal t here. For tab, use '$(printf '\\t')' instead.", + "title": "\\t is just literal t here. For tab, use "$(printf '\\t')" instead.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.html index 4dac973..0d73aa6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.html @@ -1,32 +1,62 @@ -

Use 'if cmd; then ..' to check exit code, or 'if [ "$(cmd)" = .. ]' to check output.

+

Use if cmd; then .. to check exit code, or if [ "$(cmd)" = .. ] to check output.

Problematic code

-
if [ grep -q pattern file ]
+
# WRONG
+if [ grep -q pattern file ]
 then
   echo "Found a match"
 fi
 
+
# WRONG
+if ! [[ logname == $LOGNAME ]]
+then
+    echo "Possible `su` shell"
+fi
+

Correct code

-
if grep -q pattern file
+
if grep -q pattern file
 then
   echo "Found a match"
 fi
 
+
if ! [[ $(logname) == $LOGNAME ]]
+then
+    echo "Possible `su` shell"
+fi
+

Rationale

-

[ .. ] is not part of shell syntax like if statements. It is not equivalent to parentheses in C-like languages, if (foo) { bar; }, and should not be wrapped around commands to test.

-

[ is just regular command, like whoami or grep, but with a funny name (see ls -l /bin/[). It's a shorthand for test.

-

If you want to check the exit status of a certain command, use that command directly as demonstrated in the correct code.

-

If you want to check the output of a command, use "$(..)" to get its output, and then use test or [/[[ to do a string comparison:

-
# Check output of `whoami` against the string `root`
+

[ ... ] as shell syntax is a simple command that tests for whether certain conditions are true or false, such as whether the value assigned to a variable has a non-zero length ([ -n "${foo}" ]) or whether a file system object is a directory ([ -d "${dir}" ]). If-then-(elif-then)-else-fi statements are logical constructs which themselves contain lists of commands which can include simple commands.

+

[ is just regular command, like whoami or grep, but with a funny name (see ls -l /bin/[). It's a shorthand for test. [[ is similar to both [ and test, but [[ offers some additional unary operators, such as '=~' the regular expression comparison operator. It allows one to use extglobs such as @(foo|bar) (a "bashism"), among some other less commonly used features.

+

[[, [ and test are often used within if...fi constructs in the conditional commands position: which is between the 'if' and the 'then.'

+

There are certain shell syntaxes which can be wrapped directly around simple commands, in particular:

+
    +
  • (1) { ...;}, group commands,
  • +
  • (2) $( ... ), command substitutions,
  • +
  • (3) <( ... ) and >( ... ), process substitutions,
  • +
  • (4) ( ... ), subshells, and
  • +
  • (5) $(( ... )) and (( ... )), arithmetic evaluations.
  • +
+

Some examples include:

+
    +
  • (1) { echo {a..z}; echo {0..9};} > ~/f,
  • +
  • (2) [[ $(logname) == $LOGNAME ]],
  • +
  • (3) readarray -t files < <( find ...),
  • +
  • (4) (cd /foo || exit 1; tar ...), and
  • +
  • (5) dd bs=$((2**12)) count=1 if=/dev/zero of=/tmp/zeroed-block, respectively.
  • +
+

Note how in example (2) logname is enclosed directly within a command substitution, which is itself enclosed within a [[ reserved word / conditional expression / compound command.

+

If you want to check the exit status of a certain command, use that command directly as demonstrated in the correct code, above.

+

If you want to check the output of a command, use "$(..)" to get its output, and then use test/[ or [[ to do a string comparison:

+
# Check output of `whoami` against the string `root`
 if [ "$(whoami)" = "root" ]
 then
   echo "Running as root"
 fi
 
-

For more information, see this problem in the Bash Pitfall list, or generally Tests and Conditionals in the WoolEdge BashGuide

Exceptions

None.

-

Related resources

+

Related Resources

+

For more information, see this problem in the Bash Pitfall list, or generally Tests and Conditionals in the wooledge.org BashGuide

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.json index 27e4edb..86fbdec 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1014.json @@ -1,5 +1,5 @@ { - "title": "Use 'if cmd; then ..' to check exit code, or 'if [ '$(cmd)' = .. ]' to check output.", + "title": "Use if cmd; then .. to check exit code, or if [ "$(cmd)" = .. ] to check output.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.html new file mode 100644 index 0000000..d902db1 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.html @@ -0,0 +1,16 @@ +

This is a Unicode double quote. Delete and retype it.

+

Problematic code

+
echo “hello world”
+
+

Correct code

+
echo "hello world"
+
+

Rationale

+

Blog software and word processors frequently replaces ASCII quotes "" with fancy Unicode quotes, “”. To Bash, Unicode quotes are considered regular literals and not quotes at all.

+

Simply delete them and retype them in your editor.

+

Status

+

This error was retired after 0.4.5. In this version and earlier, ShellCheck parsed slanted quotes as a valid double quote. This meant that the warning could not simply be ignored. It has since been replaced by [[SC1110]] (outside quotes) and [[SC1111]] (inside double-quotes).

+

Exceptions

+

If you really want literal Unicode double quotes, you can put them in single-quotes (or Unicode single-quotes in double-quotes) to make ShellCheck ignore them, e.g.,

+
printf 'Warning: “wakeonlan” is not installed.\n'
+
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.json new file mode 100644 index 0000000..65b0fde --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1015.json @@ -0,0 +1,14 @@ +{ + "title": "This is a Unicode double quote. Delete and retype it.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.html new file mode 100644 index 0000000..498402f --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.html @@ -0,0 +1,15 @@ +

This is a Unicode single quote. Delete and retype it.

+

Problematic code

+
echo ‘hello world’
+
+

Correct code

+
echo 'hello world'
+
+

Rationale

+

Some software, like macOS, Microsoft Word, and WordPress, may automatically replace your regular quotes with slanted Unicode quotes. Try deleting and retyping them, and/or disable “smart quotes” in your editor or OS.

+

Status

+

This error was retired after 0.4.5. In this version and earlier, ShellCheck parsed slanted quotes as a valid double-quote. This meant that the warning could not simply be ignored. It has since been replaced by [[SC1110]] (outside quotes) and [[SC1112]] (inside single-quotes).

+

Exceptions

+

If you want to use typographic single-quotes, you can put them in double-quotes (or typographic double-quotes in single-quotes) to make ShellCheck ignore them, e.g.,

+
printf "Warning: ‘wakeonlan’ is not installed.\n"
+
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.json new file mode 100644 index 0000000..02fe932 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1016.json @@ -0,0 +1,14 @@ +{ + "title": "This is a Unicode single quote. Delete and retype it.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1017.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1017.html index 192b97c..3f5d8e4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1017.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1017.html @@ -1,25 +1,27 @@

Literal carriage return. Run script through tr -d '\r' .

Problematic code

-
$ cat -v myscript
+
$ cat -v myscript
 #!/bin/sh^M
 echo "Hello World"^M
 

Correct code

-
$ cat -v myscript
+
$ cat -v myscript
 #!/bin/sh
 echo "Hello World"
 

Rationale

-

The script uses Windows/DOS style \r\n line terminators instead of UNIX style \n terminators. The additional \r aka ^M aka carriage return characters will be treated literally, and results in all sorts strange bugs and messages.

-

You can verify this with cat -v yourfile and see whether or not each line ends with a ^M. To delete them, open the file in your editor and save the file as "Unix", "UNIX/OSX Format", :set ff=unix or similar if it supports it.

+

The script uses Windows/MS-DOS style \r\n line terminators instead of Unix-style \n terminators. The additional \r aka ^M aka carriage return characters will be treated literally, and results in all sorts strange bugs and messages.

+

You can verify this with cat -v yourfile and see whether or not each line ends with a ^M. To delete them, open the file in your editor and save the file as "Unix", "Unix/macOS Format", :set ff=unix or similar if it supports it.

If you don't know how to get your editor to save a file with Unix line terminators, you can use tr:

-
tr -d '\r' < badscript   > goodscript
+
tr -d '\r' < badscript > goodscript
+# or
+dos2unix badscript
 

This will read a script badscript with possible carriage returns, and write goodscript without them.

Exceptions

None

Related resources

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.html index af3c8b6..09931ee 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.html @@ -1,4 +1,7 @@ -

This is a unicode non-breaking space. Delete it and retype as space.

-

You copy-pasted some code, probably from a blog or web site, which for formatting reasons contained unicode no-break spaces or unicode zero-width spaces instead of regular spaces or in words.

+

This is a Unicode non-breaking space. Delete it and retype as space.

+

You copy-pasted some code, probably from a blog or web site, which for formatting reasons contained Unicode no-break spaces or Unicode zero-width spaces instead of regular spaces or in words.

To humans, a zero-width space is invisible and a non-breaking space is indistinguishable from a regular space, but the shell does not agree.

-

If you have just a few, delete the indiciated space/word and retype it. If you have tons, do a search&replace in your editor (copy-paste an offending space into the search field, and type a regular space into the replace field), or use sed -e $'s/\xC2\xA0/ /g' -e $'s/\xE2\x80\x8b//g' -i yourfile to remove them.

+

If you have just a few, delete the indicated space/word and retype it. If you have tons, do a search-and-replace in your editor (copy-paste an offending space into the search field, and type a regular space into the replace field), or use the following command to remove them:

+
sed -e $'s/\xC2\xA0/ /g' -e $'s/\xE2\x80\x8b//g' -i yourfile
+
+

On macOS, a non-breaking space can be inserted into most programs by holding ⌥ Option+Space.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.json index a7473bb..6c8c4b6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1018.json @@ -1,5 +1,5 @@ { - "title": "This is a unicode non-breaking space. Delete it and retype as space.", + "title": "This is a Unicode non-breaking space. Delete it and retype as space.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1019.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1019.html index 7174ccc..8ecb522 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1019.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1019.html @@ -1,9 +1,9 @@

Expected this to be an argument to the unary condition.

Problematic code

-
[ -x ]
+
[ -x ]
 

Correct code

-
[ -x "myfile" ]
+
[ -x "myfile" ]
 

Rationale

ShellCheck has found a unary test operator that does not appear to be followed by a valid shell word.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.html index b58e50c..11ef07f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.html @@ -1,16 +1,15 @@ -

You need a space before the "]" or "]]"

+

You need a space before the ] or ]]

Problematic code

-
if [ "$STUFF" = ""]; then
+
if [ "$STUFF" = ""]; then
 

Correct code

-
if [ "$STUFF" = "" ]; then
+
if [ "$STUFF" = "" ]; then
 

Rationale

Bourne shells are very whitespace sensitive. Adding or removing spaces can drastically alter the meaning of a script. In these cases, ShellCheck has noticed that you're missing a space at the position indicated.

Exceptions

-

none.

+

None.

Ignore

-
# shellcheck disable=SC1020
-if [ "$STUFF" = ""]; then
-...
+
# shellcheck disable=SC1020
+if [ "$STUFF" = ""]; then
 
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.json index 80ecfbe..b459afc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1020.json @@ -1,5 +1,5 @@ { - "title": "You need a space before the ']' or ']]'", + "title": "You need a space before the ] or ]]", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1026.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1026.html index d3b8910..87d0a96 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1026.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1026.html @@ -1,10 +1,10 @@ -

If grouping expressions inside [[..]], use ( .. ).

+

If grouping expressions inside [[..]], use ( .. ).

Problematic code

-
[[ [ a || b ] && c ]]
+
[[ [ a || b ] && c ]]
 [ [ a -o b ] -a c ]]
 

Correct code

-
[[ ( a || b ) && c ]]
+
[[ ( a || b ) && c ]]
 [ \( a -o b \) -a c ]]  # or  { [ a ] || [ b ]; } && [ c ]
 

Rationale

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1027.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1027.html index 5575210..8342937 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1027.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1027.html @@ -1 +1,15 @@ -

Expected another argument for this operator.

\ No newline at end of file +

Expected another argument for this operator.

+

Problematic code

+
[ $a -ne ]
+
+

Correct code

+
[ $a -ne $b ]
+
+

Rationale

+

ShellCheck found a test operator without an operand. This could be a copy-paste fail, bad linebreak, or trying to use <> instead of != or -ne.

+

Exceptions

+

None.

+

Related resources

+
    +
  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
  • +
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1028.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1028.html index 33f7a27..bcb06d1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1028.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1028.html @@ -1,13 +1,13 @@ -

In [..] you have to escape \( \) or preferably combine [..] expressions.

+

In [..] you have to escape \( \) or preferably combine [..] expressions.

Problematic code

-
[ -e ~/.bashrc -a ( -x /bin/dash -o -x /bin/ash ) ]
+
[ -e ~/.bashrc -a ( -x /bin/dash -o -x /bin/ash ) ]
 

Correct code

In POSIX:

-
[ -e ~/.bashrc ] &&  { [ -x /bin/dash ] || [ -x /bin/ash ]; }
+
[ -e ~/.bashrc ] &&  { [ -x /bin/dash ] || [ -x /bin/ash ]; }
 

Obsolete XSI syntax:

-
[ -e ~/.bashrc -a \( -x /bin/dash -o -x /bin/ash \) ]
+
[ -e ~/.bashrc -a \( -x /bin/dash -o -x /bin/ash \) ]
 

Rationale

[ is implemented as a regular command, so ( is not special.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1029.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1029.html index 3c5adde..85c2a2c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1029.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1029.html @@ -1,9 +1,9 @@

In [[..]] you shouldn't escape ( or ).

Problematic code

-
[[ -e ~/.bashrc -a \( -x /bin/dash -o -x /bin/ash \) ]]
+
[[ -e ~/.bashrc && \( -x /bin/dash || -x /bin/ash \) ]]
 

Correct code

-
[[ -e ~/.bashrc -a ( -x /bin/dash -o -x /bin/ash ) ]]
+
[[ -e ~/.bashrc && ( -x /bin/dash || -x /bin/ash ) ]]
 

Rationale

You don't have to -- and can't -- escape ( or ) inside a [[ .. ]] expression like you do in [ .. ]. Just remove the escaping.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1033.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1033.html index ea56431..4273d91 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1033.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1033.html @@ -1,19 +1,19 @@

Test expression was opened with double [[ but closed with single ]. Make sure they match.

-

(or SC1034 for vice versa)

+

(or [[SC1034]] for vice versa)

Problematic code

-
[[ -z "$var" ]
+
[[ -z "$var" ]
 

Correct code

-
[[ -z "$var" ]]
+
[[ -z "$var" ]]
 

Rationale

ShellCheck found a test expression [ ... ] (POSIX) or [[ ... ]] (ksh/bash), but where the opening and closing brackets did not match (i.e. [[ .. ] or [ .. ]]). The brackets need to match up to work.

Note in particular that [..] do not work like parentheses in other languages. You can not do:

-
# Invalid
+
# Invalid
 [[ x ] || [ y ]]
 

You would instead use two separate test expressions joined by ||:

-
# Valid basic test expressions (sh/bash/ksh)
+
# Valid basic test expressions (sh/bash/ksh)
 [ x ] || [ y ]
 
 # Valid extended test expressions (bash/ksh)
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1035.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1035.html
index 59e0ec8..1879a1b 100644
--- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1035.html
+++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1035.html
@@ -1,9 +1,9 @@
 

You need a space here

Problematic code

-
if ![-z foo ]; then true; fi # if command `[-z' w/ args `foo', `]' fails..
+
if ![-z foo ]; then true; fi # if command `[-z' w/ args `foo', `]' fails..
 

Correct code

-
if ! [ -z foo ]; then true; fi # if command `[' w/ args `-z', `foo', `]' fails..
+
if ! [ -z foo ]; then true; fi # if command `[' w/ args `-z', `foo', `]' fails..
 

Rationale

Bourne shells are very whitespace sensitive. Adding or removing spaces can drastically alter the meaning of a script. In these cases, ShellCheck has noticed that you're missing a space at the position indicated.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1036.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1036.html index 8a8951f..d05c57a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1036.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1036.html @@ -1,10 +1,10 @@

( is invalid here. Did you forget to escape it?

Problematic code

-
echo (foo) bar
+
echo (foo) bar
 

Correct code

Depends on your intention:

-
echo "(foo) bar"  # Literal parentheses
+
echo "(foo) bar"  # Literal parentheses
 echo "$(foo) bar" # Command expansion
 echo "foo bar"    # Tried to use parentheses for grouping or function invocation
 
@@ -13,16 +13,16 @@

Rationale

Determine what you intended the parenthesis to do and rewrite accordingly. Common issues include:

  • Wanting them to be literal, as in echo (FAIL) Some tests failed. In this case, it requires quoting.
  • -
  • Wanting command expansion, as in echo Today is (date). Add the missing $: echo "Today is $(date)"
  • +
  • Wanting command expansion, as in echo Today is (date). Add the missing $: echo "Today is $(date)"
  • Adding parentheses because other languages need them in that context, such as foo (bar, 42) to call a function. This should be foo bar 42. Also, shells do not support tuples or passing arrays as single parameters.

Exceptions

Bash allows some parentheses as part of assignment-like tokens to certain commands, including export and eval. This is a workaround in Bash to allow commands that normally would not be valid:

-
eval foo=(bar)       # Valid command
+
eval foo=(bar)       # Valid command
 echo foo=(bar)       # Invalid syntax
 f=foo; eval $f=(bar) # Also invalid
 
-

In these cases, please quote the command, such as `eval "foo=(bar)". This does not change the behavior, but stops relying on bash specific parsing quirks.

+

In these cases, please quote the command, such as eval "foo=(bar)". This does not change the behavior, but stops relying on Bash-specific parsing quirks.

Related resources

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
  • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1037.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1037.html index 0a2b44a..67a42ba 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1037.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1037.html @@ -1,24 +1,24 @@ -

    Braces are required for positionals over 9, e.g. ${10}.

    +

    Braces are required for positionals over 9, e.g. ${10}.

    Problematic code

    -
    echo "Ninth parameter: $9"
    +
    echo "Ninth parameter: $9"
     echo "Tenth parameter: $10"
     

    Correct code

    -
    echo "Ninth parameter: $9"
    +
    echo "Ninth parameter: $9"
     echo "Tenth parameter: ${10}"
     

    Rationale

    For legacy reasons, $10 is interpreted as the variable $1 followed by the literal string 0.

    Curly braces are needed to tell the shell that both digits are part of the parameter expansion.

    Exceptions

    -

    If you wanted the trailing digits to be literal, ${1}0 will make this clear to both humans and shellcheck.

    +

    If you wanted the trailing digits to be literal, ${1}0 will make this clear to both humans and ShellCheck.

    In dash, $10 is (wrongly) interpreted as ${10}, so some 'reversed' care should also be taken:

    -
    bash -c 'set a b c d e f g h i j; echo $10 ${1}0' # POSIX: a0 a0
    -dash -c 'set a b c d e f g h i j; echo $10 ${1}0' # WRONG: j a0
    +
    bash -c 'set a b c d e f g h i j; echo $10 ${1}0' # POSIX: a0 a0
    +dash -c 'set a b c d e f g h i j; echo $10 ${1}0' # WRONG: j a0
     

    Related resources

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.html index 8a06afd..fd4875e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.html @@ -1,12 +1,12 @@ -

    Shells are space sensitive. Use '< <(cmd)', not '<<(cmd)'.

    +

    Shells are space sensitive. Use < <(cmd), not <<(cmd).

    Problematic code

    -
    while IFS= read -r line
    +
    while IFS= read -r line
     do
       printf "%q\n" "$line"
     done <<(curl -s http://example.com)
     

    Correct code

    -
    while IFS= read -r line
    +
    while IFS= read -r line
     do
       printf "%q\n" "$line"
     done <  <(curl -s http://example.com)
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.json
    index 9d3ebd7..ca97354 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1038.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Shells are space sensitive. Use '< <(cmd)', not '<<(cmd)'.",
    +  "title": "Shells are space sensitive. Use < <(cmd), not <<(cmd).",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1039.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1039.html
    index 54f5325..23d41a1 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1039.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1039.html
    @@ -1,18 +1,18 @@
     

    Remove indentation before end token (or use <<- and indent with tabs).

    Problematic code

    -
    for f in *.png
    +
    for f in *.png
     do
    -  cat << EOF
    +  cat << HTML
          <img src="$f" /><br/>
    -  EOF
    +  HTML
     done > index.html
     

    Correct code

    -
    for f in *.png
    +
    for f in *.png
     do
    -  cat << EOF
    +  cat << HTML
          <img src="$f" /><br/>
    -EOF
    +HTML
     done > index.html
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1040.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1040.html index d224966..feae993 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1040.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1040.html @@ -1,18 +1,18 @@ -

    When using <<-, you can only indent with tabs.

    +

    When using <<-, you can only indent with tabs.

    Problematic code

    Any code using <<- that is indented with spaces. cat -T script shows

    -
        cat <<- foo
    +
        cat <<- foo
             Hello world
         foo
     

    Correct code

    Code using <<- must be indented with tabs. cat -T script shows

    -
    ^Icat <<- foo
    +
    ^Icat <<- foo
     ^I^IHello world
     ^Ifoo
     

    Or simply don't indent the end token:

    -
        cat <<- foo
    +
        cat <<- foo
           Hello World
     foo
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.html index 0f9fc7b..a744fc8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.html @@ -1,13 +1,12 @@ -

    Found 'eof' further down, but not on a separate line.

    -
    Close matches include '-eof' (!= 'eof').
    -
    +

    Found eof further down, but not on a separate line.

    +
    Close matches include '-eof' (!= 'eof').

    Problematic code

    -
    cat <<-eof
    +
    cat <<-eof
     Hello World
     -eof
     

    Correct code

    -
    cat <<- eof
    +
    cat <<- eof
     Hello World
     eof
     
    @@ -15,7 +14,7 @@

    Rationale

    Your here document isn't properly terminated.

    There is a line containing the terminator you've chosen, but it's not by itself on a separate line.

    In the example code, the script uses <<-eof, which is the operator <<- followed by eof. The script therefore looks for eof and skips right past the intended terminator because it starts with a dash.

    -

    You will get some companion SC1042 errors mentioning lines that contain the string as a substring, though they all point to the start of the here document and not the relevant line:

    +

    You will get some companion [[SC1042]] errors mentioning lines that contain the string as a substring, though they all point to the start of the here document and not the relevant line:

    In foo line 4:
     Hello
     ^-- SC1041: Found 'eof' further down, but not on a separate line.
    @@ -26,5 +25,5 @@ 

    Rationale

    Try to change the line ending into LF.

    Exceptions

    None.

    -
    -

    Note that SC1041 and SC1042 swapped numbers after v0.4.6 to improve the display order. This rare instance of number reuse was justified by them always occuring together on the same line.

    +
    +

    Note that SC1041 and [[SC1042]] swapped numbers after v0.4.6 to improve the display order. This rare instance of number reuse was justified by them always occurring together on the same line.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.json index 10c5b55..6417468 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1041.json @@ -1,5 +1,5 @@ { - "title": "Found 'eof' further down, but not on a separate line.", + "title": "Found eof further down, but not on a separate line.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.html index 8e4d984..e7c2418 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.html @@ -1 +1,19 @@ -

    Found end token `EOF' further down, but with wrong casing.

    \ No newline at end of file +

    Found EOF further down, but with wrong casing.

    +

    Problematic code

    +
    cat << EOF
    +Hello World
    +Eof
    +
    +

    Correct code

    +
    cat << EOF
    +Hello World
    +EOF
    +
    +

    Rationale

    +

    ShellCheck found a here document (<<) where the end token is missing. However, the end token appears with different case further down. If this was meant to be the end of the here document, make sure the case matches.

    +

    Exceptions

    +

    None. This error is only emitted when the here document is incomplete.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.json index b10e148..a5f5c5b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1043.json @@ -1,5 +1,5 @@ { - "title": "Found end token `EOF' further down, but with wrong casing.", + "title": "Found EOF further down, but with wrong casing.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.html index 7dea1fb..d10936f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.html @@ -1,18 +1,18 @@ -

    Couldn't find end token `EOF' in the here document.

    +

    Couldn't find end token EOF in the here document.

    Problematic code

    -
    cat << EOF
    +
    cat << EOF
       Hello World
     

    Correct code

    -
    cat << EOF
    +
    cat << EOF
       Hello World
     EOF
     

    Rationale

    The << here document (aka heredoc) was not properly terminated. The terminating token needs to be on a separate line without indenting (or indented with tabs only when using <<-).

    Note that you can not put here documents in one liners. For such use cases, use a <<< here string:

    -
    cat << EOF  hello world  EOF   # Wrong: data and terminator can not be on the same line
    -cat <<< "hello world"          # Correct
    +
    cat << EOF  hello world  EOF   # Wrong: data and terminator can not be on the same line
    +cat <<< "hello world"          # Correct
     

    Exceptions

    None

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.json index 6f613c6..58c90a2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1044.json @@ -1,5 +1,5 @@ { - "title": "Couldn't find end token `EOF' in the here document.", + "title": "Couldn't find end token EOF in the here document.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.html index d7ef277..fa2c087 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.html @@ -1,9 +1,9 @@ -

    It's not 'foo &; bar', just 'foo & bar'.

    +

    It's not foo &; bar, just foo & bar.

    Problematic code

    -
    foo &; bar
    +
    foo &; bar
     

    Correct code

    -
    foo & bar
    +
    foo & bar
     

    Rationale

    Both & and ; terminate the command. You should only use one of them.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.json index 2a13c4d..6f707e1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1045.json @@ -1,5 +1,5 @@ { - "title": "It's not 'foo &; bar', just 'foo & bar'.", + "title": "It's not foo &; bar, just foo & bar.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.html index da9a7ad..d1f2c1d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.html @@ -1,12 +1,12 @@ -

    Couldn't find 'fi' for this 'if'.

    +

    Couldn't find fi for this if.

    Problematic code

    -
    if true
    +
    if true
     then
       echo "True"
     done
     

    Correct code

    -
    if true
    +
    if true
     then
       echo "True"
     fi
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.json
    index 450af8a..78392c8 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1046.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Couldn't find 'fi' for this 'if'.",
    +  "title": "Couldn't find fi for this if.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.html
    index 5cc7a6b..5048bec 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.html
    @@ -1,2 +1,2 @@
    -

    "Expected 'fi' matching previously mentioned 'if'.

    +

    Expected fi matching previously mentioned if.

    See companion warning [[SC1046]].

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.json index 938f394..65447cd 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1047.json @@ -1,5 +1,5 @@ { - "title": "'Expected 'fi' matching previously mentioned 'if'.", + "title": "Expected fi matching previously mentioned if.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.html index eb6aec5..f49aad3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.html @@ -1,12 +1,12 @@ -

    Can't have empty then clauses (use 'true' as a no-op).

    +

    Can't have empty then clauses (use true as a no-op).

    Problematic code

    -
    if [ -e foo ]
    +
    if [ -e foo ]
     then
       # TODO: handle this
     fi
     

    Correct code

    -
    if [ -e foo ]
    +
    if [ -e foo ]
     then
       # TODO: handle this
       true
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.json
    index fdd38b1..fcbd973 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1048.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Can't have empty then clauses (use 'true' as a no-op).",
    +  "title": "Can't have empty then clauses (use true as a no-op).",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.html
    index 472ed5d..131b22c 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.html
    @@ -1,13 +1,13 @@
    -

    Did you forget the 'then' for this 'if'?

    +

    Did you forget the then for this if?

    Problematic code

    -
    if true
    +
    if true
       echo "foo"
     elif true
       echo "bar"
     fi
     

    Correct code

    -
    if true
    +
    if true
     then
       echo "foo"
     elif true
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.json
    index a6c652f..13bafd5 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1049.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Did you forget the 'then' for this 'if'?",
    +  "title": "Did you forget the then for this if?",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.html
    index 907620b..2ca2f90 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.html
    @@ -1,11 +1,11 @@
    -

    Expected 'then'.

    +

    Expected then.

    Problematic code

    -
    if true
    +
    if true
       echo "True"
     fi
     

    Correct code

    -
    if true
    +
    if true
     then
       echo "True"
     fi
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.json
    index 2ec95ff..74646b3 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1050.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Expected 'then'.",
    +  "title": "Expected then.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.html
    index a51e204..36fbd53 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.html
    @@ -1,9 +1,9 @@
    -

    Semicolons directly after 'then' are not allowed. Just remove it.

    +

    Semicolons directly after then are not allowed. Just remove it.

    Problematic code

    -
    if true; then; echo "Hi"; fi
    +
    if true; then; echo "Hi"; fi
     

    Correct code

    -
    if true; then echo "Hi"; fi
    +
    if true; then echo "Hi"; fi
     

    Rationale

    then keywords should not be followed by semicolons. It's not valid shell syntax.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.json index 85baf18..7c7bd72 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1051.json @@ -1,5 +1,5 @@ { - "title": "Semicolons directly after 'then' are not allowed. Just remove it.", + "title": "Semicolons directly after then are not allowed. Just remove it.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.html index a51e204..36fbd53 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.html @@ -1,9 +1,9 @@ -

    Semicolons directly after 'then' are not allowed. Just remove it.

    +

    Semicolons directly after then are not allowed. Just remove it.

    Problematic code

    -
    if true; then; echo "Hi"; fi
    +
    if true; then; echo "Hi"; fi
     

    Correct code

    -
    if true; then echo "Hi"; fi
    +
    if true; then echo "Hi"; fi
     

    Rationale

    then keywords should not be followed by semicolons. It's not valid shell syntax.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.json index 85baf18..7c7bd72 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1052.json @@ -1,5 +1,5 @@ { - "title": "Semicolons directly after 'then' are not allowed. Just remove it.", + "title": "Semicolons directly after then are not allowed. Just remove it.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.html index da54cf7..249c14a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.html @@ -1,9 +1,9 @@ -

    Semicolons directly after 'else' are not allowed. Just remove it.

    +

    Semicolons directly after else are not allowed. Just remove it.

    Problematic code

    -
    if mycommand; then echo "True"; else; echo "False"; fi
    +
    if mycommand; then echo "True"; else; echo "False"; fi
     

    Correct code

    -
    if mycommand; then echo "True"; else echo "False"; fi
    +
    if mycommand; then echo "True"; else echo "False"; fi
     

    Rationale

    else keywords should not be followed by semicolons. It's not valid shell syntax.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.json index 043659b..7f2ab38 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1053.json @@ -1,5 +1,5 @@ { - "title": "Semicolons directly after 'else' are not allowed. Just remove it.", + "title": "Semicolons directly after else are not allowed. Just remove it.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.html index 0f11857..de3bed0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.html @@ -1,12 +1,12 @@ -

    You need a space after the '{'.

    +

    You need a space after the {.

    Problematic code

    -
    foo() {echo "hello world;}
    +
    foo() {echo "hello world"; }
     

    Correct code

    -
    foo() { echo "hello world;}
    +
    foo() { echo "hello world"; }
     

    Rationale

    { is only recognized as the start of a command group when it's a separate token.

    -

    If it's not a separate token, like in the problematic example, it will be considered a literal character, as if writing "{echo" with quotes, and therefore usually cause a syntax error.

    +

    If it's not a separate token, like in the problematic example, it will be considered a literal character, as if writing "{echo" with quotes, and therefore usually cause a syntax error.

    Exceptions

    None.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.json index 82b687f..1c75e2f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1054.json @@ -1,5 +1,5 @@ { - "title": "You need a space after the '{'.", + "title": "You need a space after the {.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.html index 3d171b0..5d9cdda 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.html @@ -1 +1,21 @@ -

    You need at least one command here. Use 'true;' as a no-op.

    \ No newline at end of file +

    You need at least one command here. Use true; as a no-op.

    +

    Problematic code

    +
    submitbug() {
    +  # TODO: Implement me
    +}
    +
    +

    Correct code

    +
    submitbug() {
    +  # TODO: Implement me
    +  true
    +}
    +
    +

    Rationale

    +

    ShellCheck found an empty code block. This could be an empty function as shown, a loop with an empty body, or similar.

    +

    Sh/bash does not allow empty code blocks. Insert at least one command. If you don't want the block to do anything, true (aka :) is a good no-op.

    +

    Exceptions

    +

    None

    +

    Related resources

    + diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.json index 379f947..34726d1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1055.json @@ -1,5 +1,5 @@ { - "title": "You need at least one command here. Use 'true;' as a no-op.", + "title": "You need at least one command here. Use true; as a no-op.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.html index 332811b..7829276 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.html @@ -1,12 +1,12 @@ -

    Expected a '}'. If you have one, try a ; or \n in front of it.

    +

    Expected a }. If you have one, try a ; or \n in front of it.

    Problematic code

    -
    #!/bin/bash
    +
    #!/bin/bash
     bar() { echo "hello world" }
     

    Correct code

    -
    #!/bin/bash
    +
    #!/bin/bash
     bar() { echo "hello world";}
     

    Rationale

    } is only recognized as the end of a command group when it's a separate token.

    -

    If it's not a separate token, like in the problematic example, it will be considered a literal character, as if writing echo "foo}" with quotes, and therefore usually cause a syntax error.

    +

    If it's not a separate token, like in the problematic example, it will be considered a literal character, as if writing echo "foo}" with quotes, and therefore usually cause a syntax error.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.json index 536da73..861055d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1056.json @@ -1,5 +1,5 @@ { - "title": "Expected a '}'. If you have one, try a ; or \\n in front of it.", + "title": "Expected a }. If you have one, try a ; or \\n in front of it.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.html index 9a42ebe..9baa100 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.html @@ -1 +1,20 @@ -

    Did you forget the 'do' for this loop?

    \ No newline at end of file +

    Did you forget the do for this loop?

    +

    Problematic code

    +
    while read -r line
    +  echo $line
    +done
    +
    +

    Correct code

    +
    while read -r line
    +do
    +  echo $line
    +done
    +
    +

    Rationale

    +

    ShellCheck found a loop that appears to be missing its do statement. Make sure the loop syntax is correct.

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.json index 08c9b47..436235f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1057.json @@ -1,5 +1,5 @@ { - "title": "Did you forget the 'do' for this loop?", + "title": "Did you forget the do for this loop?", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1058.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1058.html index e8dee2d..1a733de 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1058.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1058.html @@ -1,11 +1,11 @@

    Expected do.

    Problematic code

    -
    for file in *
    +
    for file in *
       echo "$file"
     done
     

    Correct code

    -
    for file in *
    +
    for file in *
     do
       echo "$file"
     done
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.html
    index 6a30164..ac7a38c 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.html
    @@ -1 +1,25 @@
    -

    No semicolons directly after 'do'.

    \ No newline at end of file +

    Semicolon is not allowed directly after do. You can just delete it.

    +

    Problematic code

    +
    while true; do; true; done
    +
    +while true;
    +do;
    +  true;
    +done;
    +
    +

    Correct code

    +
    while true; do true; done
    +
    +while true;
    +do
    +  true;
    +done;
    +
    +

    Rationale

    +

    Semicolon ; is not allowed directly after a do keyword. Follow it directly with either a command or a linefeed as shown in the example.

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.json index e2536c5..0fafa9c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1059.json @@ -1,5 +1,5 @@ { - "title": "No semicolons directly after 'do'.", + "title": "Semicolon is not allowed directly after do. You can just delete it.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.html index bc94d19..6ae5a75 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.html @@ -1 +1,19 @@ -

    Can't have empty do clauses (use 'true' as a no-op).

    \ No newline at end of file +

    Can't have empty do clauses (use true as a no-op)

    +

    Problematic code

    +
    for i in 1 2 3; do
    +done
    +
    +

    Correct code

    +
    for i in 1 2 3; do
    +  true
    +done
    +
    +

    Rationale

    +

    An empty do ... done block is not valid. +Use true or : if you need no command at all.

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.json index aa30ff1..666288c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1060.json @@ -1,5 +1,5 @@ { - "title": "Can't have empty do clauses (use 'true' as a no-op).", + "title": "Can't have empty do clauses (use true as a no-op)", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.html index ffc3af9..878c2b2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.html @@ -1,13 +1,13 @@ -

    Couldn't find 'done' for this 'do'.

    +

    Couldn't find done for this do.

    Problematic code

    -
    yes() {
    +
    yes() {
       while echo "y"
       do
         true
     }
     

    Correct code

    -
    yes() {
    +
    yes() {
       while echo "y"
       do
         true
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.json
    index 212e5ef..b683855 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1061.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Couldn't find 'done' for this 'do'.",
    +  "title": "Couldn't find done for this do.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.html
    index f834ce1..287e57a 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.html
    @@ -1,2 +1,2 @@
    -

    Expected 'done' matching previously mentioned 'do'.

    +

    Expected done matching previously mentioned do.

    See companion warning [[SC1061]]

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.json index d7bbf61..c3cefad 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1062.json @@ -1,5 +1,5 @@ { - "title": "Expected 'done' matching previously mentioned 'do'.", + "title": "Expected done matching previously mentioned do.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.html index 2971ff7..d647929 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.html @@ -1 +1,31 @@ -

    You need a line feed or semicolon before the 'do'.

    \ No newline at end of file +

    You need a line feed or semicolon before the do.

    +

    Problematic code

    +
    for file in * do
    +  echo "$file"
    +done
    +
    +

    Correct code

    +
    for file in *; do
    +  echo "$file"
    +done
    +
    +# or 
    +
    +for file in *
    +do
    +  echo "$file"
    +done
    +
    +

    Rationale

    +

    ShellCheck found a do on the same line as a loop, but do only starts a loop block at the start of a line/statement. Make the do the start of a new line/statement by inserting a linefeed or semicolon in front of it.

    +

    Exceptions

    +

    If you wanted to treat do as a literal string, you can quote it to make this clear to ShellCheck and humans:

    +
    for f in "for" "do" "done"
    +do
    +  echo "Shell keywords include: $f"
    +done
    +
    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.json index 534ce1e..6e9c80f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1063.json @@ -1,5 +1,5 @@ { - "title": "You need a line feed or semicolon before the 'do'.", + "title": "You need a line feed or semicolon before the do.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1064.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1064.html index b09ca5f..0c0241e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1064.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1064.html @@ -1,12 +1,12 @@ -

    Expected a { to open the function definition.

    +

    Expected a { to open the function definition.

    Problematic code

    -
    foo() {
    +
    foo() {
       echo "hello world"
     }
     foo()
     

    Correct code

    -
    foo() {
    +
    foo() {
       echo "hello world"
     }
     foo
    @@ -18,7 +18,7 @@ 

    Rationale

    If you are trying to do something else, look up the syntax for what you are trying to do.

    Exceptions

    POSIX allows the body of a function to be any compound command, e.g. foo() for i; do :; done. Since this usage is rare, ShellCheck intentionally requires the body to be { ..; } (or ( ..; )):

    -
    foo() {
    +
    foo() {
       for i; do :; done
     }
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.html index c11ad7a..fb6db94 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.html @@ -1,12 +1,12 @@ -

    Trying to declare parameters? Don't. Use () and refer to params as $1, $2..

    +

    Trying to declare parameters? Don't. Use () and refer to params as $1, $2, …

    Problematic code

    -
    foo(input) {
    +
    foo(input) {
       echo "$input"
     }
     foo("hello world");
     

    Correct code

    -
    foo() {
    +
    foo() {
       echo "$1"
     }
     foo "hello world"
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.json
    index 9481663..b628e22 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1065.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Trying to declare parameters? Don't. Use () and refer to params as $1, $2..",
    +  "title": "Trying to declare parameters? Don't. Use () and refer to params as $1, $2, \u2026",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1066.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1066.html
    index b7c6ef2..463cab2 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1066.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1066.html
    @@ -1,17 +1,17 @@
    -

    Don't use $ on the left side of assignments.

    +

    Don't use $ on the left side of assignments.

    Problematic code

    -
    $greeting="Hello World"
    +
    $greeting="Hello World"
     

    Correct code

    -
    greeting="Hello World"
    +
    greeting="Hello World"
     

    Alternatively, if the goal was to assign to a variable whose name is in another variable (indirection), use declare:

    -
    name=foo
    +
    name=foo
     declare "$name=hello world"
     echo "$foo"
     

    Or if you actually wanted to compare the value, use a test expression:

    -
    if [ "$greeting" = "hello world" ]
    +
    if [ "$greeting" = "hello world" ]
     then
       echo "Programmer, I presume?"
     fi
    @@ -19,4 +19,4 @@ 

    Correct code

    Rationale

    Unlike Perl or PHP, $ is not used when assigning to a variable.

    Exceptions

    -

    If you wanted to

    +

    None.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.html index af51536..eb18279 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.html @@ -1,35 +1,35 @@ -

    For indirection, use arrays, declare "var$n=value", or (for sh) read/eval

    +

    For indirection, use arrays, declare "var$n=value", or (for sh) read/eval

    Problematic code

    -
    n=1
    +
    n=1
     var$n="hello"
     

    Correct code

    For integer indexing in ksh/bash, consider using an indexed array:

    -
    n=1
    +
    n=1
     var[n]="hello"
     echo "${var[n]}"
     

    For string indexing in ksh/bash, use an associative array:

    -
    typeset -A var
    +
    typeset -A var
     n="greeting"
     var[$n]="hello"
     echo "${var[$n]}"
     

    If you actually need a variable with the constructed name in bash, use declare:

    -
    n="Foo"
    +
    n="Foo"
     declare "var$n=42"
     echo "$varFoo"
     

    For sh, with single line contents, consider read:

    -
    n="Foo"
    +
    n="Foo"
     read -r "var$n" << EOF
     hello
     EOF
     echo "$varFoo"
     

    or with careful escaping, eval:

    -
    n=Foo
    -eval "var$n='hello'"
    +
    n=Foo
    +eval "var$n='hello'"
     echo "$varFoo"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.json index c68b195..d60dff8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1067.json @@ -1,5 +1,5 @@ { - "title": "For indirection, use arrays, declare 'var$n=value', or (for sh) read\/eval", + "title": "For indirection, use arrays, declare "var$n=value", or (for sh) read\/eval", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1068.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1068.html index 257f03b..a7acb68 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1068.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1068.html @@ -1,11 +1,11 @@ -

    Don't put spaces around the = in assignments.

    +

    Don't put spaces around the = in assignments.

    Problematic code

    -
    foo = 42
    +
    foo = 42
     

    Correct code

    -
    foo=42
    +
    foo=42
     

    Rationale

    Shells are space sensitive. foo=42 means to assign 42 to the variable foo. foo = 42 means to run a command named foo, and pass = as $1 and 42 as $2.

    Exceptions

    -

    If you actually wanted to run a command named foo and provide = as the first argument, simply quote it to make ShellCheck be quiet: foo "=" 42.

    +

    If you actually wanted to run a command named foo and provide = as the first argument, simply quote it to make ShellCheck be quiet: foo "=" 42.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1069.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1069.html index 49dd9ca..bac71b8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1069.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1069.html @@ -1,10 +1,10 @@ -

    You need a space before the [.

    +

    You need a space before the [.

    Problematic code

    -
    if[ -e file ]
    +
    if[ -e file ]
     then echo "exists"; fi
     

    Correct code

    -
    if [ -e file ]
    +
    if [ -e file ]
     then echo "exists"; fi
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1070.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1070.html index bd97bd9..7f359ac 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1070.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1070.html @@ -1,5 +1,5 @@

    Parsing stopped here. Mismatched keywords or invalid parentheses?

    Rationale

    -

    Shellcheck found a syntax error at the indicated location. Barring a bug in Shellcheck itself, your shell will also crash with a syntax error at the same location, so you cannot ignore this check.

    +

    ShellCheck found a syntax error at the indicated location. Barring a bug in ShellCheck itself, your shell will also crash with a syntax error at the same location, so you cannot ignore this check.

    Exceptions

    None.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1071.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1071.html index 0dba38f..3f52682 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1071.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1071.html @@ -1,6 +1,6 @@

    ShellCheck only supports sh/bash/dash/ksh scripts. Sorry!

    Problematic code

    -
    #!/usr/bin/python
    +
    #!/usr/bin/python
     print "Hello"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1072.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1072.html index c9ed6de..d543302 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1072.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1072.html @@ -1,3 +1,13 @@

    Unexpected ..

    Note: There is a known bug in the current version when directives appear within then clauses of if blocks that causes Shellcheck to report SC1072 on otherwise valid code. Avoid using directives within then clauses - instead place them at the top of the if block or another enclosing block. This is fixed on the online version and the next release.

    See Parser Error.

    +

    This error can also occur with an incomplete shellcheck directive like # shellcheck disable instead of # shellcheck disable=all

    +

    Problematic code

    +
    # shellcheck disable
    +echo stuff that shellcheck up to at least v0.10.0 will not even see because of the incorrect directive above
    +
    +

    Correct code

    +
    # shellcheck disable=all
    +echo stuff that shellcheck will correctly ignore entirely
    +
    +

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1074.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1074.html index 7d41407..24d46e8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1074.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1074.html @@ -1,6 +1,6 @@

    Did you forget the ;; after the previous case item?

    Problematic code

    -
    while getoptions f option
    +
    while getoptions f option
     do
       case "${options}"
       in
    @@ -10,11 +10,11 @@ 

    Problematic code

    done

    Correct code

    -
    while getoptions f option
    +
    while getoptions f option
     do
       case "${options}"
       in
    -  f) FTR="${ARG}"
    +  f) FTR="${ARG}";;
         \?) exit;;
       esac
     done
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.html
    index ae87ddf..ce7de95 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.html
    @@ -1,15 +1,16 @@
    -

    Use 'elif' instead of 'else if'.

    +

    Use elif instead of else if.

    Problematic code

    -
    if [ "$#" -eq 0 ]
    +
    if [ "$#" -eq 0 ]
     then
       echo "Usage: ..."
     else if [ "$#" -lt 2 ]
     then
       echo "Missing operand"
     fi
    +  
     

    Correct code

    -
    if [ "$#" -eq 0 ]
    +
    if [ "$#" -eq 0 ]
     then
       echo "Usage: ..."
     elif [ "$#" -lt 2 ]
    @@ -22,7 +23,7 @@ 

    Rationale

    Exceptions

    else if is a valid (though confusing) way of nesting an if statement in a parent's else. If this is your intention, consider using canonical formatting by putting a linefeed between else and if.

    This does not change the behavior of the script, but merely makes it more obvious to ShellCheck (and other humans) that you didn't expect the else if to behave the way it does in C. Alternatively, you can [[ignore]] it with no ill effects.

    -
    
    +
    
     if x
     then
       echo "x"
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.json
    index 478c93f..a95afbe 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1075.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Use 'elif' instead of 'else if'.",
    +  "title": "Use elif instead of else if.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.html
    index cb1e54f..a3a857e 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.html
    @@ -1 +1,16 @@
    -

    Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ] or [[ $((i/2+7)) -ge 18 ]].

    \ No newline at end of file +

    Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ].

    +

    Problematic code

    +
    [ i / 2 + 7 -ge 18 ]
    +
    +

    Correct code

    +
    [ $((i / 2 + 7)) -ge 18 ]
    +
    +

    Rationale

    +

    ShellCheck found a loose +*/% in a test statement. This usually happens when trying to do arithmetic in a condition, but without using the arithmetic expansion construct $((expression)).

    +

    In C, if (a+b == c) is perfectly fine, but in sh this must be written to first expand the arithmetic operation like if [ $((a+b)) = c ].

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.json index b368c4c..45e4878 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1076.json @@ -1,5 +1,5 @@ { - "title": "Trying to do math? Use e.g. [ $((i\/2+7)) -ge 18 ] or [[ $((i\/2+7)) -ge 18 ]].", + "title": "Trying to do math? Use e.g. [ $((i\/2+7)) -ge 18 ].", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1077.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1077.html index 837cf74..fa60f67 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1077.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1077.html @@ -1,9 +1,9 @@ -

    For command expansion, the tick should slant left (` vs ´).

    +

    For command expansion, the tick should slant left (` vs ´).

    Problematic code

    -
    echo "Your username is ´whoami´"
    +
    echo "Your username is ´whoami´"
     

    Correct code

    -
    echo "Your username is $(whoami)" # Preferred
    +
    echo "Your username is $(whoami)" # Preferred
     echo "Your username is `whoami`"  # Deprecated, will give [SC2006]
     

    Rationale

    @@ -11,9 +11,9 @@

    Rationale

    Backticks start command expansions, while forward ticks are literal. To help spot bugs, ShellCheck parses backticks and forward ticks interchangeably.

    Exceptions

    If you want to write out literal forward ticks, such as fancyful ascii quotation marks:

    -
    echo "``Proprietary software is an injustice.´´  - Richard Stallman"
    +
    echo "``Proprietary software is an injustice.´´  - Richard Stallman"
     

    use single quotes instead:

    -
    echo '``Proprietary software is an injustice.´´  - Richard Stallman'
    +
    echo '``Proprietary software is an injustice.´´  - Richard Stallman'
     

    To nest forward ticks in command expansion, use $(..) instead of `..`.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.html index 7be10a9..8203865 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.html @@ -1,10 +1,10 @@ -

    Did you forget to close this double quoted string?

    +

    Did you forget to close this double-quoted string?

    Problematic code

    -
    greeting="hello
    +
    greeting="hello
     target="world"
     

    Correct code

    -
    greeting="hello"
    +
    greeting="hello"
     target="world"
     

    Rationale

    @@ -12,11 +12,11 @@

    Rationale

    ShellCheck warns when it detects multi-line double quoted, single quoted or backticked strings when the character that follows it looks out of place (and gives a companion warning [[SC1079]] at that spot).

    Exceptions

    If you do want a multiline variable, just make sure the character after it is a quote, space or line feed.

    -
    var='multiline
    -'value
    +
    var='multiline
    +'value
     

    can be rewritten for readability and to remove the warning:

    -
    var='multiline
    -value'
    +
    var='multiline
    +value'
     

    As always `..` should be rewritten to $(..).

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.json index ce7a551..c6bf65e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1078.json @@ -1,5 +1,5 @@ { - "title": "Did you forget to close this double quoted string?", + "title": "Did you forget to close this double-quoted string?", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1080.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1080.html index bf54ac7..40bd13d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1080.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1080.html @@ -1 +1,17 @@ -

    You need \ before line feeds to break lines in [ ].

    \ No newline at end of file +

    You need \ before line feeds to break lines in [ ].

    +

    Problematic code

    +
    [ "$filename" =
    +  "$otherfile" ]
    +
    +

    Correct code

    +
    [ "$filename" = \
    +  "$otherfile" ]
    +
    +

    Rationale

    +

    Bash/ksh [[ ]]] can include line breaks anywhere, but [ ] requires that you escape them. If you are writing a multi-line [ .. ] statement, make sure to include these escapes. If the [ ] is supposed to be on a single line, make sure the ] is there.

    +

    Exceptions

    +

    None

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.html index 0c44e48..95dc782 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.html @@ -1,12 +1,12 @@ -

    Scripts are case sensitive. Use 'if', not 'If'.

    +

    Scripts are case-sensitive. Use if, not If.

    Problematic code

    -
    If true
    +
    If true
     Then
       echo "hello"
     Fi
     

    Correct code

    -
    if true
    +
    if true
     then
       echo "hello"
     fi
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.json
    index 0eb572e..64d4f8d 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1081.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Scripts are case sensitive. Use 'if', not 'If'.",
    +  "title": "Scripts are case-sensitive. Use if, not If.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.html
    index 1742643..b289aac 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.html
    @@ -1,9 +1,9 @@
    -

    This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s/^...//' < yourscript .

    +

    This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s/^...//' < yourscript.

    Problematic code

    This is an encoding error that can't be seen in the script itself, but cat -v will show three bytes of garbage at the start of the file:

    $ cat -v file
     M-oM-;M-?#!/bin/bash
    -echo "hello world"
    +echo "hello world"
     

    Correct code

    The code is correct when this garbage does not appear.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.json index 6bc4ec0..6c8507b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1082.json @@ -1,5 +1,5 @@ { - "title": "This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s\/^...\/\/' < yourscript .", + "title": "This file has a UTF-8 BOM. Remove it with: LC_CTYPE=C sed '1s\/^...\/\/' < yourscript.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1083.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1083.html index 887e6c7..3ce9bbe 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1083.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1083.html @@ -1,15 +1,15 @@

    This {/} is literal. Check if ; is missing or quote the expression.

    Problematic code

    -
    rmf() { rm -f "$@" }
    +
    rmf() { rm -f "$@" }
     

    or

    -
    eval echo \${foo}
    +
    eval echo \${foo}
     

    Correct code

    -
    rmf() { rm -f "$@"; }
    +
    rmf() { rm -f "$@"; }
     

    and

    -
    eval "echo \${foo}"
    +
    eval "echo \${foo}"
     

    Rationale

    Curly brackets are normally used as syntax in parameter expansion, command grouping and brace expansion.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1084.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1084.html index 03d9f09..e693f63 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1084.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1084.html @@ -1,10 +1,10 @@ -

    Use #!, not !#, for the shebang.

    +

    Use #!, not !#, for the shebang.

    Problematic code

    -
    !#/bin/sh
    +
    !#/bin/sh
     echo "Hello World"
     

    Correct code

    -
    #!/bin/sh
    +
    #!/bin/sh
     echo "Hello World"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1086.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1086.html index 0bafe49..119aecf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1086.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1086.html @@ -1,12 +1,12 @@ -

    Don't use $ on the iterator name in for loops.

    +

    Don't use $ on the iterator name in for loops.

    Problematic code

    -
    for $var in *
    +
    for $var in *
     do
       echo "$var"
     done
     

    Correct code

    -
    for var in *
    +
    for var in *
     do
       echo "$var"
     done
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.html
    index bf0e284..f41b352 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.html
    @@ -1,9 +1,9 @@
    -

    "Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet)."

    +

    Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet).

    Problematic code

    -
    echo "$array[@]"
    +
    echo "$array[@]"
     

    Correct code

    -
    echo "${array[@]}"
    +
    echo "${array[@]}"
     

    Rationale

    Some languages use the syntax $array[index] to access an index of an arrays, but a shell will interpret this as $array followed by the unrelated literal string (or glob) [index].

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.json index 62b529a..37039c9 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1087.json @@ -1,5 +1,5 @@ { - "title": "'Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet).'", + "title": "Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1088.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1088.html index 693baa8..5a9cf08 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1088.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1088.html @@ -1,24 +1,27 @@

    Parsing stopped here. Invalid use of parentheses?

    Problematic code

    -
    grep ^(.*)\1$ file
    +
    grep ^(.*)\1$ file
     

    or

    -
    var=myfunction(value)
    +
    var=myfunction(value)
     

    Correct code

    -
    grep '^(.*)\1$' file
    +
    grep '^(.*)\1$' file
     

    or

    -
    var=$(myfunction value)
    +
    var=$(myfunction value)
     

    Rationale

    Parentheses are shell syntax and must be used correctly.

    For commands that expect literal parentheses, such as grep or find, the parentheses need to be quoted or escaped so the shell does not interpret them, but instead passes them to the command.

    For shell syntax, the shell does not use them the way most other languages do, so avoid guessing at syntax based on previous experience. In particular:

      -
    • Parentheses are NOT used to call functions.
    • -
    • Parentheses are NOT used to group expressions, except in arithmetic contexts.
    • -
    • Parentheses are NOT used in conditional statements or loops.
    • +
    • Parentheses are NOT used to call functions.

      +
    • +
    • Parentheses are NOT used to group expressions, except in arithmetic contexts.

      +
    • +
    • Parentheses are NOT used in conditional statements or loops.

      +
    • Parentheses are used differently in different contexts. ( .. ), $( .. ), $(( .. )) and var=(..) are completely separate and independent structures with different meanings, and can not be broken down into operations on expressions in parentheses.

      In C-like languages, ++ can't be broken down into two + operations, so you can't e.g. use + + or +(+). In the same way, all of the above are completely unrelated so that you can't do $(1+1) or $( (1+1) ) in place of $(( 1+1 )).

    • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1089.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1089.html index 15f8c86..685ddd8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1089.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1089.html @@ -1,13 +1,13 @@

      Parsing stopped here. Is this keyword correctly matched up?

      Problematic code

      -
      if true
      +
      if true
       then
         echo hello
       fi
       fi
       

      Correct code

      -
      if true
      +
      if true
       then
         echo hello
       fi
      @@ -15,7 +15,7 @@ 

      Correct code

      Rationale

      This error is typically seen when there are too many fi, done or esacs, or when there's a do or then without a corresponding while, for or if. This is often due to deleting a loop or conditional statement but not its terminator.

      In some cases, it can even be caused by bad quoting:

      -
      var="foo
      +
      var="foo
       if [[ $var = "bar ]
       then
         echo true
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.html
      index 7cba88a..1431f92 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.html
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.html
      @@ -1,14 +1,14 @@
      -

      Can't follow non-constant source. Use a directive to specify location.

      +

      Can't follow non-constant source. Use a directive to specify location

      Problematic code

      -
      . "${util_path}"
      +
      . "${util_path}"
       

      Correct code

      -
      # shellcheck source=src/util.sh
      +
      # shellcheck source=src/util.sh
       . "${util_path}"
       

      Rationale

      ShellCheck is not able to include sourced files from paths that are determined at runtime. The file will not be read, potentially resulting in warnings about unassigned variables and similar.

      Use a [[Directive]] to point shellcheck to a fixed location it can read instead.

      -

      ShellCheck v0.7.2+ will strip a single expansion followed by a slash, e.g. ${var}/util.sh or $(dirname "${BASH_SOURCE[0]}")/util.sh, and treat them as ./util.sh. This allowing the use of source-path directives or -P flags to specify the a location.

      +

      ShellCheck v0.7.2+ will strip a single expansion followed by a slash, e.g. ${var}/util.sh or $(dirname "${BASH_SOURCE[0]}")/util.sh, and treat them as ./util.sh. This allows the use of source-path directives or -P flags to specify the location.

      Exceptions

      If you don't care that ShellCheck is unable to account for the file, specify # shellcheck source=/dev/null.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.json index 152d65a..43ae46c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1090.json @@ -1,5 +1,5 @@ { - "title": "Can't follow non-constant source. Use a directive to specify location.", + "title": "Can't follow non-constant source. Use a directive to specify location", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1091.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1091.html index 9fbe4f7..627ead7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1091.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1091.html @@ -1,21 +1,27 @@

      Not following: (error message here)

      Reasons include: file not found, no permissions, not included on the command line, not allowing shellcheck to follow files with -x, etc.

      Problematic code

      -
      source somefile
      +
      source somefile
       

      Correct code

      In case you have access to the file, e.g. if it is included in your source code repository:

      -
      # shellcheck source=somefile
      +
      # shellcheck source=somefile
       source somefile
       

      In case you do not have access to the file:

      -
      # shellcheck source=/dev/null
      +
      # shellcheck source=/dev/null
       source somefile
       

      Rationale

      ShellCheck, for whichever reason, is not able to access the source file.

      -

      This could be because you did not include it on the command line, did not use shellcheck -x to allow following other files, don't have permissions or a variety of other problems.

      +

      This could be because:

      +
        +
      • you did not include it on the command line,
      • +
      • did not use shellcheck -x (or specified external-sources=true in the [[.shellcheckrc|Directive#external-sources]]) to allow following other files
      • +
      • don't have permissions, or
      • +
      • a variety of other problems.
      • +

      Feel free to ignore the error with a [[directive]].

      Exceptions

      -

      ShellCheck is unable to follow dynamic paths, such as source "$somedir/file". For these cases, see [[SC1090: Can't follow non-constant source. Use a directive to specify location|SC1090]] instead. You may be seeing SC1091 because ShellCheck tried to be helpful and strip a leading dynamic path element as described on that page.

      +

      ShellCheck is unable to follow dynamic paths, such as source "$somedir/file". For these cases, see SC1090: Can't follow non-constant source. Use a directive to specify location instead. You may be seeing SC1091 because ShellCheck tried to be helpful and strip a leading dynamic path element as described on that page.

      If you're fine with it, ignore the message with a [[directive]].

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.html index 9b90093..0e6d89b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.html @@ -1 +1,13 @@ -

      Stopping at 100 'source' frames :O

      \ No newline at end of file +

      Stopping at 100 source frames :O

      +

      Problematic code

      +

      An initial file sourcing a second file, which in turn sources a third file, which in turn sources a fourth file, ...., which in turn sources a 100th file.

      +

      Correct code

      +

      Anything but that.

      +

      Rationale

      +

      ShellCheck found a chain of 100+ files sourcing each other. It assumed there must be some internal bug, so it stopped.

      +

      Exceptions

      +

      If this is intentional, you can cosmetically [[ignore]] this message.

      +

      Related resources

      +
        +
      • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
      • +
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.json index ba5e7a5..2920c18 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1092.json @@ -1,5 +1,5 @@ { - "title": "Stopping at 100 'source' frames :O", + "title": "Stopping at 100 source frames :O", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1094.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1094.html index 08ee96c..542a8e8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1094.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1094.html @@ -1,9 +1,9 @@

      Parsing of sourced file failed. Ignoring it.

      Problematic code

      -
      source mylib
      +
      source mylib
       

      Correct code

      -
      # shellcheck disable=SC1094
      +
      # shellcheck disable=SC1094
       source mylib
       

      (or fix mylib)

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1095.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1095.html index 3d3d95b..5774667 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1095.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1095.html @@ -1,17 +1,17 @@

      You need a space or linefeed between the function name and body.

      Problematic code

      -
      function foo{
      +
      function foo{
         echo "hello world"
       }
       

      Correct code

      Prefer POSIX syntax:

      -
      foo() {
      +
      foo() {
         echo "hello world"
       }
       

      Alternatively, add the missing space between function name and opening {:

      -
      #           v-- Here
      +
      #           v-- Here
       function foo {
         echo "hello world"
       }
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1097.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1097.html
      index 52392f7..765b495 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1097.html
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1097.html
      @@ -1,13 +1,13 @@
      -

      Unexpected ==. For assignment, use =. For comparison, use [/[[.

      +

      Unexpected ==. For assignment, use =. For comparison, use [/[[.

      Problematic code

      -
      var==value
      +
      var==value
       

      Correct code

      Assignment:

      -
      var=value
      +
      var=value
       

      Comparison:

      -
      [ "$var" = value ]
      +
      [ "$var" = value ]
       

      Rationale

      ShellCheck has noticed that you're using == in an unexpected way. The two most common reasons for this is:

      @@ -19,5 +19,5 @@

      Rationale

    Exceptions

    If you wanted to assign a literal equals sign, use quotes to make this clear:

    -
    var="=sum(A1:A10)"
    +
    var="=sum(A1:A10)"
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.html index 8f6a67c..60e6707 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.html @@ -1,9 +1,9 @@ -

    Quote/escape special characters when using eval, e.g. eval "a=(b)".

    +

    Quote/escape special characters when using eval, e.g. eval "a=(b)".

    Problematic code

    -
    eval $var=(a b)
    +
    eval $var=(a b)
     

    Correct code

    -
    eval "$var=(a b)"
    +
    eval "$var=(a b)"
     

    Rationale

    Shells differ widely in how they handle unescaped parentheses in eval expressions.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.json index 02d1c32..1d2d2cc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1098.json @@ -1,5 +1,5 @@ { - "title": "Quote\/escape special characters when using eval, e.g. eval 'a=(b)'.", + "title": "Quote\/escape special characters when using eval, e.g. eval "a=(b)".", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1099.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1099.html index 1c7417d..035fa7a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1099.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1099.html @@ -1,12 +1,12 @@ -

    You need a space before the #.

    +

    You need a space before the #.

    Problematic code

    -
    while sleep 1
    +
    while sleep 1
     do# show time
       date
     done
     

    Correct code

    -
    while sleep 1
    +
    while sleep 1
     do # show time
       date
     done
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.html
    index 9d3bdfb..a008f67 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.html
    @@ -1,9 +1,9 @@
    -

    This is a unicode dash. Delete and retype as ASCII minus.

    +

    This is a Unicode dash. Delete and retype as ASCII minus.

    Problematic code

    -
    [[ 3 –gt 2 ]] # Uses unicode en-dash character
    +
    [[ 3 –gt 2 ]] # Uses unicode en-dash character
     

    Correct code

    -
    [[ 3 -gt 2 ]] # Uses regular ASCII hyphen-minus character
    +
    [[ 3 -gt 2 ]] # Uses regular ASCII hyphen-minus character
     

    Rationale

    A character that looks similar to - has made its way into your code. This is usually due to copy-pasting from blogs and other websites that formatted code as text, replacing the ASCII hyphen-minus with a Unicode dash character.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.json index 323c6a6..a68e13d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1100.json @@ -1,5 +1,5 @@ { - "title": "This is a unicode dash. Delete and retype as ASCII minus.", + "title": "This is a Unicode dash. Delete and retype as ASCII minus.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1101.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1101.html index 2fe5afb..f97057e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1101.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1101.html @@ -1,11 +1,11 @@ -

    Delete trailing spaces after \ to break line (or use quotes for literal space).

    +

    Delete trailing spaces after \ to break line (or use quotes for literal space).

    Problematic code

    -
    # There are spaces after the backslash:
    +
    # There are spaces after the backslash:
     echo hello \  
          world
     

    Correct code

    -
    # No spaces after the backslash:
    +
    # No spaces after the backslash:
     echo hello \
          world
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.html index c71e9ba..2f4cc4d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.html @@ -1,9 +1,9 @@ -

    Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.

    +

    Shells disambiguate $(( differently or not at all. For $(command substitution), add space after $( . For $((arithmetics)), fix parsing errors.

    Problematic code

    -
    echo "$((cmd "$@") 2>&1)"
    +
    echo "$((cmd "$@") 2>&1)"
     

    Correct code

    -
    echo "$( (cmd "$@") 2>&1)"
    +
    echo "$( (cmd "$@") 2>&1)"
     

    Rationale

    You appear to be using $(( with two (or more) parentheses in a row, where the first $( should open a subshell.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.json index e10fec2..33991ec 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1102.json @@ -1,5 +1,5 @@ { - "title": "Shells disambiguate $(( differently or not at all. For $(command substition), add space after $( . For $((arithmetics)), fix parsing errors.", + "title": "Shells disambiguate $(( differently or not at all. For $(command substitution), add space after $( . For $((arithmetics)), fix parsing errors.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1103.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1103.html index 18e74f6..8e731f8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1103.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1103.html @@ -1 +1,30 @@ -

    This shell type is unknown. Use e.g. sh or bash.

    \ No newline at end of file +

    This shell type is unknown. Use e.g. sh or bash.

    +

    Problematic code

    +
    # shellcheck shell=zsh
    +export PAGER=less
    +
    +

    Correct code

    +

    Any supported shell on the shebang or the -s option

    +
    # shellcheck shell=sh
    +export PAGER=less
    +
    +

    Rationale

    +

    Shellcheck only supports a specific range of shell dialects, there are many more applications providing shell like experiences and some of them look and feel like POSIX shell or bash but does not support the same commands.

    +

    One notable unsupported shell type is zsh, see issue #809 about supporting zsh - some efforts have been done in the past.

    +

    Exceptions

    +

    The supported shell types are listed in the help context, at the moment these are

    +
      +
    • sh
    • +
    • bash
    • +
    • dash
    • +
    • ksh
    • +
    +

    Related resources

    +
      +
    • Similar rules
        +
      • [[SC1008]] - unrecognized shebang
      • +
      • [[SC1071]] - unsupported shebang
      • +
      +
    • +
    • [[Documentation for shell directive|Directive#shell]]
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1104.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1104.html index e12805e..f7dd362 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1104.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1104.html @@ -1,14 +1,14 @@ -

    Use #!, not just !, for the shebang.

    +

    Use #!, not just !, for the shebang.

    Problematic code

    -
    !/bin/sh
    +
    !/bin/sh
     echo "Hello"
     

    Correct code

    -
    #!/bin/sh
    +
    #!/bin/sh
     echo "Hello"
     

    Rationale

    You appear to be specifying an interpreter in a shebang, but it's missing the hash part. The shebang must always start with #!.

    -

    Even the name "shebang" itself comes from "hash" (#) + "bang" (!).

    +

    Even the name "shebang" itself comes from "hash" (#) + "bang" (!).

    Exceptions

    None.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1105.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1105.html index 02de71d..8e1c347 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1105.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1105.html @@ -1,2 +1,2 @@ -

    Shells disambiguate (( differently or not at all. If the first ( should start a subshell, add a space after it.

    +

    Shells disambiguate (( differently or not at all. If the first ( should start a subshell, add a space after it.

    See [[SC1102]], the similar warning for ambiguous $((.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1106.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1106.html index 030e5e6..1c92a23 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1106.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1106.html @@ -1,20 +1,20 @@

    In arithmetic contexts, use < instead of -lt

    Similarly, > instead of -gt, <= instead of -le, >= instead of -ge, == instead of -eq, != instead of -ne.

    Problematic code

    -
    if (( 2 -lt 3 ))
    +
    if (( 2 -lt 3 ))
     then
       echo "True"
     fi
     

    Correct code

    -
    if (( 2 < 3 ))
    +
    if (( 2 < 3 ))
     then
       echo "True"
     fi
     

    Rationale

    The comparators -lt, -ge, -eq and friends are flags for the test command aka [. You are instead using it in an arithmetic context, such as (( .. )) or $(( .. )), where you should be using <, >=, == etc instead.

    -

    In arithmetic contexts, -lt is simply interpreted as "subtract the value of $lt", which is clearly not the intention.

    +

    In arithmetic contexts, -lt is simply interpreted as "subtract the value of $lt", which is clearly not the intention.

    Exceptions

    If you do want to subtract $lt you can add a space to make this clear to ShellCheck: echo $((3 - lt))

    Related resources

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1107.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1107.html index 556b05b..a006ce4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1107.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1107.html @@ -1,6 +1,6 @@

    This directive is unknown. It will be ignored.

    Problematic code

    -
    # shellcheck foobar=baz
    +
    # shellcheck foobar=baz
     echo "Hello World"
     

    Correct code

    @@ -14,7 +14,7 @@

    Exceptions

    # shellcheck disable=SC1107 { # shellcheck unrecognized=directive - echo "Hello World" + echo "Hello World" }

    Before 0.4.5, unrecognized directives are considered parse errors.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1108.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1108.html index bb66044..3369742 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1108.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1108.html @@ -1,12 +1,12 @@ -

    You need a space before and after the = .

    +

    You need a space before and after the = .

    Problematic code

    -
    [ "$var"= 2 ]
    +
    [ "$var"= 2 ]
     

    Correct code

    -
    [ "$var" = 2 ]
    +
    [ "$var" = 2 ]
     

    Rationale

    You appear to be missing the space on the left side of the operator. Shell in general, and [ in particular, is space sensitive. Operators and operands must be separate tokens.

    Please ensure that the operator, like the = in the example, has a space both before and after it.

    Exceptions

    -

    None. If you're comparing values in C style reverse order like [ -eq == $1 ], use quotes: [ "-eq" == "$1" ]. Also, it's pointless since [ a = b ] doesn't assign.

    +

    None. If you're comparing values in C style reverse order like [ -eq == $1 ], use quotes: [ "-eq" == "$1" ]. Also, it's pointless since [ a = b ] doesn't assign.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1109.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1109.html index e9b245a..435f0b2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1109.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1109.html @@ -1,9 +1,9 @@

    This is an unquoted HTML entity. Replace with corresponding character.

    Problematic code

    -
    foo &amp;&amp; bar
    +
    foo &amp;&amp; bar
     

    Correct code

    -
    foo && bar
    +
    foo && bar
     

    Rationale

    There is an unquoted HTML entity, such as &amp;, &gt; or &lt; (instead of &, > and <) in your code. This usually happens when copy-pasting from a web site that has mismanaged its code formatting.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.html index 97a5188..c4f5b8e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.html @@ -1,13 +1,13 @@ -

    This is a unicode quote. Delete and retype it (or quote to make literal).

    +

    This is a Unicode quote. Delete and retype it (or quote to make literal).

    Problematic code

    -
    echo ‘hello world’
    +
    echo ‘hello world’
     

    Correct code

    -
    echo 'hello world'
    +
    echo 'hello world'
     

    Rationale

    Some software, like OS X, Word and WordPress, may automatically replace your regular quotes with slanted Unicode quotes. Try deleting and retyping them, and/or disable “smart quotes” in your editor or OS.

    Exceptions

    If you want to use typographic single quotes, you can put them in double quotes (or typographic double quotes in single quotes) to make shellcheck ignore them, e.g.,

    -
    printf "Warning: ‘wakeonlan’ is not installed.\n"
    +
    printf "Warning: ‘wakeonlan’ is not installed.\n"
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.json index 6628c88..25b030e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1110.json @@ -1,5 +1,5 @@ { - "title": "This is a unicode quote. Delete and retype it (or quote to make literal).", + "title": "This is a Unicode quote. Delete and retype it (or quote to make literal).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.html index 050054c..9e43264 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.html @@ -1,18 +1,18 @@ -

    This is a unicode quote. Delete and retype it (or ignore/singlequote for literal).

    +

    This is a Unicode quote. Delete and retype it (or ignore/singlequote for literal).

    Problematic code

    -
    echo "hello world”
    +
    echo "hello world”
     

    Correct code

    -
    echo "hello world"
    +
    echo "hello world"
     

    Rationale

    Some software, like OS X, Word and WordPress, may automatically replace your regular quotes with slanted Unicode quotes. The shell does not recognize these quotes and will not respect them.

    In this case, you have slanted double quotes in a double quoted string. Try deleting and retyping them, and/or disable “smart quotes” in your editor or OS.

    Exceptions

    If you want to use literal slanted double quotes for typographic reasons, you can put them in single quotes to make ShellCheck ignore them:

    -
    printf 'Warning: “wakeonlan” is not installed.\n'
    +
    printf 'Warning: “wakeonlan” is not installed.\n'
     

    Alternatively, use single slanted Unicode quotes like so:

    -
    printf "Warning: ‘wakeonlan’ is not installed.\n"
    +
    printf "Warning: ‘wakeonlan’ is not installed.\n"
     

    You can also just [[ignore]] this warning.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.json index 216bf90..565f1ab 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1111.json @@ -1,5 +1,5 @@ { - "title": "This is a unicode quote. Delete and retype it (or ignore\/singlequote for literal).", + "title": "This is a Unicode quote. Delete and retype it (or ignore\/singlequote for literal).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.html index 72a958d..c20fb09 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.html @@ -1,15 +1,15 @@ -

    This is a unicode quote. Delete and retype it (or ignore/doublequote for literal).

    +

    This is a Unicode quote. Delete and retype it (or ignore/doublequote for literal).

    Problematic code

    -
    echo 'hello world’
    +
    echo 'hello world’
     

    Correct code

    -
    echo 'hello world'
    +
    echo 'hello world'
     

    Rationale

    Some software, like OS X, Word and WordPress, may automatically replace your regular quotes with slanted Unicode quotes. The shell does not recognize these quotes and will not respect them.

    In this case, you have slanted single quotes in a single quoted string. Try deleting and retyping them, and/or disable “smart quotes” in your editor or OS.

    Exceptions

    If you want to use literal slanted single quotes for typographic reasons, you can put them in double quotes to make ShellCheck ignore them:

    -
    printf "Warning: ‘wakeonlan’ is not installed.\\n"
    +
    printf "Warning: ‘wakeonlan’ is not installed.\\n"
     

    You can also just [[ignore]] this warning.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.json index dd6128d..f093a09 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1112.json @@ -1,5 +1,5 @@ { - "title": "This is a unicode quote. Delete and retype it (or ignore\/doublequote for literal).", + "title": "This is a Unicode quote. Delete and retype it (or ignore\/doublequote for literal).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1113.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1113.html index 2b6a620..95bb830 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1113.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1113.html @@ -1,10 +1,10 @@ -

    Use #!, not just #, for the shebang.

    +

    Use #!, not just #, for the shebang.

    Problematic code

    -
    # /bin/bash
    +
    # /bin/bash
     echo "Hello World"
     

    Correct code

    -
    #! /bin/bash
    +
    #! /bin/bash
     echo "Hello World"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1114.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1114.html index 6a3e9b0..99db640 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1114.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1114.html @@ -1,10 +1,10 @@

    Remove leading spaces before the shebang.

    Problematic code

    -
      #!/bin/sh
    +
      #!/bin/sh
     echo "Hello world"
     

    Correct code

    -
    #!/bin/sh
    +
    #!/bin/sh
     echo "Hello World"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1115.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1115.html index e46af49..2068a85 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1115.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1115.html @@ -1,10 +1,10 @@ -

    Remove spaces between # and ! in the shebang.

    +

    Remove spaces between # and ! in the shebang.

    Problematic code

    -
    # !/bin/sh
    +
    # !/bin/sh
     echo "Hello World"
     

    Correct code

    -
    #!/bin/sh
    +
    #!/bin/sh
     echo "Hello World"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1116.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1116.html index 4f31964..c935217 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1116.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1116.html @@ -1,14 +1,13 @@ -

    Missing $ on a $((..)) expression? (or use ( ( for arrays).

    +

    Missing $ on a $((..)) expression? (or use ( ( for arrays).

    Problematic code

    -
    var=((foo+1))
    +
    var=((foo+1))
     

    Correct code

    -
    var=$((foo+1))
    +
    var=$((foo+1))
     

    Rationale

    You appear to be missing the $ on an assignment from an arithmetic expression var=$((..)) .

    Without the $, this is an array expression which is either nested (ksh) or invalid (bash).

    Exceptions

    If you are trying to define a multidimensional Ksh array, add spaces between the ( ( to clarify:

    -
    var=( (1 2 3) (4 5 6) )
    -
    +
    var=( (1 2 3) (4 5 6) )
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.html new file mode 100644 index 0000000..3d76b21 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.html @@ -0,0 +1,20 @@ +

    Backslash is literal in "\n". Prefer explicit escaping: "\\n".

    +

    Note: this warning has been retired due to being too pedantic. Removed in v0.6.0 - 2018-12-03

    +

    Problematic code

    +
    printf "%s\n" "Hello"
    +
    +

    Correct code

    +
    printf "%s\\n" "Hello"
    +
    +

    or alternatively, with single quotes:

    +
    printf '%s\n' "Hello"
    +
    +

    Rationale

    +

    In a double quoted string, you have escaped a character that has no special behavior when escaped. Instead, it's invoking the fallback behavior of being interpreted literally.

    +

    Instead of relying on this implicit fallback, you should escape the backslash explicitly. This makes it clear that it's meant to be passed as a literal backslash in the string parameter.

    +

    Exceptions

    +

    None. This is a stylistic issue which can be [[ignored]]. But can you name the 5 characters that are special when escaped in double quotes?

    +

    They are $, `, ", \, or newline. More infos are available in the bash manual.

    +

    Retired

    +

    This warning is no longer emitted as of d8a32da07 (strictly after v0.5).

    +

    The number of harmlessly affected printf, sed and grep statements was significantly higher than the number of actual unexpanded escape sequences. It may return some day under a -pedantic type flag.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.json new file mode 100644 index 0000000..d5a856d --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1117.json @@ -0,0 +1,14 @@ +{ + "title": "Backslash is literal in "\\n". Prefer explicit escaping: "\\\\n".", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1118.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1118.html index dabc94b..45692e3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1118.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1118.html @@ -1,12 +1,12 @@

    Delete whitespace after the here-doc end token.

    Problematic code

    -

    "▭" below indicates an otherwise invisible space:

    -
    cat << "eof"
    +

    "▭" below indicates an otherwise invisible space:

    +
    cat << "eof"
     Hello
    -eof▭
    +eof▭   
     

    Correct code

    -
    cat << "eof"
    +
    cat << "eof"
     Hello
     eof
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.html index a2f1f75..5fe0445 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.html @@ -1,11 +1,12 @@ -

    Add a linefeed between end token and terminating ')'.

    +

    Add a linefeed between end token and terminating ).

    Problematic code

    -
    var=$(fmt -s "$COLUMNS" << "eof"
    +
    var=$(fmt -s "$COLUMNS" << "eof"
     This is a bunch of text
     eof)
    +
     

    Correct code

    -
    var=$(fmt -s "$COLUMNS" << "eof"
    +
    var=$(fmt -s "$COLUMNS" << "eof"
     This is a bunch of text
     eof
     )
    @@ -14,7 +15,6 @@ 

    Rationale

    When embedding a here document in $(..) or (..), there needs to be a linefeed (newline) between the here doc token and the closing ). Please insert one.

    Failing to do so may cause warnings like this:

    bash: warning: here-document at line 15 delimited by end-of-file (wanted `eof')`
    -dash: 5: Syntax error: end of file unexpected (expecting ")")
    -
    +dash: 5: Syntax error: end of file unexpected (expecting ")")

    Exceptions

    This error may be incorrectly emitted for ksh, where this is allowed. In this case, please either write it in a standard way or ignore this warning.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.json index 865b79d..19c0cad 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1119.json @@ -1,5 +1,5 @@ { - "title": "Add a linefeed between end token and terminating ')'.", + "title": "Add a linefeed between end token and terminating ).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1120.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1120.html index aa37363..3f11b87 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1120.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1120.html @@ -1,11 +1,11 @@

    No comments allowed after here-doc token. Comment the next line instead.

    Problematic code

    -
    cat << eof  # --- Start greeting --
    +
    cat << eof  # --- Start greeting --
     Hello
     eof         # --- End greeting ---
     

    Correct code

    -
    cat << eof  # --- Start greeting --
    +
    cat << eof  # --- Start greeting --
     Hello
     eof
                 # --- End greeting ---
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1121.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1121.html
    index 3649486..72e4dac 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1121.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1121.html
    @@ -1,12 +1,13 @@
    -

    Add ;/& terminators (and other syntax) on the line with the <<, not here.

    +

    Add ;/& terminators (and other syntax) on the line with the <<, not here.

    Problematic code

    -
    sudo bash -s << "END"
    +
    sudo bash -s << "END"
       cmd1
       cmd2
     END &
    +
     

    Correct code

    -
    sudo bash -s << "END" &
    +
    sudo bash -s << "END" &
       cmd1
       cmd2
     END
    @@ -14,6 +15,6 @@ 

    Correct code

    Rationale

    You are using &, ;, &> or similar after a here document. This is not allowed.

    This should instead be part of the line that initiated the here document, i.e. the one with the <<.

    -

    If it helps, look at << "END" as if it was < file, and make sure the resulting command is valid. This is what the shell does. You can then append here document data after the command.

    +

    If it helps, look at << "END" as if it was < file, and make sure the resulting command is valid. This is what the shell does. You can then append here document data after the command.

    Exceptions

    None.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1122.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1122.html index 2aa69c3..87b3e02 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1122.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1122.html @@ -1,17 +1,17 @@

    Nothing allowed after end token. To continue a command, put it on the line with the <<.

    Problematic code

    -
    cat << EOF
    +
    cat << EOF
     Hello
     EOF | nl
     

    Correct code

    -
    cat << EOF | nl
    +
    cat << EOF | nl
     Hello
     EOF
     

    Rationale

    You have a here document, and appear to have added text after the terminating token.

    This is not allowed. If it was meant to continue the command, put it on the line with the <<.

    -

    If it helps, look at << "END" as if it was < file, and make sure the resulting command is valid. This is what the shell does. You can then append here document data after the command.

    +

    If it helps, look at << "END" as if it was < file, and make sure the resulting command is valid. This is what the shell does. You can then append here document data after the command.

    Exceptions

    None

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1123.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1123.html index 26748fb..3379996 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1123.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1123.html @@ -1,6 +1,6 @@

    ShellCheck directives are only valid in front of complete compound commands, like if, not e.g. individual elif branches.

    Problematic code

    -
    if [ "$prod" = "true" ]
    +
    if [ "$prod" = "true" ]
     then
       echo "Prod mode"
     # shellcheck disable=2154
    @@ -10,7 +10,7 @@ 

    Problematic code

    fi

    Correct code

    -
    # Applies to entire `if...fi` command
    +
    # Applies to entire `if...fi` command
     # shellcheck disable=2154
     if [ "$prod" = "true" ]
     then
    @@ -21,7 +21,7 @@ 

    Correct code

    fi

    or

    -
    if [ "$prod" = "true" ]
    +
    if [ "$prod" = "true" ]
     then
       echo "Prod mode"
     elif  # Applies only to this [ .. ] command
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.html
    index e7b9b30..51f28b8 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.html
    @@ -1,13 +1,14 @@
    -

    ShellCheck directives are only valid in front of complete commands like 'case' statements, not individual case branches.

    +

    ShellCheck directives are only valid in front of complete commands like case statements, not individual case branches.

    Problematic code

    -
    case $? in
    +
    case $? in
       0) echo "Success" ;;
       # shellcheck disable=2154
       *) echo "$cmd $flag returned failure" ;;
     esac
    +
     

    Correct code

    -
    # Applies to everything in the `case` statement
    +
    # Applies to everything in the `case` statement
     # shellcheck disable=2154
     case $? in
       0) echo "Success" ;;
    @@ -15,7 +16,7 @@ 

    Correct code

    esac

    or

    -
    case $? in
    +
    case $? in
       0) echo "Success" ;;
       *)
          # Applies to a single command within the `case`
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.json
    index 4ea9ef6..83c591a 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1124.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "ShellCheck directives are only valid in front of complete commands like 'case' statements, not individual case branches.",
    +  "title": "ShellCheck directives are only valid in front of complete commands like case statements, not individual case branches.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1125.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1125.html
    index 0f00954..cb12dd8 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1125.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1125.html
    @@ -1,9 +1,9 @@
    -

    Invalid key=value pair in directive

    +

    Invalid key=value pair in directive

    Problematic code

    -
    # shellcheck disable=SC2153 (variable not a misspelling)
    +
    # shellcheck disable=SC2153 (variable not a misspelling)
     

    Correct code

    -
    # shellcheck disable=SC2153 # variable not a misspelling
    +
    # shellcheck disable=SC2153 # variable not a misspelling
     

    Rationale

    A comment at the end of a directive must be preceded by a # diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1126.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1126.html index 80746bc..bbe1784 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1126.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1126.html @@ -1,14 +1,13 @@

    Place shellcheck directives before commands, not after.

    Problematic code

    -
    var=1 # shellcheck disable=SC2034
    +
    var=1 # shellcheck disable=SC2034
     

    Correct code

    -
    # shellcheck disable=SC2034
    -var=1
    +
    # shellcheck disable=SC2034
    +var=1 
     

    Rationale

    ShellCheck expects directives to come before the relevant command. They are not allowed after.

    Exceptions

    If this is not a directive and just a comment mentioning ShellCheck, please rewrite or capitalize:

    -
    var=1 # ShellCheck encourages lowercase variable names
    -
    +
    var=1 # ShellCheck encourages lowercase variable names
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1127.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1127.html index 039f163..762d7ec 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1127.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1127.html @@ -1,10 +1,10 @@

    Was this intended as a comment? Use # in sh.

    Problematic code

    -
    // This is a comment.
    +
    // This is a comment.
     /* This too. */
     

    Correct code

    -
    # This is a comment.
    +
    # This is a comment.
     # This too.
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1128.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1128.html index 45e156e..92f7805 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1128.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1128.html @@ -1,10 +1,10 @@

    The shebang must be on the first line. Delete blanks and move comments.

    Problematic code

    -
    # Copyright 2018 Foobar, All rights reserved
    +
    # Copyright 2018 Foobar, All rights reserved
     #!/bin/bash
     

    Correct code

    -
    #!/bin/bash
    +
    #!/bin/bash
     # Copyright 2018 Foobar, All rights reserved
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1129.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1129.html index 5126c9a..3796739 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1129.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1129.html @@ -1,10 +1,10 @@ -

    You need a space before the !.

    +

    You need a space before the !.

    Problematic code

    -
    while! [ -f file ]
    +
    while! [ -f file ]
     do sleep 1; done
     

    Correct code

    -
    while ! [ -f file ]
    +
    while ! [ -f file ]
     do sleep 1; done
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1130.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1130.html index e8093df..914c302 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1130.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1130.html @@ -1,14 +1,14 @@

    You need a space before the :.

    Problematic code

    -
    until make
    +
    until make
     do:; done
     

    Correct code

    -
    until make
    +
    until make
     do :; done
     

    Rationale

    -

    ShellCheck found a keyword immediately followed by a :. : is a synonym for true, the command that "does nothing, successfully", and as a command name it needs a space.

    +

    ShellCheck found a keyword immediately followed by a :. : is a synonym for true, the command that "does nothing, successfully", and as a command name it needs a space.

    do: is as invalid as dotrue. Use do :, or preferably, do true for readability.

    Exceptions

    None

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1131.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1131.html index 11d6c5e..e7b3a35 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1131.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1131.html @@ -1,6 +1,6 @@

    Use elif to start another branch.

    Problematic code

    -
    if false
    +
    if false
     then
       echo "hi"
     elseif true
    @@ -9,7 +9,7 @@ 

    Problematic code

    fi

    Correct code

    -
    if false
    +
    if false
     then
       echo "hi"
     elif true
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1132.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1132.html
    index f7202aa..a7556eb 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1132.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1132.html
    @@ -1,9 +1,9 @@
     

    This & terminates the command. Escape it or add space after & to silence.

    Problematic code

    -
    curl https://www.google.com/search?q=cats&tbm=isch
    +
    curl https://www.google.com/search?q=cats&tbm=isch
     

    Correct code

    -
    curl "https://www.google.com/search?q=cats&tbm=isch"
    +
    curl "https://www.google.com/search?q=cats&tbm=isch"
     

    Rationale

    An unescaped and unquoted & terminates the command, but here it's used in the middle of what would otherwise be a shell word. This most commonly happens when copying a URL with query string parameters without escaping the &.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1133.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1133.html index b901a08..9c423f7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1133.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1133.html @@ -1,10 +1,10 @@ -

    Unexpected start of line. If breaking lines, |/||/&& should be at the end of the previous one.

    +

    Unexpected start of line. If breaking lines, |/||/&& should be at the end of the previous one.

    Problematic code

    -
    dmesg
    +
    dmesg
       | grep "error"
     

    Correct code

    -
    dmesg |
    +
    dmesg |
       grep "error"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.html index b1a567e..232d164 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.html @@ -1 +1,4 @@ -

    Parse error: failed to process file.

    \ No newline at end of file +

    Error parsing shellcheckrc:

    +
    SC1134 (error): Failed to process foo, line bar:  Fix any mentioned problems and try again.
    +
    +

    Need more information.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.json index e0c46ff..68094f3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1134.json @@ -1,5 +1,5 @@ { - "title": "Parse error: failed to process file.", + "title": "Error parsing shellcheckrc:", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.html index 368e72a..a9ca794 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.html @@ -1,10 +1,10 @@ -

    Prefer escape over ending quote to make $ literal. Instead of "It costs $"5, use "It costs \$5"

    +

    Prefer escape over ending quote to make $ literal. Instead of "It costs $"5, use "It costs \$5"

    Problematic code

    -
    echo "The apples are $""1 each"
    +
    echo "The apples are $""1 each"
     eval "var=$"name
     

    Correct code

    -
    echo "The apples are \$1 each"
    +
    echo "The apples are \$1 each"
     eval "var=\$name"
     # or better yet: var="${!name}"
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.json index 2b5e415..0ea18a3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1135.json @@ -1,5 +1,5 @@ { - "title": "Prefer escape over ending quote to make $ literal. Instead of 'It costs $'5, use 'It costs \\$5'", + "title": "Prefer escape over ending quote to make $ literal. Instead of "It costs $"5, use "It costs \\$5"", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1136.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1136.html index ffc223c..aac291a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1136.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1136.html @@ -1,11 +1,12 @@ -

    Unexpected characters after terminating ]. Missing semicolon/linefeed?

    +

    Unexpected characters after terminating ]. Missing semicolon/linefeed? +Note: Removed in V0.7.2 2021-04-20

    Problematic code

    -
    if [ -e "foo.txt" ]: then
    +
    if [ -e "foo.txt" ]: then
       echo "Exists"
     fi
     

    Correct code

    -
    if [ -e "foo.txt" ]; then
    +
    if [ -e "foo.txt" ]; then
       echo "Exists"
     fi
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.html index 479e393..b094f63 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.html @@ -1 +1,21 @@ -

    Missing second '(' or ')' to start arithmetic or terminate the condition of the 'for ((;;))' loop

    \ No newline at end of file +

    Missing second ( to start arithmetic for ((;;)) loop

    +

    Problematic code

    +
    for (i=0; i<10; i++))
    +do
    +  echo $i
    +done
    +
    +

    Correct code

    +
    for ((i=0; i<10; i++))
    +do
    +  echo $i
    +done
    +
    +

    Rationale

    +

    ShellCheck found an arithmetic for ((;;)) expression where either the (( or the )) did not come as a pair. Make sure to use (( )) and not ( ).

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.json index 57bb723..65bc2d0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1137.json @@ -1,5 +1,5 @@ { - "title": "Missing second '(' or ')' to start arithmetic or terminate the condition of the 'for ((;;))' loop", + "title": "Missing second ( to start arithmetic for ((;;)) loop", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.html index 3d60c60..20d0de5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.html @@ -1 +1,15 @@ -

    Remove spaces in arithmetic for loop.

    \ No newline at end of file +

    Remove spaces between (( in arithmetic for loop.

    +

    Problematic code

    +
    for( (i=0; i<10; i++) ); do echo $i; done
    +
    +

    Correct code

    +
    for((i=0; i<10; i++)); do echo $i; done
    +
    +

    Rationale

    +

    ShellCheck finds arithmetic for ((;;)) expressions where (( or )) are intervening with spaces

    +

    Exceptions

    +

    None

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.json index 014b8e3..f40ea8a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1138.json @@ -1,5 +1,5 @@ { - "title": "Remove spaces in arithmetic for loop.", + "title": "Remove spaces between (( in arithmetic for loop.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.html index a27bf4e..b304f14 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.html @@ -1,13 +1,13 @@ -

    Use || instead of '-o' between test commands.

    -

    And variations, like "Use && instead of and".

    +

    Use || instead of -o between test commands.

    +

    And variations, like "Use && instead of and".

    Problematic code

    -
    if [ "$1" = "-v" ] -o [ -n "$VERBOSE" ]
    +
    if [ "$1" = "-v" ] -o [ -n "$VERBOSE" ]
     then
       echo "Verbose log"
     fi
     

    Correct code

    -
    if [ "$1" = "-v" ] || [ -n "$VERBOSE" ]
    +
    if [ "$1" = "-v" ] || [ -n "$VERBOSE" ]
     then
       echo "Verbose log"
     fi
    @@ -15,7 +15,7 @@ 

    Correct code

    Rationale

    You have a [ .. ] or [[ .. ]] test expression followed by -o/-a (or by Python-style or/and).

    -o and -a work inside [ .. ], but they do not work between them. The Python operators or and and are never recognized in Bash.

    -

    To join two separate test expressions, instead use || for "logical OR", or && for "logical AND".

    +

    To join two separate test expressions, instead use || for "logical OR", or && for "logical AND".

    Exceptions

    None

    Related resources

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.json index cff4dd5..8436a08 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1139.json @@ -1,5 +1,5 @@ { - "title": "Use || instead of '-o' between test commands.", + "title": "Use || instead of -o between test commands.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1140.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1140.html index d801cb1..3f319b1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1140.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1140.html @@ -1,9 +1,9 @@ -

    Unexpected parameters after condition. Missing &&/||, or bad expression?

    +

    Unexpected parameters after condition. Missing &&/||, or bad expression?

    Problematic code

    -
    [ "$1" ] input="$1"
    +
    [ "$1" ] input="$1"
     

    Correct code

    -
    [ "$1" ] && input="$1"
    +
    [ "$1" ] && input="$1"
     

    Rationale

    ShellCheck found characters (other than redirections) after the ] or ]] in a test expression. This is not valid.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1141.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1141.html index fb4ae81..66f6761 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1141.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1141.html @@ -1,12 +1,12 @@ -

    Unexpected tokens after compound command. Bad redirection or missing ;/&&/||/|?

    +

    Unexpected tokens after compound command. Bad redirection or missing ;/&&/||/|?

    Problematic code

    -
    while echo "$2"; do true; done \
    +
    while echo "$2"; do true; done \
       head -n "$1"
     
     while sleep 1; do date; done > my file
     

    Correct code

    -
    while echo "$2"; do true; done \
    +
    while echo "$2"; do true; done \
       | head -n "$1"
     
     while sleep 1; do date; done > "my file"
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.html
    index f01b12c..e443f4c 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.html
    @@ -1,17 +1,17 @@
    -

    Use 'done < <(cmd)' to redirect from process substitution (currently missing one '<').

    +

    Use done < <(cmd) to redirect from process substitution (currently missing one <).

    Problematic code

    -
    sum=0
    +
    sum=0
     while IFS="" read -r n
     do
       (( sum += n ))
    -done <(file)
    +done <(file) 
     

    Correct code

    -
    sum=0
    +
    sum=0
     while IFS="" read -r n
     do
       (( sum += n ))
    -done < <(file)
    +done < <(file) 
     

    Rationale

    ShellCheck found a done keyword followed by a process substitution, e.g. done <(cmd).

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.json index 6547fbd..7e57171 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1142.json @@ -1,5 +1,5 @@ { - "title": "Use 'done < <(cmd)' to redirect from process substitution (currently missing one '<').", + "title": "Use done < <(cmd) to redirect from process substitution (currently missing one <).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1143.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1143.html index a547a84..e261015 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1143.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1143.html @@ -1,13 +1,13 @@

    This backslash is part of a comment and does not continue the line.

    Problematic code

    -
    sed \
    +
    sed \
       -e "s/HOST/$HOSTNAME/g"   \
     # -e "s/USER/$USER/g"       \
       -e "s/ARCH/$(uname -m)/g" \
       "$buildfile"
     

    Correct code

    -
    sed \
    +
    sed \
       -e "s/HOST/$HOSTNAME/g"   \
       -e "s/ARCH/$(uname -m)/g" \
       "$buildfile"
    @@ -16,7 +16,7 @@ 

    Correct code

    # -e "s/USER/$USER/g" \

    or using backticked, inlined comments:

    -
    sed \
    +
    sed \
       -e "s/HOST/$HOSTNAME/g"   \
     `# -e "s/USER/$USER/g"`     \
       -e "s/ARCH/$(uname -m)/g" \
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1144.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1144.html
    index d86cc50..785f71f 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1144.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1144.html
    @@ -1 +1,16 @@
    -

    external-sources can only be enabled in .shellcheckrc, not in individual files.

    \ No newline at end of file +

    external-sources can only be enabled in .shellcheckrc, not in individual files.

    +

    Problematic code

    +
    # shellcheck external-sources=true
    +source /dev/zero
    +
    +

    Correct code

    +

    Add external-sources=true to .shellcheckrc

    +

    Rationale

    +

    Due to its origins as an online tool, ShellCheck will by default run in a sandbox where it only reads the files explicitly named on the command line.

    +

    The external-sources [[directive]] allows disabling this, but must be specified in .shellcheckrc. This is because the sandbox would be useless if the sandboxed script can disable it for itself.

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1145.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1145.html index 8e29335..1e933d8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1145.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1145.html @@ -1 +1,16 @@ -

    Unknown external-sources value. Expected true/false.

    \ No newline at end of file +

    Unknown external-sources value. Expected true/false.

    +

    Problematic code

    +

    .shellcheckrc:

    +
    external-sources=maybe
    +
    +

    Correct code

    +
    external-sources=true
    +
    +

    Rationale

    +

    The external-sources [[directive]] expects a value of true or false.

    +

    Exceptions

    +

    None.

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.html new file mode 100644 index 0000000..1569a5c --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.html @@ -0,0 +1 @@ +

    Unknown extended-analysis value. Expected true/false.

    \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.json new file mode 100644 index 0000000..15d3716 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC1146.json @@ -0,0 +1,14 @@ +{ + "title": "Unknown extended-analysis value. Expected true\/false.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Blocker" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2000.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2000.html index fc62b29..1d82c7c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2000.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2000.html @@ -1,9 +1,9 @@ -

    See if you can use ${#variable} instead

    -

    ${#variable} will be equal to the number of characters in "${variable}"

    -

    This is the same result as "$( echo "$variable" | wc -m )" -When "$variable" only contains single-byte characters, it's also the same as "$( echo "$variable" | wc -c )"

    +

    See if you can use ${#variable} instead

    +

    ${#variable} will be equal to the number of characters in "${variable}"

    +

    This is the same result as "$( echo "$variable" | wc -m )" +When "$variable" only contains single-byte characters, it's also the same as "$( echo "$variable" | wc -c )"

    example code

    -
    #!/usr/bin/env  bash
    +
    #!/usr/bin/env  bash
     
     if [ "$( echo "$1" | wc -c )" -gt 1 ]; then
       echo "greater than 1"
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.html
    index eec608b..d0e08ce 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.html
    @@ -1,17 +1,72 @@
    -

    SC2001: See if you can use ${variable//search/replace} instead.

    +

    See if you can use ${variable//search/replace} instead.

    Problematic code

    -
    string="stirng" ; echo "$string" | sed -e "s/ir/ri/"
    +
    string="stirng" ; echo "$string" | sed -e "s/ir/ri/"
     

    Correct code

    -
    string="stirng" ; echo "${string//ir/ri}"
    +
    string="stirng" ; echo "${string//ir/ri}"
     
    +
    +

    Here is a demonstration of the different search/replace available in Bash:

    +
    var="foo foo"
    +# the following two echo's should be equivalent:
    +echo "$var" | sed 's/^foo/bar/g'
    +echo ${var/#foo/bar}
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    $varsed expressionbash equivalentresult
    foo foos/foo/bar/${var/foo/bar}bar foo
    foo foos/foo/bar/g${var//foo/bar}bar bar
    foo foos/^foo/bar/${var/#foo/bar}bar foo
    -foo foos/^foo/bar/${var/#foo/bar}-foo foo
    -foo foos/foo$/bar/${var/%foo/bar}-foo bar
    -foo foo-s/foo$/bar/${var/%foo/bar}-foo foo-

    Rationale

    -

    Let's assume somewhere earlier in your code you have put data into a variable (Ex: $string). Now you want to do a search and replace inside the contents of $string and echo the contents out. You could pass this to sed as done in the example above, but for simple substitutions, parameter expansion can do it with less overhead.

    +

    Let's assume somewhere earlier in your code, you have put data into a variable (Ex: $string). Now you want to search and replace inside the contents of $string and echo the contents out. You could pass this to sed as done in the example above, but for simple substitutions, a parameter expansion can do it with less overhead.

    Exceptions

    Occasionally a more complex sed substitution is required. For example, getting the last character of a string.

    -
    string="stirng" ; echo "$string" | sed -e "s/^.*\(.\)$/\1/"
    +
    string="stirng" ; echo "$string" | sed -e "s/^.*\(.\)$/\1/"
     
    -

    This is a bit simple for the example and there are alternative ways of doing this in the shell, but this SC2001 flags on several of my crazy complex sed commands which are beyond the scope of this example. Utilizing some of the more complex capabilities of sed is required occasionally and it is safe to ignore SC2001.

    +

    This is a bit simple for the example, and there are alternative ways of doing this in the shell, but this SC2001 flags on several of my crazy complex sed commands beyond this example's scope. Utilizing some of the more complex capabilities of sed is required occasionally, and it is safe to ignore SC2001.

    Related resources

    • Bash Manual: Shell Parameter Expansion
    • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.json index 9c35cbc..c8f134a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2001.json @@ -1,5 +1,5 @@ { - "title": "SC2001: See if you can use ${variable\/\/search\/replace} instead.", + "title": "See if you can use ${variable\/\/search\/replace} instead.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.html index 6c29a9e..5f2d3b0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.html @@ -1,14 +1,16 @@ -

      Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

      +

      Useless cat. Consider cmd < file | .. or cmd file | .. instead.

      +

      This suggestion was enabled by default up to and including ShellCheck 0.10.0.

      +

      In later versions, it is [[optional]]. It must be explicitly enabled with a [[directive]] enable=useless-use-of-cat in a # shellcheck comment or .shellcheckrc

      Problematic code

      -
      cat file | tr ' ' _ | nl
      +
      cat file | tr ' ' _ | nl
       cat file | while IFS= read -r i; do echo "${i%?}"; done
       

      Correct code

      -
      < file tr ' ' _ | nl  
      +
      < file tr ' ' _ | nl  
       while IFS= read -r i; do echo "${i%?}"; done < file
       

      Rationale

      -

      cat is a tool for con"cat"enating files. Reading a single file as input to a program is considered a Useless Use Of Cat (UUOC)#Useless_use_of_cat).

      +

      cat is a tool for con"cat"enating files. Reading a single file as input to a program is considered a Useless Use Of Cat (UUOC)#Useless_use_of_cat).

      It's more efficient and less roundabout to simply use redirection. This is especially true for programs that can benefit from seekable input, like tail or tar.

      Many tools also accept optional filenames, e.g. grep -q foo file instead of cat file | grep -q foo.

      Exceptions

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.json index 1d8dfca..537cd75 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2002.json @@ -1,5 +1,5 @@ { - "title": "Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.", + "title": "Useless cat. Consider cmd < file | .. or cmd file | .. instead.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.html index f59ebc1..bef6718 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.html @@ -1,14 +1,14 @@ -

      expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].

      +

      expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].

      Problematic code

      -
      i=$(expr 1 + 2)
      +
      i=$(expr 1 + 2)
       l=$(expr length "$var")
       

      Correct code

      -
      i=$((1+2))
      +
      i=$((1+2))
       l=${#var}
       

      WARNING: constants with a leading 0 are interpreted as octal numbers by bash, but not by expr. Then you should specify the base when a leading zero may occur:

      -
      $ x=08
      +
      $ x=08
       $ echo $(expr 1 + $x)
       9
       $ echo $((1 + $x))
      @@ -19,9 +19,10 @@ 

      Correct code

      See issue #1910

      Rationale

      To quote POSIX:

      -

      The expr utility has a rather difficult syntax [...] In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr. Newly written scripts should avoid expr in favor of the new features within the shell.

      +
      +

      The expr utility has a rather difficult syntax [...] In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in expr. Newly written scripts should avoid expr in favor of the new features within the shell.

      Exceptions

      -

      sh doesn't have a great replacement for the : operator (regex match). ShellCheck tries not to warn when using expr with :, but e.g. op=:; expr string "$op" regex will still trigger it.

      +

      sh doesn't have a great replacement for the : operator (regex match). ShellCheck tries not to warn when using expr with :, but e.g. op=:; expr string "$op" regex will still trigger it.

      Other than that, all uses of expr can be rewritten to use modern shell features instead.

      Bash has [[ string =~ regex ]], so not even expr .. : .. is necessary.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.json index cbf3351..941ff75 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2003.json @@ -1,5 +1,5 @@ { - "title": "expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].", + "title": "expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2004.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2004.html index f3b0d32..a1cd93b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2004.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2004.html @@ -1,13 +1,13 @@ -

      $/${} is unnecessary on arithmetic variables.

      +

      $/${} is unnecessary on arithmetic variables.

      Problematic code

      -
      echo $(($n + ${arr[i]}))
      +
      echo $(($n + ${arr[i]}))
       

      Correct code

      -
      echo $((n + arr[i]))
      +
      echo $((n + arr[i]))
       

      Rationale

      The $ or ${..} on regular variables in arithmetic contexts is unnecessary, and can even lead to subtle bugs. This is because the contents of $((..)) is first expanded into a string, and then evaluated as an expression:

      -
      $ a='1+1'
      +
      $ a='1+1'
       $ echo $(($a * 5))    # becomes 1+1*5
       6
       $ echo $((a * 5))     # evaluates as (1+1)*5
      @@ -15,7 +15,7 @@ 

      Rationale

      The $ is unavoidable for special variables like $1 vs 1, $# vs #. It's also required when adding modifiers to parameters expansions, like ${#var} or ${var%-}. ShellCheck does not warn about these cases.

      The $ is also required (and not warned about) when you need to specify the base for a variable value:

      -
      $ a=09
      +
      $ a=09
       $ echo $((a + 1))          # leading zero forces octal interpretation
       bash: 09: value too great for base (error token is "09")
       $ echo $((10#a + 1))
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2005.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2005.html
      index c56f0bc..4961386 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2005.html
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2005.html
      @@ -1,22 +1,22 @@
       

      Useless echo? Instead of echo $(cmd), just use cmd

      Problematic code

      -
      echo "$(whoami)"
      +
      echo "$(whoami)"
       

      Correct code

      -
      whoami
      +
      whoami
       

      Rationale

      -

      ShellCheck found the unnecessary construct echo "$(somecommand here)".

      -

      This is generally due to a misunderstanding about what echo does. It has no role in "showing on screen" or similar, but simply writes a string to standard output. This is also how all other programs output data.

      -

      echo "$(somecommand)" will capture the output somecommand writes to standard output and write it to standard output, where it was already going. At best this is a no-op, but it may have several other negative effects:

      +

      ShellCheck found the unnecessary construct echo "$(somecommand here)".

      +

      This is generally due to a misunderstanding about what echo does. It has no role in "showing on screen" or similar, but simply writes a string to standard output. This is also how all other programs output data.

      +

      echo "$(somecommand)" will capture the output somecommand writes to standard output and write it to standard output, where it was already going. At best this is a no-op, but it may have several other negative effects:

        -
      • It disables parallel processing in pipelines, such as echo "$(find . -name '*.iso')" | xargs sha1sum which does not allow iterating files and checksumming at the same time. Similarly, users don't see incremental updates as programs run.
      • +
      • It disables parallel processing in pipelines, such as echo "$(find . -name '*.iso')" | xargs sha1sum which does not allow iterating files and checksumming at the same time. Similarly, users don't see incremental updates as programs run.
      • It introduces shell and echo related pitfalls like being unable to output the string -n, stripping NUL bytes and trailing linefeeds, and expanding escape sequences in some shells but not others.
      • -
      • It suppresses the exit code of the command, so that echo "$(grep '^user:' /etc/passwd)" no longer returns with failure when the user is not found.
      • -
      • It does not allow programs to tailor their output for terminals, such as ls vs echo "$(ls)" where the former outputs columns and colors according to user preferences, while the latter doesn't.
      • +
      • It suppresses the exit code of the command, so that echo "$(grep '^user:' /etc/passwd)" no longer returns with failure when the user is not found.
      • +
      • It does not allow programs to tailor their output for terminals, such as ls vs echo "$(ls)" where the former outputs columns and colors according to user preferences, while the latter doesn't.
      • It uses unnecessary memory to buffer up the data before writing it where it was already going.
      -

      To avoid all this, simply replace echo "$(somecommand)" with somecommand as in the example. It's shorter, faster, and more correct.

      +

      To avoid all this, simply replace echo "$(somecommand)" with somecommand as in the example. It's shorter, faster, and more correct.

      Exceptions

      If you are relying on one of the otherwise detrimental effects for correctness, you can consider one of:

      # Suppress exit code without the other negative effects
      @@ -29,6 +29,6 @@ 

      Exceptions

      cmd > file.tmp cat file.tmp -# Exactly like `echo "$(cmd)"`, but allows output like `-n` and works the same across shells -printf '%s\n' "$(cmd)" +# Exactly like `echo "$(cmd)"`, but allows output like `-n` and works the same across shells +printf '%s\n' "$(cmd)"
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2006.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2006.html index 5e5b499..3988ec2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2006.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2006.html @@ -1,9 +1,9 @@

      Use $(...) notation instead of legacy backticked `...`.

      Problematic code

      -
      echo "You are running on `uname`"
      +
      echo "You are running on `uname`"
       

      Correct code

      -
      echo "You are running on $(uname)"
      +
      echo "You are running on $(uname)"
       

      Rationale

      Backtick command substitution `...` is legacy syntax with several issues.

      @@ -13,10 +13,14 @@

      Rationale

    • It's exceptionally hard to nest.
    • $(...) command substitution has none of these problems, and is therefore strongly encouraged.

      +

      Note: The $(...) syntax was introduced in the 1989 Korn Shell (ksh). Finally, in 2011, Solaris 11 was the last operating system to switch from the Bourne Shell to the Korn Shell. After 2011, all typical shells have supported the POSIX $(...) notation.

      Exceptions

      -

      None.

      +

      Related resources

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.html index 56bbd1a..a8bbc59 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.html @@ -1,10 +1,10 @@ -

      Use $((..)) instead of deprecated $[..]

      +

      Use $((..)) instead of deprecated $[..].

      Problematic code

      -
      n=1
      +
      n=1
       n=$[n+1]
       

      Correct code

      -
      n=1
      +
      n=1
       n=$((n+1))
       

      Rationale

      @@ -13,5 +13,5 @@

      Exceptions

      None.

      See also

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.json index 6a80822..9b5707b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2007.json @@ -1,5 +1,5 @@ { - "title": "Use $((..)) instead of deprecated $[..]", + "title": "Use $((..)) instead of deprecated $[..].", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2008.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2008.html index 9c5c9f5..4cd3007 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2008.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2008.html @@ -1,9 +1,10 @@ -

      echo doesn't read from stdin, are you sure you should be piping to it?

      +

      echo doesn't read from stdin, are you sure you should be piping to it? +Note: Removed in v0.4.7 - 2020-03-07

      Problematic code

      -
      find . | echo
      +
      find . | echo
       

      Correct code

      -
      find .
      +
      find .
       

      Rationale

      You are piping command output to echo, but echo ignores all piped input.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.html index 5917dc8..8b238fe 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.html @@ -1,21 +1,22 @@ -

      SC2009 Consider using pgrep instead of grepping ps output.

      +

      Consider using pgrep instead of grepping ps output.

      Problematic Code

      -
      ps ax | grep -v grep | grep "$service" > /dev/null
      +
      ps ax | grep -v grep | grep "$service" > /dev/null
       

      Correct Code

      -
      pgrep -f "$service" > /dev/null
      +
      pgrep -f "$service" > /dev/null
       

      Rationale

      If you are just after a pid from a running program, then pgrep is a much safer alternative. Especially if you are also looking for a pid belonging to a certain user or group. All of the parameters are in one command and it can eliminate multiple greps, cuts, seds, awks, etc.

      If you want a field that's not the pid, consider doing this through ps + pgrep instead of ps + grep:

      for pid in $(pgrep '^python$')
       do
      -  user=$(ps -o user= -p "$pid")
      -  echo "The process $pid is run by $user"
      +  user=$(ps -o user= -p "$pid")
      +  echo "The process $pid is run by $user"
       done
       

      This is more robust than ps .. | grep python | cut .. because it does not try to match against unrelated fields, such as if the user's name was pythonguru.

      Exceptions

      +

      pgrep is not POSIX. Please [[ignore]] this warning if you are targeting POSIX userlands.

      You can [[ignore]] this error if you are trying to match against something that pgrep doesn't support:

      # pgrep does not support filtering by 'nice' value
       # shellcheck disable=SC2009
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.json
      index cd3ab34..996cfc5 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.json
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2009.json
      @@ -1,5 +1,5 @@
       {
      -  "title": "SC2009 Consider using pgrep instead of grepping ps output.",
      +  "title": "Consider using pgrep instead of grepping ps output.",
         "type": "CODE_SMELL",
         "status": "ready",
         "remediation": {
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2010.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2010.html
      index 687b532..cdc9f4b 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2010.html
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2010.html
      @@ -1,15 +1,12 @@
      -

      Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames.

      +

      Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames.

      Problematic code

      -
      ls /directory | grep mystring
      +
      ls /directory | grep mystring
       

      or

      -
      rm $(ls | grep -v '\.c$')
      +
      rm $(ls | grep -v '\.c$')
       

      Correct code

      -
      ls /directory/*mystring*
      -
      -

      or

      -
      # BASH
      +
      # BASH
       shopt -s extglob
       rm -- !(*.c)
       
      @@ -26,7 +23,7 @@ 

      Rationale

      Parsing ls is generally a bad idea because the output is fragile and human readable. To better handle non-alphanumeric filenames, use a glob. If you need more advanced matching than a glob can provide, use a for loop.

      Exceptions

        -
      • ls has sorting options that are tricky to get right with other commands. If a specific order of files is needed, ls \<sort options> | grep might be the best alternative.
      • +
      • ls has sorting options that are tricky to get right with other commands. If a specific order of files is needed, ls <sort options> | grep might be the best alternative.
      • network shares like AFS behave much faster using ls

      Related resources

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2011.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2011.html index bf95b19..56b6677 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2011.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2011.html @@ -1,11 +1,11 @@

      Use find -print0 or find -exec to better handle non-alphanumeric filenames.

      Problematic code

      -
      ls | xargs -n1 wc -w
      +
      ls | xargs -n1 wc -w
       

      Correct code

      -
      find . -maxdepth 1 -print0 | xargs -0 -n1 wc -w
      +
      find . -maxdepth 1 -print0 | xargs -0 -n1 wc -w
       
      -
      find . -maxdepth 1 -exec wc -w {} \;
      +
      find . -maxdepth 1 -exec wc -w {} \;
       

      Rationale

      Using -print0 separates each output with a NUL character, rather than a newline, which is safer to pipe into xargs. Alternatively using -exec avoids the problem of piping and parsing filenames in the first place.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2012.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2012.html index 0cfb0c9..2ac6921 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2012.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2012.html @@ -1,20 +1,19 @@

      Use find instead of ls to better handle non-alphanumeric filenames.

      Problematic code

      -
      ls -l | grep " $USER " | grep '\.txt$'
      +
      ls -l | grep " $USER " | grep '\.txt$'
       
      -
      NUMGZ="$(ls -l *.gz | wc -l)"
      +
      NUMGZ="$(ls -l *.gz | wc -l)"
       

      Correct code

      -
      find . -maxdepth 1 -name '*.txt' -user "$USER"  # Using the names of the files
      +
      find ./*.txt -user "$USER"  # Using the names of the files
       
      -
      gz_files=(*.gz)
      +
      gz_files=(*.gz)
       numgz=${#gz_files[@]} # Sometimes, you just need a count
      -`
       

      Rationale

      -

      ls is only intended for human consumption: it has a loose, non-standard format and may "clean up" filenames to make output easier to read.

      +

      ls is only intended for human consumption: it has a loose, non-standard format and may "clean up" filenames to make output easier to read.

      Here's an example:

      -
      $ ls -l
      +
      $ ls -l
       total 0
       -rw-r----- 1 me me 0 Feb  5 20:11 foo?bar
       -rw-r----- 1 me me 0 Feb  5  2011 foo?bar
      @@ -23,6 +22,7 @@ 

      Rationale

      It shows three seemingly identical filenames, and did you spot the time format change? How it formats and what it redacts can differ between locale settings, ls version, and whether output is a tty.

      Tips for replacing ls with find

      Just the filenames, ma'am

      +

      (Note that -maxdepth is not POSIX, but can be simulated by having the expression call -prune on all directories it finds, e.g. find ./* -prune -print)

      ls can usually be replaced by find if it's just the filenames, or a count of them, that you're after. Note that if you are using ls to get at the contents of a directory, a straight substitution of find may not yield the same results as ls. Here is an example:

      $ ls -c1 .snapshot
       rnapdev1-svm_4_05am_6every4hours.2019-04-01_1605
      @@ -56,8 +56,8 @@ 

      Just the filenames, ma'am

      .snapshot/snapmirror.1501b4aa-3f82-11e8-9c31-00a098cef13d_2147868328.2019-04-01_190000

      Note: If the directory argument to find is a fully expressed path (/home/somedir/.snapshot), then you should use basename on the -name filter:

      -
      $ theDir="$HOME/.snapshot"
      -$ find "$theDir" -maxdepth 1 ! -name "$(basename $theDir)"
      +
      $ theDir="$HOME/.snapshot"
      +$ find "$theDir" -maxdepth 1 ! -name "$(basename $theDir)"
       /home/matt/.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0005
       /home/matt/.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0405
       /home/matt/.snapshot/rnapdev1-svm_4_05am_6every4hours.2019-04-02_0805
      @@ -67,6 +67,6 @@ 

      Just the filenames, ma'am

      /home/matt/.snapshot/snapmirror.1501b4aa-3f82-11e8-9c31-00a098cef13d_2147868328.2019-04-01_190000

      All the other info

      -

      If trying to parse out any other fields, first see whether stat (GNU, OS X, FreeBSD) or find -printf (GNU) can give you the data you want directly. When trying to determine file size, try: wc -c. This is more portable as wc is a mandatory unix command, unlike stat and find -printf. It may be slower as unoptimized wc -c may read the entire file rather than just checking its properties. On some systems, wc -c adds whitespace to the file size which can be trimmed by double expansion: $(( $(wc -c < "filename") ))

      +

      If trying to parse out any other fields, first see whether stat (GNU, OS X, FreeBSD) or find -printf (GNU) can give you the data you want directly. When trying to determine file size, try: wc -c. This is more portable as wc is a mandatory unix command, unlike stat and find -printf. It may be slower as unoptimized wc -c may read the entire file rather than just checking its properties. On some systems, wc -c adds whitespace to the file size which can be trimmed by double expansion: $(( $(wc -c < "filename") ))

      Exceptions

      -

      If the information is intended for the user and not for processing (ls -l ~/dir | nl; echo "Ok to delete these files?") you can ignore this error with a [[directive]].

      +

      If the information is intended for the user and not for processing (ls -l ~/dir | nl; echo "Ok to delete these files?") you can ignore this error with a [[directive]].

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.html index 3dca641..09f38a1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.html @@ -1,45 +1,52 @@ -

      To read lines rather than words, pipe/redirect to a 'while read' loop.

      +

      To read lines rather than words, pipe/redirect to a while read loop.

      Problematic code

      -
      for line in $(cat file | grep -v '^ *#')
      +
      for line in $(cat file | grep -v '^ *#')
       do
         echo "Line: $line"
       done
       

      Correct code

      -
      grep -v '^ *#' < file | while IFS= read -r line
      +
      grep -v '^ *#' file | while IFS= read -r line
       do
         echo "Line: $line"
       done
       

      or without a subshell (bash, zsh, ksh):

      -
      while IFS= read -r line
      +
      while IFS= read -r line
       do
         echo "Line: $line"
      -done < <(grep -v '^ *#' < file)
      +done < <(grep -v '^ *#' file)
       

      or without a subshell, with a pipe (more portable, but write a file on the filesystem):

      -
      mkfifo mypipe
      -grep -v '^ *#' < file > mypipe &
      +
      mkfifo mypipe
      +grep -v '^ *#' file > mypipe &
       while IFS= read -r line
       do
         echo "Line: $line"
       done < mypipe
       rm mypipe
       
      +

      NOTE: grep -v '^ *#' is a placeholder example and not needed. To just loop through a file:

      +
      while IFS= read -r line
      +do
      +  echo "Line: $line"
      +done < file
      +# or: done <<< "$variable"
      +

      Rationale

      For loops by default (subject to $IFS) read word by word. Additionally, glob expansion will occur.

      Given this text file:

      -
      foo *
      +
      foo *
       bar
       

      The for loop will print:

      -
      Line: foo
      +
      Line: foo
       Line: aardwark.jpg
       Line: bullfrog.jpg
       ...
       

      The while loop will print:

      -
      Line: foo *
      +
      Line: foo *
       Line: bar
       

      Exceptions

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.json index bf0b8f8..e274b21 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2013.json @@ -1,5 +1,5 @@ { - "title": "To read lines rather than words, pipe\/redirect to a 'while read' loop.", + "title": "To read lines rather than words, pipe\/redirect to a while read loop.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2014.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2014.html index ed37fc4..72d4c29 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2014.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2014.html @@ -1,14 +1,14 @@

      This will expand once before find runs, not per file found.

      Problematic code

      -
      find . -name '*.tar' -exec tar xf {} -C "$(dirname {})" \;
      +
      find . -name '*.tar' -exec tar xf {} -C "$(dirname {})" \;
       

      Correct code

      -
      find . -name '*.tar' -exec sh -c 'tar xf "$1" -C "$(dirname "$1")"' _ {} \;
      +
      find . -name '*.tar' -exec sh -c 'tar xf "$1" -C "$(dirname "$1")"' _ {} \;
       

      Rationale

      Bash evaluates any command substitutions before the command they feature in is executed. In this case, the command is find. This means that $(dirname {}) will run before find runs, and not while find runs.

      To run shell code for each file, we can write a tiny script and inline it with sh -c. We add _ as a dummy argument that becomes $0, and a filename argument that becomes $1 in the inlined script:

      -
      $ sh -c 'echo "$1 is in $(dirname "$1")"' _ "mydir/myfile"
      +
      $ sh -c 'echo "$1 is in $(dirname "$1")"' _ "mydir/myfile"
       mydir/myfile is in mydir
       

      This command can be executed by find -exec, with {} as the filename argument. It executes shell which interprets the inlined script once for each file. Note that the inlined script is single quoted, again to ensure that the expansion does not happen prematurely .

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2015.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2015.html index 331c2d8..c74e4b5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2015.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2015.html @@ -1,9 +1,9 @@ -

      Note that A && B || C is not if-then-else. C may run when A is true.

      +

      Note that A && B || C is not if-then-else. C may run when A is true.

      Problematic code

      -
      [[ $dryrun ]] && echo "Would delete file" || rm file
      +
      [[ $dryrun ]] && echo "Would delete file" || rm file
       

      Correct code

      -
      if [[ $dryrun ]]
      +
      if [[ $dryrun ]]
       then
         echo "Would delete file"
       else
      @@ -18,7 +18,7 @@ 

      Rationale

      If an if clause is used instead, this problem is avoided.

      Boring detail

      We can think of the example above as

      -
      ((([[ $dryrun ]]) && echo "Would delete file") || rm file)
      +
      ((([[ $dryrun ]]) && echo "Would delete file") || rm file)
       

      expressing the left-associativity of the && || operators.

      Whenever a command (strictly, a pipeline) succeeds or fails, the execution proceeds following the next && (for success) or || (for failure). (More strictly, the parentheses should be replaced with { command; } to avoid making a subshell, but that's ugly and boring.)

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2016.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2016.html index 1dc2290..aff0107 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2016.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2016.html @@ -1,17 +1,17 @@

      Expressions don't expand in single quotes, use double quotes for that.

      Problematic code

      -
      name=World
      -echo 'Hello $name'   # Outputs Hello $name
      +
      name=World
      +echo 'Hello $name'   # Outputs Hello $name
       

      Correct code

      -
      name=World
      +
      name=World
       echo "Hello $name"   # Outputs Hello World
       

      Rationale

      ShellCheck found an expansion like $var, $(cmd), or `cmd` in single quotes.

      Single quotes express all such expansions. If you want the expression to expand, use double quotes instead.

      If switching to double quotes would require excessive escaping of other metacharacters, note that you can mix and match quotes in the same shell word:

      -
      dialog --msgbox "Filename $file may not contain any of: "'`&;"\#%$' 10 70
      +
      dialog --msgbox "Filename $file may not contain any of: "'`&;"\#%$' 10 70
       

      Exceptions

      If you know that you want the expression literally without expansion, you can [[ignore]] this message:

      @@ -19,12 +19,21 @@

      Exceptions

      # shellcheck disable=SC2016 echo 'PATH=$PATH:/usr/local/bin' >> ~/.bashrc
      +
      # We also want this variable to expand "$BASH_SOURCE:$LINE..." during an execution trace. 
      +# shellcheck disable=SC2016
      +PS4='+$BASH_SOURCE:$LINENO:$FUNCNAME: '
      +
      +
      # We want to control which environment variables envsubst replaces
      +# shellcheck disable=SC2016
      +envsubst '${SERVICE_HOST}:${SERVICE_PORT}' config.template > config
      +

      ShellCheck also does not warn about escaped expansions in double quotes:

      -
      echo "PATH=\$PATH:/usr/local/bin" >> ~/.bashrc
      +
      echo "PATH=\$PATH:/usr/local/bin" >> ~/.bashrc
       

      This suggestion is primarily meant to help newbies who assume single and double quotes are basically the same, like in Python and JavaScript. It's not at all meant to discourage experienced users from using single quotes in general. If you are well aware of the difference, please do not hesitate to permanently disable this suggestion with disable=SC2016 in your .shellcheckrc.

      ShellCheck tries to increase the signal-to-noise ratio of this warning by ignoring certain well known commands that frequently expect literal dollar signs, such as sh and perl. However, there's a long tail of less common commands and flags that also frequently expect $s, and it's not in ShellCheck's scope to try to keep track of them all. When you come across such a command, please [[ignore]] the suggestion, either permanently or for that one instance.

      Related resources

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2017.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2017.html index 35666b0..697e93d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2017.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2017.html @@ -1,9 +1,9 @@ -

      Increase precision by replacing a/b*c with a*c/b.

      +

      Increase precision by replacing a/b*c with a*c/b.

      Problematic code

      -
      percent=$((count/total*100))
      +
      percent=$((count/total*100))
       

      Correct code

      -
      percent=$((count*100/total))
      +
      percent=$((count*100/total))
       

      Rationale

      If integer division is performed before multiplication, the intermediate result will be truncated causing a loss of precision.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.html index a833081..b0be275 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.html @@ -1,19 +1,17 @@ -

      Use '[:lower:]' to support accents and foreign alphabets.

      +

      Use [:lower:] to support accents and foreign alphabets.

      Problematic code

      -
      PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
      +
      PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
       

      Correct code

      -
      PLATFORM="$(uname -s | tr '[:upper:]' '[:lower:]')"
      +
      PLATFORM="$(uname -s | tr '[:upper:]' '[:lower:]')"
       

      Rationale

      -

      A-Z and a-z are commonly intended to mean "all uppercase" and "all lowercase letters" respectively. This ignores accented characters in English, and foreign characters in other languages:

      -
      $ tr 'a-z' 'A-Z' <<< "My fiancée ordered a piña colada."
      -MY FIANCéE ORDERED A PIñA COLADA.
      -
      +

      A-Z and a-z are commonly intended to mean "all uppercase" and "all lowercase letters" respectively. This ignores accented characters in English, and foreign characters in other languages:

      +
      $ tr 'a-z' 'A-Z' <<< "My fiancée ordered a piña colada."
      +MY FIANCéE ORDERED A PIñA COLADA.

      Instead, you can use [:lower:] and [:upper:] to explicitly specify case:

      -
      $ tr '[:lower:]' '[:upper:]' <<< "My fiancée ordered a piña colada."
      -MY FIANCÉE ORDERED A PIÑA COLADA.
      -
      +
      $ tr '[:lower:]' '[:upper:]' <<< "My fiancée ordered a piña colada."
      +MY FIANCÉE ORDERED A PIÑA COLADA.

      Exceptions

      If you don't want a-z to match é or A-Z to match Ñ, you can ignore this message.

      As of 2019-09-08, BusyBox tr does not support character classes, so you would have to ignore this message.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.json index cfbd8fa..6e2d3af 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2018.json @@ -1,5 +1,5 @@ { - "title": "Use '[:lower:]' to support accents and foreign alphabets.", + "title": "Use [:lower:] to support accents and foreign alphabets.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.html index cd5dccd..edf29f9 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.html @@ -1,2 +1,2 @@ -

      Use '[:upper:]' to support accents and foreign alphabets.

      +

      Use [:upper:] to support accents and foreign alphabets.

      See the equivalent warning for lowercase matching: [[SC2018]]

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.json index b279c15..e63188e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2019.json @@ -1,5 +1,5 @@ { - "title": "Use '[:upper:]' to support accents and foreign alphabets.", + "title": "Use [:upper:] to support accents and foreign alphabets.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2020.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2020.html index b83f1db..fb98a2a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2020.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2020.html @@ -1,13 +1,13 @@ -

      tr replaces sets of chars, not words (mentioned due to duplicates).

      +

      tr replaces sets of chars, not words (mentioned due to duplicates).

      Problematic code

      -
      echo 'hello world' | tr 'hello' 'goodbye'
      +
      echo 'hello world' | tr 'hello' 'goodbye'
       

      Correct code

      -
      echo 'hello world' | sed -e 's/hello/goodbye/g'
      +
      echo 'hello world' | sed -e 's/hello/goodbye/g'
       

      Rationale

      tr is for transliteration, turning some characters into other characters. It doesn't match strings or words, only individual characters.

      -

      In this case, it transliterates h->g, e->o, l->d, o->y, resulting in the string "goddb wbrdd" instead of "goodbye world".

      +

      In this case, it transliterates h->g, e->o, l->d, o->y, resulting in the string "goddb wbrdd" instead of "goodbye world".

      The solution is to use a tool that does string search and replace, such as sed.

      Exceptions

      None.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2021.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2021.html index cfe8f0b..0acc331 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2021.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2021.html @@ -1,9 +1,9 @@ -

      Don't use [] around ranges in tr, it replaces literal square brackets.

      +

      Don't use [] around ranges in tr, it replaces literal square brackets.

      Problematic code

      -
      tr -cd '[a-z]'
      +
      tr -cd '[a-z]'
       

      Correct code

      -
      tr -cd 'a-z'
      +
      tr -cd 'a-z'
       

      Rationale

      Ancient System V tr required brackets around operands, but modern implementations including POSIX, GNU, OS X and *BSD instead treat them as literals.

      @@ -11,3 +11,5 @@

      Rationale

      Exceptions

      If you do want to replace literal square brackets, reorder the expression (e.g. a-z[] to make it clear that the brackets are not special).

      ShellCheck does not warn about correct usage of [..] in character and equivalence classes like [:lower:] and [=e=].

      +

      Note

      +

      Busybox requires CONFIG_FEATURE_TR_CLASSES for this to work. If busybox was compiled without this feature, character classes will obviously not be available, and the correct usage is to use a-z, not [a-z].

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.html index 00d3732..c4fec4c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.html @@ -1,14 +1,14 @@ -

      Note that unlike globs, o* here matches 'ooo' but not 'oscar'

      +

      Note that unlike globs, o* here matches ooo but not oscar.

      Problematic code

      -
      grep 'foo*'
      +
      grep 'foo*'
       

      when wanting to match food and foosball, but not mofo or keyfob.

      Correct code

      -
      grep '^foo'
      +
      grep '^foo'
       

      Rationale

      -

      As a glob, foo* means "Any string starting with foo", e.g. food and foosball.

      -

      As a regular expression, "foo*" means "f followed by 1 or more o's, anywhere", e.g. "mofo" or "keyfob".

      +

      As a glob, foo* means "Any string starting with foo", e.g. food and foosball.

      +

      As a regular expression, "foo*" means "f followed by 1 or more o's, anywhere", e.g. "mofo" or "keyfob".

      This construct is way more common as a glob than as a regex, so ShellCheck notifies you about it.

      Exceptions

      If you're aware of the above, you can ignore this message. If you'd like shellcheck to be quiet, use a [[directive]] or 'fo[o]*'.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.json index 4b4091c..6eb4388 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2022.json @@ -1,5 +1,5 @@ { - "title": "Note that unlike globs, o* here matches 'ooo' but not 'oscar'", + "title": "Note that unlike globs, o* here matches ooo but not oscar.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.html index 4d9d657..6f46f2c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.html @@ -1 +1,12 @@ -

      The shell may override 'time' as seen in man time(1). Use 'command time ..' for that one.

      \ No newline at end of file +

      The shell may override time as seen in man time(1). Use command time .. for that one.

      +

      Problematic code

      +
      time -some some
      +
      +

      Correct code

      +
      command time -some some
      +
      +

      Rationale

      +

      time is a built-in command. +If you would like to use time from $PATH, you need to use command to execute it as a regular command.

      +

      Exceptions

      +

      None

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.json index 0a895b1..ce71a5f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2023.json @@ -1,5 +1,5 @@ { - "title": "The shell may override 'time' as seen in man time(1). Use 'command time ..' for that one.", + "title": "The shell may override time as seen in man time(1). Use command time .. for that one.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2024.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2024.html index 481c76f..f66c072 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2024.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2024.html @@ -1,6 +1,6 @@

      sudo doesn't affect redirects. Use ..| sudo tee file

      -

      or "Use ..| sudo tee -a file" instead of >> to append.

      -

      or "Use sudo cat file | .." instead of < to read.

      +

      or "Use ..| sudo tee -a file" instead of >> to append.

      +

      or "Use sudo cat file | .." instead of < to read.

      Problematic code

      # Write to a file
       sudo echo 3 > /proc/sys/vm/drop_caches
      @@ -24,10 +24,10 @@ 

      Correct code

      Rationale

      Redirections are performed by the current shell before sudo is started. This means that it will use the current shell's user and permissions to open and read from or write to the file.

        -
      • To read from a file that requires additional privileges, you can replace sudo command < file with sudo cat file | command.
      • +
      • To read from a file that requires additional privileges, you can replace sudo command < file with sudo cat file | command.
      • To write to a file that requires additional privileges, you can replace sudo command > file with command | sudo tee file > /dev/null
      • -
      • To append to a file, use the above with tee -a.
      • -
      • If the file does not require special privileges but the command does, then you are already doing the right thing: please [[ignore]] the message.
      • +
      • To append to a file, use the above with tee -a.
      • +
      • If the file does not require special privileges but the command does, then you are already doing the right thing: please [[ignore]] the message.

      The substitutions work by having a command open the file for reading or writing, instead of relying on the current shell. Since the command is run with elevated privileges, it will have access to files that the current user does not.

      Note: there is nothing special about tee. It's just the simplest command that can both truncate and append to files without help from the shell. Here are equivalent alternatives:

      @@ -36,7 +36,7 @@

      Rationale

      echo 'data' | sudo sed 'w file'

      Appending:

      -
      echo 'data' | sudo awk '{ print $0 >> "file" }'
      +
      echo 'data' | sudo awk '{ print $0 >> "file" }'
       echo 'data' | sudo sh -c 'cat >> file'
       

      Exceptions

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.html index 57c44a6..3941aa3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.html @@ -1,9 +1,9 @@ -

      Make sure all escape sequences are enclosed in \[..\] to prevent line wrapping issues

      +

      Make sure all escape sequences are enclosed in \[..\] to prevent line wrapping issues.

      Problematic code

      -
      PS1='\e[36m\$ \e(B\e[m'
      +
      PS1='\e[36m\$ \e(B\e[m'
       

      Correct code

      -
      PS1='\[\e[36m\]\$ \[\e(B\e[m\]'
      +
      PS1='\[\e[36m\]\$ \[\e(B\e[m\]'
       

      Rationale

      Bash is unable to determine exactly which parts of your prompt are text and which are terminal codes. You have to help it by wrapping invisible control codes in \[..\] (and ensuring that visible characters are not wrapped in \[..\]).

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.json index e60eb5a..5c498af 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2025.json @@ -1,5 +1,5 @@ { - "title": "Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues", + "title": "Make sure all escape sequences are enclosed in \\[..\\] to prevent line wrapping issues.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.html index 81cfb3e..e17f8a2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.html @@ -1,25 +1,25 @@ -

      This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?

      +

      This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead?

      Problematic code

      -
      alias server_uptime='ssh $host 'uptime -p''
      +
      alias server_uptime='ssh $host 'uptime -p''
       

      Correct code

      -
      alias server_uptime='ssh $host '"'uptime -p'"
      +
      alias server_uptime='ssh $host '"'uptime -p'"
       

      Rationale

      In the first case, the user has four single quotes on a line, wishfully hoping that the shell will match them up as outer quotes around a string with literal single quotes:

      -
      #                   v--------match--------v
      -alias server_uptime='ssh $host 'uptime -p''
      +
      #                   v--------match--------v
      +alias server_uptime='ssh $host 'uptime -p''
       #                              ^--match--^
       

      The shell, meanwhile, always terminates single quoted strings at the first possible single quote:

      -
      #                   v---match--v
      -alias server_uptime='ssh $host 'uptime -p''
      +
      #                   v---match--v
      +alias server_uptime='ssh $host 'uptime -p''
       #                                        ^^
       

      Which is the same thing as alias server_uptime='ssh $host uptime' -p.

      There is no way to nest single quotes. However, single quotes can be placed literally in double quotes, so we can instead concatenate a single quoted string and a double quoted string:

      -
      #                   v--match---v
      -alias server_uptime='ssh $host '"'uptime -p'"
      +
      #                   v--match---v
      +alias server_uptime='ssh $host '"'uptime -p'"
       #                               ^---match---^
       

      This results in an alias with embedded single quotes.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.json index 49f9e8d..279c41d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2026.json @@ -1,5 +1,5 @@ { - "title": "This word is outside of quotes. Did you intend to 'nest '''single quotes''' instead'?", + "title": "This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead?", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2027.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2027.html index ee4c243..acb584d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2027.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2027.html @@ -1,14 +1,14 @@

      The surrounding quotes actually unquote this. Remove or escape them.

      Problematic code

      -
      echo "You enter "$HOSTNAME". You can smell the wumpus." >> /etc/issue
      +
      echo "You enter "$HOSTNAME". You can smell the wumpus." >> /etc/issue
       

      Correct code

      -
      echo "You enter $HOSTNAME. You can smell the wumpus." >> /etc/issue
      +
      echo "You enter $HOSTNAME. You can smell the wumpus." >> /etc/issue
       

      Rationale

      Always quoting variables and command expansions is good practice, but blindly putting quotes left and right of them is not.

      In this case, ShellCheck has noticed that the quotes around the expansion are unquoting it, because the left quote is terminating an existing double quoted string, while the right quote starts a new one:

      -
      echo "You enter "$HOSTNAME". You can smell the wumpus."
      +
      echo "You enter "$HOSTNAME". You can smell the wumpus."
            |----------|         |---------------------------|
               Quoted   No quotes           Quoted
       
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2028.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2028.html index 4b28acc..b4a566e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2028.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2028.html @@ -1,16 +1,16 @@ -

      echo won't expand escape sequences. Consider printf.

      +

      echo won't expand escape sequences. Consider printf.

      Problematic code

      -
      echo "Name:\t$value"
      +
      echo "Name:\t$value"
       

      Correct code

      -
      printf 'Name:\t%s\n' "$value"
      +
      printf 'Name:\t%s\n' "$value"
       

      Rationale

      Backslash escapes like \t and \n are not expanded by echo, and become literal backslash-t, backslash-n.

      printf does expand these sequences, and should be used instead.

      Other, non-portable methods include echo -e '\t' and echo $'\t'. ShellCheck will warn if this is used in a script with shebang #!/bin/sh.

      If you actually wanted a literal backslash-t, use

      -
      echo "\\t"
      +
      echo "\\t"
       

      Exceptions

      None

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2029.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2029.html index 1d3c162..a7c9ea7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2029.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2029.html @@ -1,16 +1,16 @@

      Note that, unescaped, this expands on the client side.

      Problematic code

      -
      ssh host "echo $HOSTNAME"
      +
      ssh host "echo $HOSTNAME"
       

      Correct code

      -
      ssh host "echo \$HOSTNAME"
      +
      ssh host "echo \$HOSTNAME"
       

      or

      -
      ssh host 'echo $HOSTNAME'
      +
      ssh host 'echo $HOSTNAME'
       

      Rationale

      Bash expands all arguments that are not escaped/singlequoted. This means that the problematic code is identical to

      -
      ssh host "echo clienthostname"
      +
      ssh host "echo clienthostname"
       

      and will print out the client's hostname, not the server's hostname.

      By escaping the $ in $HOSTNAME, it will be transmitted literally and evaluated on the server instead.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2031.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2031.html index d821e63..a6318f4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2031.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2031.html @@ -1,40 +1,64 @@

      var was modified in a subshell. That change might be lost.

      Problematic code

      There are many ways of accidentally creating subshells, but a common one is piping to a loop:

      -
      n=0
      +
      n=0
       printf "%s\n" {1..10} | while read i; do (( n+=i )); done
       echo $n
       

      Correct code

      -
      # Bash specific: process substitution. Also try shopts like lastpipe.
      +
      # Bash specific: process substitution. Also try shopts like lastpipe.
       n=0
       while read i; do (( n+=i )); done < <(printf "%s\n" {1..10})
       echo $n
       
      -

      In sh, a temp file (better if fifo or fd) can be used instead of process substitution. And if it's acceptable to do it with waiting, try Here Documents.

      +

      In sh, temporary files, FIFOs or file descriptors can be used instead. When the output of the command can be stored to a variable before entering the loop, here documents are a preferable alternative.

      +
      n=0
      +SUMMANDS="1
      +2
      +3
      +4
      +5
      +6
      +7
      +8
      +9
      +10
      +"
      +while read i; do n=$(( n + i )); done <<SUMMANDS_HEREDOC_INPUT
      +$SUMMANDS
      +SUMMANDS_HEREDOC_INPUT
      +echo $n
      +
      +

      With Bash 4.2+ you can also use shopt -s lastpipe which will change the pipe behaviour to be similar to Ksh and Zsh (see Rationale below) as long as job control is not active (e.g. inside a script):

      +
      #!/usr/bin/env bash
      +shopt -s lastpipe
      +n=0
      +printf "%s\n" {1..10} | while read i; do (( n+=i )); done
      +echo $n
      +

      Rationale

      Variables set in subshells are not available outside the subshell. This is a wide topic, and better described on the Wooledge Bash Wiki.

      Here are some constructs that cause subshells (shellcheck may not warn about all of them). In each case, you can replace subshell1 by a command or function that sets a variable, e.g. simply var=foo, and the variable will appear to be unset after the command is run. Similarly, you can replace regular with var=foo, and it will be set afterwards:

      Pipelines:

      -
      subshell1 | subshell2 | subshell3    # Bash, Dash, Ash
      -subshell1 | subshell2 | regular      # Ksh, Zsh
      +
      subshell1 | subshell2 | subshell3    # Dash, Ash, Bash (default)
      +subshell1 | subshell2 | regular      # Ksh, Zsh, Bash (with lastpipe=on and no job control)
       

      Command substitution:

      -
      regular "$(subshell1)" "`subshell2`"
      +
      regular "$(subshell1)" "`subshell2`"
       

      Process substitution:

      -
      regular <(subshell1) >(subshell2)
      +
      regular <(subshell1) >(subshell2)
       

      Some forms of grouping:

      -
      ( subshell )
      +
      ( subshell )
       { regular; }
       

      Backgrounding:

      -
      subshell1 &
      +
      subshell1 &
       subshell2 &
       

      Anything executed by external processes:

      -
      find . -exec subshell1 {} \;
      +
      find . -exec subshell1 {} \;
       find . -print0 | xargs -0 subshell2
       sudo subshell3
       su -c subshell4
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.html
      index 5f6aa37..27151a2 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.html
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.html
      @@ -1,3 +1,3 @@
      -

      Use own script or sh -c '..' to run this from su.

      +

      This function can't be invoked via su on line 42.

      (or from xargs, sudo, chroot, or other commands)

      -

      See [[SC2033]].

      +

      See companion warning [[SC2033]].

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.json index 4de4350..26b6c98 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2032.json @@ -1,5 +1,5 @@ { - "title": "Use own script or sh -c '..' to run this from su.", + "title": "This function can't be invoked via su on line 42.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.html index bc11d5f..af7bdca 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.html @@ -1,17 +1,18 @@ -

      Shell functions can't be passed to external commands.

      +

      Shell functions can't be passed to external commands. Use separate script or sh -c.

      Problematic code

      -
      foo() { bar --baz "$@"; frob --baz "$@"; };
      +
      foo() { bar --baz "$@"; frob --baz "$@"; };
       find . -exec foo {} +
       

      Correct code

      -
      find . -exec sh -c 'bar --baz "$@"; frob --baz "$@";' -- {} +
      +
      find . -exec sh -c 'bar --baz "$@"; frob --baz "$@";' -- {} +
       

      Rationale

      Shell functions are only known to the shell. External commands like find, xargs, su and sudo do not recognize shell functions.

      Instead, the function contents can be executed in a shell, either through sh -c or by creating a separate shell script as an executable file.

      Exceptions

      If you're intentionally passing a word that happens to have the same name as a declared function, you can quote it to make shellcheck ignore it, e.g.

      -
      nobody() {
      +
      nobody() {
         sudo -u "nobody" "$@"
       }
      +
       
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.json index 60ab960..f84960f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2033.json @@ -1,5 +1,5 @@ { - "title": "Shell functions can't be passed to external commands.", + "title": "Shell functions can't be passed to external commands. Use separate script or sh -c.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2034.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2034.html index 8d4c537..47388a4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2034.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2034.html @@ -1,10 +1,10 @@

      foo appears unused. Verify it or export it.

      Problematic code:

      -
      foo=42
      +
      foo=42
       echo "$FOO"
       

      Correct code:

      -
      foo=42
      +
      foo=42
       echo "$foo"
       

      Rationale:

      @@ -14,7 +14,7 @@

      foo appears unused. Verify it or export it.

      This warning may be falsely emitted when a variable is only referenced indirectly, and for variables that are intentionally unused.

      Indirection

      It's ShellCheck's intended behavior to emit this warning for any variable that is only referenced though indirection:

      -
      # foo generates a warning, even though it has five indirect references
      +
      # foo generates a warning, even though it has five indirect references
       foo=42
       name=foo
       echo "${!name} $((name))"
      @@ -30,18 +30,15 @@ 

      Indirection

      Compilers are forced to do the former, but static analysis tools generally do the latter. This includes pylint, jshint and shellcheck itself. This is a design decision meant to make the tools more helpful at the expense of some noise. For consistency and to avoid giving the impression that it should work more generally, ShellCheck does not attempt to resolve even trivial indirections.

      Intentionally unused variables

      For throwaway variables, consider using _ as a dummy:

      -
      read _ last _ zip _ _ <<< "$str"
      +
      read _ last _ zip _ _ <<< "$str"
       echo "$last, $zip"
       

      Or optionally as a prefix for dummy variables (ShellCheck >0.7.2).

      -

      ```sh ​ -read _first last _email zip _lat _lng <<< "$str" -echo "$last, $zip"

      -
      
      -For versions <= 0.7.2, the message can optionally be [[ignore]]d with a directive:
      -
      -```sh
      -# shellcheck disable=SC2034  # Unused variables left for readability
      -read first last email zip lat lng <<< "$str"
      -echo "$last, $zip"
      +
      read _first last _email zip _lat _lng <<< "$str"
      +echo "$last, $zip"
      +
      +

      For versions <= 0.7.2, the message can optionally be [[ignore]]d with a directive:

      +
      # shellcheck disable=SC2034  # Unused variables left for readability
      +read first last email zip lat lng <<< "$str"
      +echo "$last, $zip"
       
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.html index 37d003b..abf10c0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.html @@ -1,18 +1,15 @@ -

      Use ./*glob or -- \glob* so names with dashes won't become options.

      +

      Use ./*glob* or -- *glob* so names with dashes won't become options.

      Problematic code

      -
      rm *
      -
      +
      rm *

      Correct code

      -
      rm ./*
      -
      +
      rm ./*

      or

      -
      rm -- *
      -
      +
      rm -- *

      Rationale

      Since files and arguments are strings passed the same way, programs can't properly determine which is which, and rely on dashes to determine what's what.

      A file named -f (touch -- -f) will not be deleted by the problematic code. It will instead be interpreted as a command line option, and rm will even report success.

      Using ./* will instead cause the glob to be expanded into ./-f, which no program will treat as an option.

      Similarly, -- by convention indicates the end of options, and nothing after it will be treated like flags (except for some programs possibly still special casing - as e.g. stdin).

      Note that changing * to ./* in GNU Tar parameters will add ./ prefix to path names in the created archive. This may cause subtle problems (eg. to search for a specific file in archive, the ./ prefix must be specified as well). So using -- * is a safer fix for GNU Tar commands.

      -

      echo and printf does not have issues unless the glob is the first word in the command. ShellCheck 0.7.2+ does not warn for these commands.

      -

      For more information, see "Filenames and Pathnames in Shell: How to do it Correctly".

      +

      echo and printf do not have issues unless the glob is the first word in the command. ShellCheck 0.7.2+ does not warn for these commands.

      +

      For more information, see "Filenames and Pathnames in Shell: How to do it Correctly".

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.json index 52608b0..9d11af0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2035.json @@ -1,5 +1,5 @@ { - "title": "Use .\/*glob or -- \\glob* so names with dashes won't become options.", + "title": "Use .\/*glob* or -- *glob* so names with dashes won't become options.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2036.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2036.html index 869e526..26b3778 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2036.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2036.html @@ -1,9 +1,9 @@ -

      If you wanted to assign the output of the pipeline, use a=$(b | c) .

      +

      If you wanted to assign the output of the pipeline, use a=$(b | c) .

      Problematic code

      -
      sum=find | wc -l
      +
      sum=find | wc -l
       

      Correct code

      -
      sum=$(find | wc -l)
      +
      sum=$(find | wc -l)
       

      Rationale

      The intention in this code was that sum would in some way get the value of the command find | wc -l.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2037.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2037.html index d3472f9..593822e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2037.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2037.html @@ -1,9 +1,9 @@ -

      To assign the output of a command, use var=$(cmd) .

      +

      To assign the output of a command, use var=$(cmd) .

      Problematic code

      -
      var=grep -c pattern file
      +
      var=grep -c pattern file
       

      Correct code

      -
      var=$(grep -c pattern file)
      +
      var=$(grep -c pattern file)
       

      Rationale

      To assign the output of a command to a variable, use $(command substitution). Just typing a command after the = sign does not work.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2038.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2038.html index eaf0fde..1d6626e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2038.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2038.html @@ -1,9 +1,9 @@ -

      Use -print0/-0 or find -exec + to allow for non-alphanumeric filenames.

      +

      Use -print0/-0 or find -exec + to allow for non-alphanumeric filenames.

      Problematic code

      -
      find . -type f | xargs md5sum
      +
      find . -type f | xargs md5sum
       

      Correct code

      -
      find . -type f -print0 | xargs -0 md5sum
      +
      find . -type f -print0 | xargs -0 md5sum
       find . -type f -exec md5sum {} +
       

      Rationale

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2039.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2039.html index b60cb3b..09897ab 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2039.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2039.html @@ -1,57 +1,57 @@

      In POSIX sh, something is undefined.

      -

      Note: This warning has been retired in favor of individual SC3xxx warnings for each individual issue.

      +

      Note: This warning has been retired in favor of individual SC3xxx warnings for each individual issue. Removed in V0.7.2 - 2021-04-19

      You have declared that your script works with /bin/sh, but you are using features that have undefined behavior according to the POSIX specification.

      It may currently work for you, but it can or will fail on other OS, the same OS with different configurations, from different contexts (like initramfs/chroot), or in different versions of the same OS, including future updates to your current system.

      Either declare that your script requires a specific shell like #!/bin/bash or #!/bin/dash, or rewrite the script in a portable way.

      For help with rewrites, the Ubuntu wiki has a list of portability issues that broke people's #!/bin/sh scripts when Ubuntu switched from Bash to Dash. See also Bashism on wooledge's wiki. ShellCheck may not warn about all these issues.

      $'c-style-escapes'

      bash, ksh:

      -
      a=$' \t\n'
      +
      a=$' \t\n'
       

      POSIX:

      -
      a="$(printf '%b_' ' \t\n')"; a="${a%_}" # protect trailing \n
      +
      a="$(printf '%b_' ' \t\n')"; a="${a%_}" # protect trailing \n
       
      -

      Want some good news? See http://austingroupbugs.net/view.php?id=249#c590.

      +

      Want some good news? See http://austingroupbugs.net/view.php?id=249#c590.

      Testing Equality

      == operator is not supported in POSIX sh

      Bash:

      -
      if [ "$a" == "$b" ]; then
      +
      if [ "$a" == "$b" ]; then
           echo "equal"
       fi
       

      POSIX:

      -
      if [ "$a" = "$b" ]; then
      +
      if [ "$a" = "$b" ]; then
           echo "equal"
       fi
       
      -

      $"msgid"

      +

      $"msgid"

      Bash:

      -
      echo $"foo $(bar) baz"
      +
      echo $"foo $(bar) baz"
       

      POSIX:

      -
      . gettext.sh # GNU Gettext sh library
      +
      . gettext.sh # GNU Gettext sh library
       # ...
       barout=$(bar)
      -eval_gettext 'foo $barout baz' # See GNU Gettext doc for more info.
      +eval_gettext 'foo $barout baz' # See GNU Gettext doc for more info.
       

      Or you can change them to normal double quotes so you go without gettext.

      ${var:1} (substring expansion)

      -

      https://wiki.ubuntu.com/DashAsBinSh#A.24.7Bfoo:3.5B:1.5D.7D

      +

      https://wiki.ubuntu.com/DashAsBinSh#A.24.7Bfoo:3.5B:1.5D.7D

      Arithmetic for loops

      Bash:

      -
      for ((init; test; next)); do foo; done
      +
      for ((init; test; next)); do foo; done
       

      POSIX:

      -
      : $((init))
      +
      : $((init))
       while [ $((test)) -ne 0 ]; do foo; : $((next)); done
       

      Arithmetic exponentiation

      Bash:

      -
      printf "%s\n" "$(( 2**63 ))"
      +
      printf "%s\n" "$(( 2**63 ))"
       

      POSIX:

      The POSIX standard does not allow for exponents. However, you can replicate them completely built-in using a POSIX compatible function. As an example, the pow function from here.

      -
      pow() {
      +
      pow() {
           set -- "$1" "$2" 1
           while [ "$2" -gt 0 ]; do
             set -- "$1" $(($2-1)) $(($1*$3))
      @@ -62,14 +62,14 @@ 

      Arithmetic exponentiation

      }

      To compare:

      -
      $ echo "$(( 2**62 ))"
      +
      $ echo "$(( 2**62 ))"
       4611686018427387904
       $ pow 2 62
       4611686018427387904
       

      Alternatively, if you don't mind using an external program, you can use bc. Be aware though: bash and other programs may abide by a certain maximum integer that bc does not (for bash that's: 64-bit signed long int, failing back to 32-bit signed long int).

      Example:

      -
      # Note the overflow that gives a negative number
      +
      # Note the overflow that gives a negative number
       $ echo "$(( 2**63 ))"
       -9223372036854775808
       
      @@ -77,7 +77,7 @@ 

      Arithmetic exponentiation

      $ echo 2^63 | bc 9223372036854775808 -# 'bc' just keeps on going +# 'bc' just keeps on going $ echo 2^1280 | bc 20815864389328798163850480654728171077230524494533409610638224700807\ 21611934672059602447888346464836968484322790856201558276713249664692\ @@ -88,20 +88,20 @@

      Arithmetic exponentiation

      standalone ((..))

      Bash:

      -
      ((a=c+d))
      +
      ((a=c+d))
       ((d)) && echo d is true.
       

      POSIX:

      -
      : $((a=c+d)) # discard the output of the arith expn with `:` command
      +
      : $((a=c+d)) # discard the output of the arith expn with `:` command
       [ $((d)) -ne 0 ] && echo d is true. # manually check non-zero => true
       

      select loops

      It takes extra care over terminal columns to make select loop look like bash's, which generates a list with multiple items on one line, or like ls.

      It is, however, still possible to make a naive translation for select foo in bar baz; do eat; done:

      -
      while
      +
      while
         _i=0 _foo= foo=
         for _name in bar baz; do echo "$((_i+=1))) $_name"; done
      -  printf '$# '; read _foo
      +  printf '$# '; read _foo
       do
         case _foo in 1) foo=bar;; 2) foo=baz;; *) continue;; esac
         eat
      @@ -109,42 +109,43 @@ 

      select loops

      Here-strings

      Bash, ksh:

      -
      grep aaa <<< "$g"
      +
      read aaa bbb <<< $(grep foo bar)
       

      POSIX:

      -
      # not exactly the same -- <<< adds a trailing \n if $g doesn't end with \n
      -printf '%s' "$g" | grep aaa
      +
      read aaa bbb << EOF
      +$(grep foo bar)
      +EOF
       

      echo flags

      -

      See https://unix.stackexchange.com/tags/echo/info.

      +

      See https://unix.stackexchange.com/tags/echo/info.

      ${var/pat/replacement}

      Bash:

      -
      echo "${TERM/%-256*}"
      +
      echo "${TERM/%-256*}"
       

      POSIX:

      -
      echo "$TERM" | sed -e 's/-256.*$//g'
      +
      echo "$TERM" | sed -e 's/-256.*$//g'
       # Special case for this since we are matching the end (the start [#] also works):
       echo "${TERM%-256*}"
       

      printf %q

      Bash:

      -
      printf '%q ' "$@"
      +
      printf '%q ' "$@"
       

      POSIX:

      -
      # TODO: Interpret it back to printf escapes for hard-to-copy chars like \t?
      +
      # TODO: Interpret it back to printf escapes for hard-to-copy chars like \t?
       # See also: http://git.savannah.gnu.org/cgit/libtool.git/tree/gl/build-aux/funclib.sh?id=c60e054#n1029
       reuse_quote()(
         for i; do
      -    __i_quote=$(printf '%s\n' "$i" | sed -e "s/'/'\\\\''/g"; echo x)
      -    printf "'%s'" "${__i_quote%x}"
      +    __i_quote=$(printf '%s\n' "$i" | sed -e "s/'/'\\\\''/g"; echo x)
      +    printf "'%s'" "${__i_quote%x}"
         done
       )
       reuse_quote "$@"
       

      jobs flags

      The only acceptable flags under POSIX sh for jobs are -l and -p (see spec). Common flags supported by other shells are -s and -r, to check for stopped/suspended jobs and running jobs. A portable alternative is using grep or awk:

      -
      "$(jobs | awk '/(S|s)(topped|uspended)/')" # instead of jobs -s
      -"$(jobs | awk '/(R|r)(unning)/')"          # instead of jobs -r
      +
      "$(jobs | awk '/(S|s)(topped|uspended)/')" # instead of jobs -s
      +"$(jobs | awk '/(R|r)(unning)/')"          # instead of jobs -r
       

      Although the state of stopped jobs is Stopped in Bash and dash, and it's the one specified by POSIX, Suspended is also a valid alternative (but Zsh happens to not respect the capitalization, that's why we try to match suspended). Similarly, the state of running jobs is Running according to POSIX. Bash and dash respect this, but Zsh uses running.

      Redirect both stdout and stderr

      @@ -157,27 +158,32 @@

      Redirect both stdout and stderr

      No Comments / Exceptions

      Prefixing signal names with SIG

      Instead of e.g.:

      -
      trap my_handler SIGTERM
      +
      trap my_handler SIGTERM
       

      use:

      -
      trap my_handler TERM
      +
      trap my_handler TERM
       # or (`trap -l` for a list of signal numbers; not every one is portable!)
       trap my_handler 15
       

      disown

      Bash:

      -
      <command>
      +
      <command>
       disown %<command>
       

      POSIX:

      -
      nohup <command>
      +
      nohup <command>
       

      Note that while nohup can be used to achieve the same result, their semantics is different. Also note that nohup will, by default, redirect input and output.

      +

      Errors with changed codes

      +

      Some errors, currently having their own pages, were linked to this page in older ShellCheck versions.

      +

      Exception

      Depends on what your expected POSIX shell providers would use.

      Some features have POSIX proposals:

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.html index 7f36c09..6339483 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.html @@ -1,4 +1,5 @@ -

      !/bin/sh was specified, so __ is not supported, even when sh is actually bash.

      +

      #!/bin/sh was specified, so ____ is not supported, even when sh is actually bash.

      +

      Note: Removed in v0.4.2 - 2016-01-10

      The shebang indicates that the script works with /bin/sh, but you are using non-standard features that may not work with /bin/sh, even if /bin/sh is actually bash. Bash behaves differently when invoked as sh, and disabling support for the highlighted feature is one part of that.

      Specify #!/usr/bin/env bash to ensure that bash (or your shell of choice) will be used, or rewrite the script to be more portable.

      The Ubuntu wiki has a list of portability issues and suggestions on how to rewrite them.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.json index 68a61dd..8ec74a8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2040.json @@ -1,5 +1,5 @@ { - "title": "!\/bin\/sh was specified, so __ is not supported, even when sh is actually bash.", + "title": "#!\/bin\/sh was specified, so ____ is not supported, even when sh is actually bash.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2041.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2041.html index 45cda02..9bd4ce6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2041.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2041.html @@ -1,12 +1,12 @@ -

      This is a literal string. To run as a command, use $(..) instead of '..' .

      +

      This is a literal string. To run as a command, use $(..) instead of '..' .

      Problematic code

      -
      for i in 'seq 1 10'
      +
      for i in 'seq 1 10'
       do
         echo "$i"
       done
       

      Correct code

      -
      for i in $(seq 1 10)
      +
      for i in $(seq 1 10)
       do
         echo "$i"
       done
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2042.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2042.html
      index 3daad20..eb07203 100644
      --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2042.html
      +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2042.html
      @@ -1 +1,21 @@
      -

      Use spaces, not commas, to separate loop elements.

      \ No newline at end of file +

      Use spaces, not commas, to separate loop elements.

      +

      Problematic code

      +
      for f in foo,bar,baz
      +do
      +  echo "$f"
      +done
      +
      +

      Correct code

      +
      for f in foo bar baz
      +do
      +  echo "$f"
      +done
      +
      +

      Rationale

      +

      ShellCheck found a for loop where the items appeared to be delimited by commas. These will be treated as literal commas. Use spaces instead.

      +

      Exceptions

      +

      None

      +

      Related resources

      +
        +
      • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
      • +
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.html index fe3027d..eacfbb3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.html @@ -1,6 +1,6 @@ -

      This loop will only ever run once for a constant value. Did you perhaps mean to loop over dir/*, $var or $(cmd)?"

      +

      This loop will only ever run once for a constant value. Did you perhaps mean to loop over dir/*, $var or $(cmd)?

      Problematic code

      -
      for var in value
      +
      for var in value
       do
         echo "$var"
       done
      @@ -8,19 +8,19 @@ 

      Problematic code

      Correct code

      Correct code depends on what you want to do.

      To iterate over files in a directory, instead of for var in /my/dir use:

      -
      for var in /my/dir/* ; do echo "$var"; done
      +
      for var in /my/dir/* ; do echo "$var"; done
       

      To iterate over lines in a file or command output, use a while read loop instead:

      -
      mycommand | while IFS= read -r line; do echo "$line"; done
      +
      mycommand | while IFS= read -r line; do echo "$line"; done
       

      To iterate over words written to a command or function's stdout, instead of for var in myfunction, use

      -
      for var in $(myfunction); do echo "$var"; done
      +
      for var in $(myfunction); do echo "$var"; done
       

      To iterate over words in a variable, instead of for var in myvariable, use

      -
      for var in $myvariable; do echo "$var"; done
      +
      for var in $myvariable; do echo "$var"; done
       

      Rationale

      ShellCheck has detected that your for loop iterates over a single, constant value. This is most likely a bug in your code, caused by you not expanding the value in the way you want.

      You should make sure that whatever you loop over will expand into multiple words.

      Exceptions

      -

      None.

      +

      If you stylistically choose to use for loops with a single element, e.g. to align with other code or to set up for future extensibility (for target in x86_64; do ..), you can [[ignore]] this warning.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.json index 87b8eaf..54e3dea 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2043.json @@ -1,5 +1,5 @@ { - "title": "This loop will only ever run once for a constant value. Did you perhaps mean to loop over dir\/*, $var or $(cmd)?'", + "title": "This loop will only ever run once for a constant value. Did you perhaps mean to loop over dir\/*, $var or $(cmd)?", "type": "BUG", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2044.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2044.html index 3395bc1..ba99781 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2044.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2044.html @@ -1,8 +1,8 @@ -

      For loops over find output are fragile. Use find -exec or a while read loop.

      +

      For loops over find output are fragile. Use find -exec or a while read loop.

      Problematic code

      -
      for file in $(find mydir -mtime -7 -name '*.mp3')
      +
      for file in $(find mydir -mtime -7 -name '*.mp3')
       do
      -  let count++
      +  (( count++ ))
         echo "Playing file no. $count"
         play "$file"
       done
      @@ -12,22 +12,22 @@ 

      Problematic code

      Correct code

      There are many possible fixes, each with its pros and cons.

      The most general fix (that requires the least amount of thinking to apply) is having find output a \0 separated list of files and consuming them in a while read loop:

      -
      while IFS= read -r -d '' file
      +
      while IFS= read -r -d '' file
       do
      -  let count++
      +  (( count++ ))
         echo "Playing file no. $count"
         play "$file"
      -done <   <(find mydir -mtime -7 -name '*.mp3' -print0)
      +done <   <(find mydir -mtime -7 -name '*.mp3' -print0)
       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.

      If find is just matching globs recursively

      If you don't need find logic like -mtime -7 and just use it to match globs recursively (all *.mp3 files under a directory), you can instead use globstar and nullglob instead of find, and still use a for loop:

      -
      shopt -s globstar nullglob
      +
      shopt -s globstar nullglob
       for file in mydir/**/*.mp3
       do
      -  let count++
      +  (( count++ ))
         echo "Playing file no. $count"
         play "$file"
       done
      @@ -36,7 +36,7 @@ 

      If find is just matching globs recursively

      This is bash 4 specific.

      For POSIX

      If you need POSIX compliance, this is a fair approach:

      -
      find mydir ! -name "$(printf "*\n*")" -name '*.mp3' > tmp
      +
      find mydir ! -name "$(printf "*\n*")" -name '*.mp3' > tmp
       while IFS= read -r file
       do
         let count++
      @@ -46,23 +46,23 @@ 

      For POSIX

      rm tmp echo "Played $count files"
      -

      The only problem is for filenames containing line feeds. A ! -name "$(printf "*\n*")" has been added to simply skip these files, just in case there are any.

      +

      The only problem is for filenames containing line feeds. A ! -name "$(printf "*\n*")" has been added to simply skip these files, just in case there are any.

      If you don't need variables to be available after the loop (here, if you don't need to print the final play count at the end), you can skip the tmp file and just pipe from find to while.

      For simple commands with no aggregation

      If you don't need a shell script loop body or any form of variable like if we only wanted to play the file, we can dramatically simplify while maintaining POSIX compatibility:

      -
      # Simple and POSIX
      -find mydir -name '*.mp3' -exec play {} \;
      +
      # Simple and POSIX
      +find mydir -name '*.mp3' -exec play {} \;
       

      This does not allow things like let counter++ because let is a shell builtin, not an external command.

      For shell commands with no aggregation

      If we do need a shell script body but no aggregation, you can do the above but invoking sh (this is still POSIX):

      -
      find mydir -name '*.mp3' -exec sh -c '
      +
      find mydir -name '*.mp3' -exec sh -c '
           echo "Playing ${1%.mp3}"
           play "$1"
      -  ' sh {} \;
      +  ' sh {} \;
       

      This would not be possible without sh, because ${1%.mp3} is a shell construct that find can't evaluate by itself. If we had tried to let counter++ in this loop, we would have found that the value never changes.

      -

      Note that using + instead of \;, and using an embedded for file in "$@" loop rather than "$1", will not allow aggregating variables. This is because for large lists, find will invoke the command multiple times, each time with some chunk of the input.

      +

      Note that using + instead of \;, and using an embedded for file in "$@" loop rather than "$1", will not allow aggregating variables. This is because for large lists, find will invoke the command multiple times, each time with some chunk of the input.

      Rationale

      for var in $(find ...) loops rely on word splitting and will evaluate globs, which will wreck havoc with filenames containing whitespace or glob characters.

      find -exec for i in glob and find+while do not rely on word splitting, so they avoid this problem.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2045.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2045.html index 4fecac5..2d68128 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2045.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2045.html @@ -1,12 +1,12 @@

      Iterating over ls output is fragile. Use globs.

      Problematic code

      -
      for f in $(ls *.wav)
      +
      for f in $(ls *.wav)
       do
         echo "$f"
       done
       

      Correct code

      -
      for f in *.wav
      +
      for f in *.wav
       do
         [[ -e "$f" ]] || break  # handle the case of no *.wav files
         echo "$f"
      @@ -16,13 +16,15 @@ 

      Correct code

      Rationale

      When looping over a set of files, it's always better to use globs when possible. Using command expansion causes word splitting and glob expansion, which will cause problems for certain filenames (typically first seen when trying to process a file with spaces in the name).

      The following files can or will break the first loop:

      -
      touch 'filename with spaces.wav'
      -touch 'filename with * globs.wav'
      -touch 'More_Globs[2003].wav'
      -touch 'files_with_fønny_chæracters_in_certain_locales.wav'
      +
      touch 'filename with spaces.wav'
      +touch 'filename with * globs.wav'
      +touch 'More_Globs[2003].wav'
      +touch 'files_with_fønny_chæracters_in_certain_locales.wav'
       

      Related resources

      + diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2046.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2046.html index aee2c7a..74cd80f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2046.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2046.html @@ -1,13 +1,13 @@ -

      Quote this to prevent word splitting.

      +

      Quote this to prevent word splitting.

      Problematic code

      -
      ls -l $(getfilename)
      +
      ls -l $(getfilename)
       

      Correct code

      -
      # getfilename outputs 1 file
      +
      # getfilename outputs 1 file
       ls -l "$(getfilename)"
       
       # getfilename outputs multiple files, linefeed separated
      -getfilename | while IFS='' read -r line
      +getfilename | while IFS='' read -r line
       do
           ls -l "$line"
       done
      @@ -18,14 +18,16 @@ 

      Rationale

      If the command substitution outputs multiple pieces of data, use a loop instead.

      Exceptions

      In rare cases you actually want word splitting, such as in

      -
      # shellcheck disable=SC2046
      +
      # shellcheck disable=SC2046
       gcc $(pkg-config --libs openssl) client.c
       

      This is because pkg-config outputs -lssl -lcrypto, which you want to break up by spaces into -lssl and -lcrypto.

      A bash alternative in these cases is to use read -a for words or mapfile for lines. ksh can also use read -a, or a while read loop for lines. In this case, since pkg-config outputs words, you could use:

      -
      # Read words into an array in bash and ksh
      +
      # Read words from one line into an array in bash and ksh, then expand each as args
       read -ra args < <(pkg-config --libs openssl)
      +gcc "${args[@]}" client.c # expand as args
       
      -# expand args
      -gcc "${args[@]}" client.c
      +# Read lines into an array, then expand each as args
      +readarray -t file_args < <(find /etc/ -type f | grep some-check)
      +your-linter.sh "${file_args[@]}" # expand as args
       
      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.html index 9b36393..61c380b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.html @@ -1,17 +1,17 @@ -

      Use "$@" (with quotes) to prevent whitespace problems.

      -

      Or: Use "${array[@]}" (with quotes) to prevent whitespace problems.

      +

      Use "$@" (with quotes) to prevent whitespace problems.

      +

      Or: Use "${array[@]}" (with quotes) to prevent whitespace problems.

      Problematic code

      -
      cp $* ~/dir
      +
      cp $* ~/dir
       cp ${array[*]} ~/dir
       

      Correct code

      -
      cp "$@" ~/dir
      +
      cp "$@" ~/dir
       cp "${array[@]}" ~/dir
       

      Rationale

      $* and ${array[*]}, unquoted, is subject to word splitting and globbing.

      Let's say you have three arguments or array elements: baz, foo bar and *

      -

      "$@" and "${array[@]}"will expand into exactly that: baz, foo bar and *

      +

      "$@" and "${array[@]}" will expand into exactly that: baz, foo bar and *

      $* and ${array[*]} will expand into multiple other arguments: baz, foo, bar, file.txt and otherfile.jpg

      Since the latter is rarely expected or desired, ShellCheck warns about it.

      Exceptions

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.json index ad5c797..a2bfb67 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2048.json @@ -1,5 +1,5 @@ { - "title": "Use '$@' (with quotes) to prevent whitespace problems.", + "title": "Use "$@" (with quotes) to prevent whitespace problems.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2049.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2049.html index 31265b6..cf72b9f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2049.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2049.html @@ -1,9 +1,9 @@ -

      =~ is for regex, but this looks like a glob. Use = instead.

      +

      =~ is for regex, but this looks like a glob. Use = instead.

      Problematic code

      -
      [[ $file =~ *.txt ]]
      +
      [[ $file =~ *.txt ]]
       

      Correct code

      -
      [[ $file = *.txt ]]
      +
      [[ $file = *.txt ]]
       

      Rationale

      You are using =~ to match against a regex -- specifically a Extended Regular Expression (ERE) -- but the right-hand side looks more like a glob:

      @@ -22,7 +22,7 @@

      Rationale

    Please ensure that the pattern is correct as an ERE, or switch to glob matching if that's what you intended.

    -

    This is similar to [[SC2063]], where grep "*foo*" produces an equivalent warning.

    +

    This is similar to [[SC2063]], where grep "*foo*" produces an equivalent warning.

    Exceptions

    If you are aware of the difference, you can [[ignore]] this message, but this warning is not emitted for the more probable EREs \*.txt, \.txt$, ^s or s+, so it should rarely be necessary.

    Related resources

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2050.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2050.html index ba9c7d1..cd1426d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2050.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2050.html @@ -1,12 +1,12 @@

    This expression is constant. Did you forget the $ on a variable?

    Problematic code

    -
    if [ myvar = "test" ]
    +
    if [ myvar = "test" ]
     then
       echo "Test mode"
     fi
     

    Correct code

    -
    if [ "$myvar" = "test" ]
    +
    if [ "$myvar" = "test" ]
     then
       echo "Test mode"
     fi
    @@ -14,11 +14,10 @@ 

    Correct code

    Rationale

    ShellCheck has found a [ .. ] or [[ .. ]] comparison that only involves literal strings. The intention was probably to check a variable or command output instead.

    This is usually due to missing $ or bad quoting:

    -
    if [[ "myvar" = "test" ]]             # always false because myvar is a literal string
    -if [[ "$myvar" = "test" ]]            # correctly compares a variable
    +
    if [[ "myvar" = "test" ]]             # always false because myvar is a literal string
    +if [[ "$myvar" = "test" ]]            # correctly compares a variable
     
     if [ 'grep -c foo bar' -ge 10 ]       # always false because grep doesn't run
    -if [ "$(grep -c foo bar)" -ge 10 ]    # correctly checks grep output
    -
    +if [ "$(grep -c foo bar)" -ge 10 ] # correctly checks grep output

    Exceptions

    None

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2051.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2051.html index 61f525c..b9a6243 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2051.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2051.html @@ -1,12 +1,12 @@

    Bash doesn't support variables in brace range expansions.

    Problematic code

    -
    for i in {1..$n}
    +
    for i in {1..$n}
     do
       echo "$i"
     done
     

    Correct code

    -
    for ((i=0; i<n; i++))
    +
    for ((i=1; i <= n; i++))
     do
       echo "$i"
     done
    @@ -14,15 +14,15 @@ 

    Correct code

    Rationale

    In Bash, brace expansion happens before variable expansion. This means that brace expansion will not account for variables.

    For integers, use an arithmetic for loop instead. For zero-padded numbers or letters, use of eval may be warranted:

    -
    from="a" to="m"
    +
    from="a" to="m"
     for c in $(eval "echo {$from..$to}"); do echo "$c"; done
     

    or more carefully (if from/to could be user input, or if the brace expansion could have spaces):

    -
    from="a" to="m"
    -while IFS= read -d '' -r c
    +
    from="a" to="m"
    +while IFS= read -d '' -r c
     do
       echo "Read $c"
    -done <  <(eval "printf '%s\0' $(printf "{%q..%q}.jpg" "$from" "$to")")
    +done <  <(eval "printf '%s\0' $(printf "{%q..%q}.jpg" "$from" "$to")")
     

    Exceptions

    None (if you're writing for e.g. zsh, you can use a directive to disable this check)

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2053.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2053.html index 188b4a6..0eb6c3f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2053.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2053.html @@ -1,9 +1,9 @@ -

    Quote the rhs of = in [[ ]] to prevent glob matching.

    +

    Quote the rhs of = in [[ ]] to prevent glob matching.

    Problematic code

    -
    [[ $a = $b ]]
    +
    [[ $a = $b ]]
     

    Correct code

    -
    [[ $a = "$b" ]]
    +
    [[ $a = "$b" ]]
     

    Rationale

    When the right-hand side of =, == or != is unquoted in [[ .. ]], it will be treated like a glob.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2054.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2054.html index 1db2179..ffe5f20 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2054.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2054.html @@ -1,10 +1,10 @@

    Use spaces, not commas, to separate array elements.

    Problematic code

    -
    flags=("-l", "-d", "--sort=size")
    +
    flags=("-l", "-d", "--sort=size")
     ls "${flags[@]}"
     

    Correct code

    -
    flags=("-l" "-d" "--sort=size")
    +
    flags=("-l" "-d" "--sort=size")
     ls "${flags[@]}"
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2055.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2055.html index 4582a86..e411db6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2055.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2055.html @@ -1,12 +1,12 @@ -

    You probably wanted && here, otherwise it's always true.

    +

    You probably wanted && here, otherwise it's always true.

    Problematic code

    -
    if [[ $1 != foo || $1 != bar ]]
    +
    if [[ $1 != foo || $1 != bar ]]
     then
       echo "$1 is not foo or bar"
     fi
     

    Correct code

    -
    if [[ $1 != foo && $1 != bar ]]
    +
    if [[ $1 != foo && $1 != bar ]]
     then
       echo "$1 is not foo or bar"
     fi
    @@ -29,10 +29,10 @@ 

    Rationale

    Exceptions

    Rare.

      -
    • If I want to check that $FOO, $BAR and $COW are all equal:
      if [[ $FOO != $BAR || $FOO != $COW ]]
      +
    • If I want to check that $FOO, $BAR and $COW are all equal:
    • +
    +
    if [[ $FOO != $BAR || $FOO != $COW ]]
     then
    -  echo "$FOO and $BAR and $COW are not all equal"
    +    echo "$FOO and $BAR and $COW are not all equal"
     fi
     
    - -
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2056.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2056.html index f1998aa..c3892ee 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2056.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2056.html @@ -1,12 +1,12 @@ -

You probably wanted && here

+

You probably wanted && here

Problematic code

-
if  (( $1 != 0 || $1 != 3 ))
+
if  (( $1 != 0 || $1 != 3 ))
 then
   echo "$1 is not 0 or 3"
 fi
 

Correct code

-
if  (( $1 != 0 && $1 != 3 ))
+
if  (( $1 != 0 && $1 != 3 ))
 then
   echo "$1 is not 0 or 3"
 fi
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2057.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2057.html
index 65926a5..a593ef9 100644
--- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2057.html
+++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2057.html
@@ -1,9 +1,9 @@
 

Unknown binary operator.

Problematic code

-
[ "$var" -leq 42 ]
+
[ "$var" -leq 42 ]
 

Correct code

-
[ "$var" -le 42 ]
+
[ "$var" -le 42 ]
 

Rationale

You are using an unknown binary operator in a test expression. Choose one that exists.

@@ -29,8 +29,7 @@

Rationale

Arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal -than ARG2. -
+than ARG2.

Exceptions

None. If you've tested and verified that the operator works but the latest version of ShellCheck says it's unknown, please submit a bug report.

Related resources

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2058.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2058.html index efc34ee..49598fe 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2058.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2058.html @@ -1,9 +1,9 @@

Unknown unary operator.

Problematic code

-
[ -E 42 ]
+
[ -E 42 ]
 

Correct code

-
[ -e 42 ]
+
[ -e 42 ]
 

Rationale

You are using an unknown unary operator in a test expression. Perhaps it's a typo?

@@ -28,8 +28,7 @@

Rationale

-x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. - -N FILE True if the file has been modified since it was last read. -
+ -N FILE True if the file has been modified since it was last read.

Exceptions

None. If you've tested and verified that the operator works but the latest version of ShellCheck says it's unknown, please submit a bug report.

Related resources

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.html index 8bc9738..05e67b8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.html @@ -1,26 +1,33 @@ -

Don't use variables in the printf format string. Use printf "..%s.." "$foo".

+

Don't use variables in the printf format string. Use printf "..%s.." "$foo".

Problematic code

-
printf "Hello, $NAME\n"
+
printf "Hello, $NAME\n"
 

Correct code

-
printf "Hello, %s\n" "$NAME"
+
printf "Hello, %s\n" "$NAME"
 

Rationale

printf interprets escape sequences and format specifiers in the format string. If variables are included, any escape sequences or format specifiers in the data will be interpreted too, when you most likely wanted to treat it as data. Example:

-
coverage='96%'
+
coverage='96%'
 printf "Unit test coverage: %s\n" "$coverage"
 printf "Unit test coverage: $coverage\n"
 

The first printf writes Unit test coverage: 96%.

The second writes bash: printf: `\': invalid format character

-

Exceptions

Sometimes you may actually want to interpret data as a format string, like in:

-
octToAscii() { printf "\\$1"; }
+
octToAscii() { printf "\\$1"; }
+octToAscii 130
+
+

In this case, use the %b format specifier that expands escape sequences without interpreting other format specifiers:

+
octToAscii() { printf '%b' "\0$1"; }
 octToAscii 130
 
-

In Bash, Ksh and BusyBox, there's a %b format specifier that expands escape sequences without interpreting other format specifiers: printf '%b' "\\$1". In POSIX, you can instead [[ignore]] this warning.

-

Other times, you might have a pattern in a variable:

-
filepattern="file-%d.jpg"
+

Exceptions

+

Sometimes you might have a pattern in a variable:

+
filepattern="file-%d.jpg"
 printf -v filename "$filepattern" "$number"
 

This has no good rewrite. Please [[ignore]] the warning with a [[directive]].

+

Related resources

+
    +
  • https://pubs.opengroup.org/onlinepubs/9799919799/utilities/printf.html
  • +
diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.json index 875e307..b65a5b4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2059.json @@ -1,5 +1,5 @@ { - "title": "Don't use variables in the printf format string. Use printf '..%s..' '$foo'.", + "title": "Don't use variables in the printf format string. Use printf "..%s.." "$foo".", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2060.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2060.html index e7376cb..1b59cb1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2060.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2060.html @@ -1,12 +1,12 @@ -

Quote parameters to tr to prevent glob expansion.

+

Quote parameters to tr to prevent glob expansion.

Problematic code

-
tr -cd [:digit:]
+
tr -cd [:digit:]
 

Correct code

-
tr -cd '[:digit:]'
+
tr -cd '[:digit:]'
 

Rationale

-

From the shell's point of view, unquoted [:digit:] is a glob equivalent to [dgit:] that matches any single character filename from the group, such as d or t, in the current directory.

+

From the shell's point of view, unquoted [:digit:] is a glob equivalent to [digit:] that matches any single character filename from the group, such as d or t, in the current directory.

If someone starts learning D and creates a directory named d to hold the source code, the glob will be expanded and the script will end up executing tr -cd d instead, which is clearly unintended.

Quoting the argument prevents this, and will pass it correctly as the literal string [:digit:] no matter which files exist in the current directory.

Exceptions

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2061.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2061.html index 0211441..f39dc78 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2061.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2061.html @@ -1,9 +1,9 @@ -

Quote the parameter to -name so the shell won't interpret it.

+

Quote the parameter to -name so the shell won't interpret it.

Problematic code

-
find . -name *.txt
+
find . -name *.txt
 

Correct code

-
find . -name '*.txt'
+
find . -name '*.txt'
 

Rationale

Several find options take patterns to match against, including -ilname, -iname, -ipath, -iregex, -iwholename, -lname, -name, -path, -regex and -wholename.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2062.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2062.html index e21655c..1563524 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2062.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2062.html @@ -1,10 +1,8 @@

Quote the grep pattern so the shell won't interpret it.

Problematic code

-
grep foo* file
-
+
grep foo* file

Correct code

-
grep "foo*" file
-
+
grep "foo*" file

Rationale

-

The regex passed to grep frequently contains characters that collide with globs. The code above is supposed to match "f followed by 1 or more o's", but if the directory contains a file called "foo.txt", an unquoted pattern will cause it to become grep foo.txt file.

+

The regex passed to grep frequently contains characters that collide with globs. The code above is supposed to match "f followed by 1 or more o's", but if the directory contains a file called "foo.txt", an unquoted pattern will cause it to become grep foo.txt file.

To prevent this, always quote the regex passed to grep, especially when it contains one or more glob character.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2063.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2063.html index ad90398..af14147 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2063.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2063.html @@ -1,15 +1,15 @@ -

Grep uses regex, but this looks like a glob.

+

Grep uses regex, but this looks like a glob.

Problematic code

-
grep '*foo*'
+
grep '*foo*'
 

Correct code

-
grep 'foo'   # or more explicitly,  grep '.*foo.*'
+
grep 'foo'   # or more explicitly,  grep '.*foo.*'
 

Rationale

In globs, * matches any number of any character.

In regex, * matches any number of the preceding character.

grep uses regex, not globs, so this means that grep '*foo' is nonsensical because there's no preceding character for *.

-

If the intention was to match "any number of characters followed by foo", use '.*foo'. Also note that since grep matches substrings, this will match "fishfood". Use anchors to prevent this, e.g. foo$.

-

This also means that f* will match "hello", because f* matches 0 (or more) "f"s and there are indeed 0 "f" characters in "hello". Again, use grep 'f' to find strings containing "f", or grep '^f' to find strings starting with "f".

+

If the intention was to match "any number of characters followed by foo", use '.*foo'. Also note that since grep matches substrings, this will match "fishfood". Use anchors to prevent this, e.g. foo$.

+

This also means that f* will match "hello", because f* matches 0 (or more) "f"s and there are indeed 0 "f" characters in "hello". Again, use grep 'f' to find strings containing "f", or grep '^f' to find strings starting with "f".

Exceptions

If you're aware of the differences between globs and regex, you can ignore this message.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2064.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2064.html index db03f18..67fcbf4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2064.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2064.html @@ -1,9 +1,13 @@

Use single quotes, otherwise this expands now rather than when signalled.

Problematic code

-
trap "echo \"Finished on $(date)\"" EXIT
+
trap "echo \"Finished on $(date)\"" EXIT
+trap "rm -fr '$testdir1' '$testdir2'" $TRAP_SIGNALS
+trap "rm $tmp" $TRAP_SIGNALS
+trap "${remove_aar_temp}" SIGKILL SIGTERM SIGQUIT EXIT
+trap "$(shopt -p extglob)" RETURN
 

Correct code

-
trap 'echo "Finished on $(date)"' EXIT
+
trap 'echo "Finished on $(date)"' EXIT
 

Rationale

With double quotes, all parameter and command expansions will expand when the trap is defined rather than when it's executed.

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2065.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2065.html index 37c816a..b1e3021 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2065.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2065.html @@ -1,9 +1,9 @@

This is interpreted as a shell file redirection, not a comparison.

Problematic code

-
[ 1 >2 ] || [ 3>'aaa bb' ] # Simple example of problematic code
+
[ 1 >2 ] || [ 3>'aaa bb' ] # Simple example of problematic code
 

Correct code

-
[ 1 -gt 2 ] || [ 3 \> 'aaa bb' ] # arithmetical, lexicographical
+
[ 1 -gt 2 ] || [ 3 \> 'aaa bb' ] # arithmetical, lexicographical
 

Rationale

A word that looks like a redirection in simple shell commands causes it to be interpreted as a redirection. diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.html index 8377d36..e9a456e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.html @@ -1,23 +1,23 @@ -

Since you double quoted this, it will not word split, and the loop will only run once.

+

Since you double-quoted this, it will not word split, and the loop will only run once.

Problematic code

-
for s in "$(mycommand)"; do echo "$s"; done
+
for s in "$(mycommand)"; do echo "$s"; done
 

Correct code

The correct code depends on your intention. Let's say you're in a directory with the files file.png and My cat.png, and you want to loop over a command that outputs (or variable that contains):

-
hello world
+
hello world
 My *.png
 

Loop over each line without globbing (hello world, My *.png)

-
mycommand | while IFS= read -r s; do echo "$s"; done
+
mycommand | while IFS= read -r s; do echo "$s"; done
 

Loop over each word with globbing (hello, world, My, file.png, My cat.png):

-
# relies on the fact that IFS by default contains space-tab-linefeed
+
# relies on the fact that IFS by default contains space-tab-linefeed
 for s in $(mycommand); do echo "$s"; done
 

Loop over each line with globbing (hello world, My cat.png)

-
# explicitly set IFS to contain only a line feed
-IFS='
-'
+
# explicitly set IFS to contain only a line feed
+IFS='
+'
 for s in $(mycommand); do echo "$s"; done
 

Rationale

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.json index 94c8290..34c985b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2066.json @@ -1,5 +1,5 @@ { - "title": "Since you double quoted this, it will not word split, and the loop will only run once.", + "title": "Since you double-quoted this, it will not word split, and the loop will only run once.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.html index e73cb37..cbe9e77 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.html @@ -1,20 +1,20 @@ -

Missing ';' or + terminating -exec. You can't use |/||/&&, and ';' has to be a separate, quoted argument.

+

Missing ; or + terminating -exec. You can't use |/||/&&, and ; has to be a separate, quoted argument.

Problematic code

-
find . -type f -exec shellcheck {} | wc -l \;
+
find . -type f -exec shellcheck {} | wc -l \;
 find . -exec echo {} ;
 

Correct code

-
find . -type f -exec sh -c 'shellcheck "$1" | wc -l' -- {} \;
+
find . -type f -exec sh -c 'shellcheck "$1" | wc -l' -- {} \;
 find . -exec echo {} \;
 

Rationale

find -exec is still subject to all normal shell rules, so all shell features like |, ||, & and && will apply to the find command itself, and not to the command you are trying to construct with -exec.

-

find . -exec foo {} && bar {} \; means run the command find . -exec foo {}, and if find is successful, run the command bar "{}" ";".

+

find . -exec foo {} && bar {} \; means run the command find . -exec foo {}, and if find is successful, run the command bar "{}" ";".

To instead go through each file and run foo file && bar file on it, invoke a shell that can interpret &&:

-
find . -exec sh 'foo "$1" && bar "$1"' -- {} \;
+
find . -exec sh 'foo "$1" && bar "$1"' -- {} \;
 

You can also use find -a instead of shell &&:

-
find . -exec foo {} \; -a -exec bar {} \;
+
find . -exec foo {} \; -a -exec bar {} \;
 

This will have the same effect (-a is also the default when two commands are specified, and can therefore be omitted).

Exceptions

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.json index 4c15951..6e1d123 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2067.json @@ -1,5 +1,5 @@ { - "title": "Missing ';' or + terminating -exec. You can't use |\/||\/&&, and ';' has to be a separate, quoted argument.", + "title": "Missing ; or + terminating -exec. You can't use |\/||\/&&, and ; has to be a separate, quoted argument.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2068.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2068.html index 23fdeae..fbe8e63 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2068.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2068.html @@ -1,14 +1,14 @@

Double quote array expansions to avoid re-splitting elements.

Problematic code

-
cp $@ ~/dir
+
cp $@ ~/dir
 

Correct code

-
cp "$@" ~/dir
+
cp "$@" ~/dir
 

Rationale

Double quotes around $@ (and similarly, ${array[@]}) prevents globbing and word splitting of individual elements, while still expanding to multiple separate arguments.

Let's say you have four arguments: baz, foo bar, * and /*/*/*/*

-

"$@" will expand into exactly that: baz, foo bar, * and /*/*/*/*

+

"$@" will expand into exactly that: baz, foo bar, * and /*/*/*/*

$@ will expand into multiple other arguments: baz, foo, bar, file.txt, otherfile.jpg, and (eventually) a list of most files on the system

Since the latter is rarely expected or desired, ShellCheck warns about it.

Exceptions

diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.html index fe1d787..387e823 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.html @@ -1,19 +1,18 @@ -

To redirect stdout+stderr, 2>&1 must be last (or use '{ cmd > file; } 2>&1' to clarify).

+

To redirect stdout+stderr, 2>&1 must be last (or use { cmd > file; } 2>&1 to clarify).

Problematic code

-
firefox 2>&1 > /dev/null
+
firefox 2>&1 > /dev/null
 

Correct code

-
firefox > /dev/null 2>&1
+
firefox > /dev/null 2>&1
 

Rationale

When it comes to redirection, order matters.

-

The problematic code means "Point stderr to where stdout is currently pointing (the terminal). Then point stdout to /dev/null".

-

The correct code means "Point stdout to /dev/null. Then point stderr to where stdout is currently pointing (/dev/null)".

+

The problematic code means "Point stderr to where stdout is currently pointing (the terminal). Then point stdout to /dev/null".

+

The correct code means "Point stdout to /dev/null. Then point stderr to where stdout is currently pointing (/dev/null)".

In other words, the problematic code hides stdout and shows stderr. The correct code hides both stderr and stdout, which is usually the intention.

Exceptions

If you actually do want to redirect stdout to a file, and then turn stderr into the new stdout, you can make this more explicit with braces:

-
{ firefox > /dev/null; } 2>&1
-
+
{ firefox > /dev/null; } 2>&1

Also note that this warning does not trigger when output is piped or captured.

Related resources

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.json index f51d31a..0a2e82a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2069.json @@ -1,5 +1,5 @@ { - "title": "To redirect stdout+stderr, 2>&1 must be last (or use '{ cmd > file; } 2>&1' to clarify).", + "title": "To redirect stdout+stderr, 2>&1 must be last (or use { cmd > file; } 2>&1 to clarify).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2070.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2070.html index a97fd3e..a88869b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2070.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2070.html @@ -1,6 +1,6 @@

    -n doesn't work with unquoted arguments. Quote or use [[ ]].

    Problematic code

    -
    if [ -n $var ]
    +
    if [ -n $var ]
     then
       echo "var has a value"
     else
    @@ -9,7 +9,7 @@ 

    Problematic code

    Correct code

    In POSIX:

    -
    if [ -n "$var" ]
    +
    if [ -n "$var" ]
     then
       echo "var has a value"
     else
    @@ -17,7 +17,7 @@ 

    Correct code

    fi

    In bash/ksh:

    -
    if [[ -n $var ]]
    +
    if [[ -n $var ]]
     then
       echo "var has a value"
     else
    @@ -26,7 +26,7 @@ 

    Correct code

    Rationale

    When $var is unquoted, a blank value will cause it to wordsplit and disappear. If $var is empty, these two statements are identical:

    -
    [ -n $var ]
    +
    [ -n $var ]
     [ -n ]
     

    [ string ] is shorthand for testing if a string is empty. This is still true if string happens to be -n. [ -n ] is therefore true, and by extension so is [ -n $var ].

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.html index 9d02276..97978bc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.html @@ -1,13 +1,12 @@ -

    is for string comparisons. Use -gt instead.

    -
    +

    > is for string comparisons. Use -gt instead.

    Problematic code

    -
    if [[ $var > 10 ]]
    +
    if [[ $var > 10 ]]
     then
       echo "Incorrectly triggers when var=5"
     fi
     

    Correct code

    -
    if [[ $var -gt 10 ]]
    +
    if [[ $var -gt 10 ]]
     then
       echo "Correct numerical comparison"
     fi
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.json
    index b51028c..3c23b78 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2071.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "is for string comparisons. Use -gt instead.",
    +  "title": "> is for string comparisons. Use -gt instead.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2072.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2072.html
    index 0648fa2..75fe972 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2072.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2072.html
    @@ -1,9 +1,9 @@
    -

    Decimals are not supported. Either use integers only, or use bc or awk to compare.

    +

    Decimals are not supported. Either use integers only, or use bc or awk to compare.

    Problematic code

    -
    [[ 2 -lt 3.14 ]]
    +
    [[ 2 -lt 3.14 ]]
     

    Correct code

    -
    [[ 200 -lt 314 ]]                   # Use fixed point math
    +
    [[ 200 -lt 314 ]]                   # Use fixed point math
     [[ $(echo "2 < 3.14" | bc) == 1 ]]  # Use bc
     

    Rationale

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2073.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2073.html index 160e727..5d05289 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2073.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2073.html @@ -1,25 +1,25 @@

    Escape \< to prevent it redirecting (or switch to [[ .. ]]).

    Problematic code

    -
    if [ "aardvark" < "zebra" ]
    +
    if [ "aardvark" < "zebra" ]
     then
       echo "Alphabetical!"
     fi
     

    Correct code

    -
    if [ "aardvark" \< "zebra" ]
    +
    if [ "aardvark" \< "zebra" ]
     then
       echo "Alphabetical!"
     fi
     

    or optionally in Bash/Ksh:

    -
    if [[ "aardvark" < "zebra" ]]
    +
    if [[ "aardvark" < "zebra" ]]
     then
       echo "Alphabetical!"
     fi
     

    Rationale

    You are using the operator < or > in a [ test expression.

    -

    In this context, it will be considered a file redirection operator instead, so [ "aardvark" < "zebra" ] is equivalent to [ "aardvark" ] < ./zebra, which is true if there exists a readable file zebra in the current directory.

    +

    In this context, it will be considered a file redirection operator instead, so [ "aardvark" < "zebra" ] is equivalent to [ "aardvark" ] < ./zebra, which is true if there exists a readable file zebra in the current directory.

    If you wanted to compare two strings lexicographically (alphabetically), escape the < or > with a backslash as in the correct example.

    If you want to compare two numbers numerically, use -lt or -ge instead.

    Exceptions

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2074.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2074.html index c82662f..e726c8d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2074.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2074.html @@ -1,9 +1,9 @@

    Can't use =~ in [ ]. Use [[..]] instead.

    Problematic code

    -
    [ "$input" =~ DOC[0-9]*\.txt ] && echo "match"
    +
    [ "$input" =~ DOC[0-9]*\.txt ] && echo "match"
     

    Correct code

    -
    [[ "$input" =~ DOC[0-9]*\.txt ]] && echo "match"
    +
    [[ "$input" =~ DOC[0-9]*\.txt ]] && echo "match"
     

    Rationale

    =~ only works in [[ .. ]] tests. It does not work with test or [ in any shell.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.html index 2dddd85..5ad32ca 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.html @@ -1 +1,16 @@ -

    Escaping \< and \> is required in [..], but invalid in [[..]]

    \ No newline at end of file +

    Escaping \< is required in [..], but invalid in [[..]]

    +

    Problematic code

    +
    [[ aardvark \< zebra ]]
    +
    +

    Correct code

    +
    [[ aardvark < zebra ]]
    +
    +

    Rationale

    +

    Grammatically speaking, [ is considered a normal command name, so < and > are interpreted as redirections. When using the lexicographical string operators < and > in [ .. ], they must be escaped (e.g. \< or "<").

    +

    [[ is considered its own grammatical construct, and therefore it does not require (nor does it allow) escaping < or >.

    +

    Exceptions

    +

    None

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.json index 13e3716..7221d0a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2075.json @@ -1,5 +1,5 @@ { - "title": "Escaping \\< and \\> is required in [..], but invalid in [[..]]", + "title": "Escaping \\< is required in [..], but invalid in [[..]]", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2076.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2076.html index 2b17c46..20ac762 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2076.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2076.html @@ -1,19 +1,19 @@ -

    Don't quote rhs of =~, it'll match literally rather than as a regex.

    +

    Don't quote rhs of =~, it'll match literally rather than as a regex.

    Problematic code

    -
    [[ $foo =~ "^fo+ bar$" ]]
    +
    [[ $foo =~ "^fo+ bar$" ]]
     

    Correct code

    -
    [[ $foo =~ ^fo+\ bar$ ]]
    +
    [[ $foo =~ ^fo+\ bar$ ]]
     

    Rationale

    -

    Quotes on the right hand side of =~ can be used to match literally, so that [[ $1 =~ ^"$2".* ]] works even if $2 contains regex metacharacters. This mirrors the behavior of globs, [[ $1 = "$2"* ]].

    +

    Quotes on the right hand side of =~ can be used to match literally, so that [[ $1 =~ ^"$2".* ]] works even if $2 contains regex metacharacters. This mirrors the behavior of globs, [[ $1 = "$2"* ]].

    This also means that the problematic code tries to match literal carets and plus signs instead of interpreting them as regular expression matchers. To match as a regex, the regex metacharacters it must be unquoted. Literal parts of the expression can be quoted with double or single quotes, or escaped.

    Exceptions

    -

    If you do want to match literally just to do a plain substring search, e.g. [[ $foo =~ "bar" ]], you could ignore this message, but consider using a more canonical glob match instead: [[ $foo == *"bar"* ]].

    +

    If you do want to match literally just to do a plain substring search, e.g. [[ $foo =~ "bar" ]], you could ignore this message, but consider using a more canonical glob match instead: [[ $foo == *"bar"* ]].

    Compatibility

    • In Bash 3.2 and newer with shopt compat31 disabled (the default), quoted patterns are literal whereas unquoted patterns are parsed for regex metacharacters.
    • In Bash 3.2 and newer with shopt compat31 enabled, quoted and unquoted patterns match identically.
    • In Bash 3.1 quoted and unquoted patterns match identically.
    -

    See http://stackoverflow.com/questions/218156/bash-regex-with-quotes

    +

    See http://stackoverflow.com/questions/218156/bash-regex-with-quotes

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2077.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2077.html index f6f0274..ead8cd2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2077.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2077.html @@ -1,13 +1,13 @@

    You need spaces around the comparison operator.

    Problematic code

    -
    [[ 0=1 ]]
    +
    [[ 0=1 ]]
     

    Correct code

    -
    [[ 0 = 1 ]]
    +
    [[ 0 = 1 ]]
     

    Rationale

    -

    [[ 0 = 1 ]] means "check if 0 and 1 are equal".

    -

    [[ str ]] is short form for [[ -n str ]], and means "check if str is non-empty". It doesn't matter if str happens to contain 0=1.

    +

    [[ 0 = 1 ]] means "check if 0 and 1 are equal".

    +

    [[ str ]] is short form for [[ -n str ]], and means "check if str is non-empty". It doesn't matter if str happens to contain 0=1.

    Always use spaces around the comparison operator in [..] and [[..]], otherwise it won't be recognized as an operator.

    Exceptions

    None.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2078.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2078.html index e8ed4ca..a58d9b8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2078.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2078.html @@ -1,12 +1,12 @@

    This expression is constant. Did you forget a $ somewhere?

    Problematic code

    -
    if [ "myvar" ]
    +
    if [ "myvar" ]
     then
       echo "myvar is set"
     fi
     

    Correct code

    -
    if [ "$myvar" ]
    +
    if [ "$myvar" ]
     then
       echo "myvar is set"
     fi
    @@ -19,7 +19,6 @@ 

    Rationale

    if [[ 'grep foo bar' ]] # always true if [[ `grep foo bar` ]] # checks grep output (poorly) -if grep -q foo bar # checks for grep match (preferred) -
    +if grep -q foo bar # checks for grep match (preferred)

    Exceptions

    None

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2079.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2079.html index 36a8f20..80e638a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2079.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2079.html @@ -1,4 +1,4 @@ -

    (( )) doesn't support decimals. Use bc or awk.

    +

    (( )) doesn't support decimals. Use bc or awk.

    Bash arithmetic conditional evaluation can only be performed on integers. More detail: Bash has limited data types which include integer, but everything is effectively untyped.

    -

    Suggested workarounds to this constraint use bc or awk, here are +

    Suggested workarounds to this constraint use bc or awk, here are some examples.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2080.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2080.html index ad2df37..8292642 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2080.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2080.html @@ -1,20 +1,19 @@

    Numbers with leading 0 are considered octal.

    Problematic code

    -
    echo $(( 16 - 08 ))
    +
    echo $(( 16 - 08 ))
     

    Correct code

    -
    echo $(( 16 - 8 ))
    +
    echo $(( 16 - 8 ))
     

    Rationale

    ShellCheck found an integer literal with a leading zero, but containing the digits 8 or 9.

    This is invalid, as the integer will be interpreted as an octal value (e.g. 0777 == 0x1FF == 511).

    To have the value parsed in base 10, either remove the leading zeros as in the example, or specify the radix explicitly:

    -
    echo $((10#08)) 
    -
    +
    echo $((10#08)) 

    Exceptions

    None

    Related resources

    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    -

    The BASH manual "Shell Arithmetic" chapter

    +

    The BASH manual "Shell Arithmetic" chapter

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2081.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2081.html index b2f7bbb..b05c5eb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2081.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2081.html @@ -1,21 +1,21 @@

    [ .. ] can't match globs. Use [[ .. ]] or grep.

    Problematic code

    -
    if [ $var == *[^0-9]* ]
    +
    if [ $var == *[^0-9]* ]
     then
       echo "$var is not numeric"
    -fi
    +fi 
     

    Correct code

    -
    if [[ $var == *[^0-9]* ]]
    +
    if [[ $var == *[^0-9]* ]]
     then
       echo "$var is not numeric"
    -fi
    +fi 
     

    Rationale

    [ .. ] aka test can not match against globs.

    In bash/ksh, you can instead use [[ .. ]] which supports this behavior.

    In sh, you can rewrite to use grep.

    -
    if echo $var | grep -q '^[0-9]*$'; then
    +
    if echo $var | grep -q '^[0-9]*$'; then
       echo "$var is numeric"
     fi
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.html index a098d54..90fd16e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.html @@ -1,26 +1,26 @@ -

    To expand via indirection, use name="foo$n"; echo "${!name}".

    +

    To expand via indirection, use name="foo$n"; echo "${!name}".

    Problematic code

    -
    var_1="hello world"
    +
    var_1="hello world"
     n=1
     echo "${var_$n}"
     

    Correct code

    Bash/ksh:

    -
    # Use arrays instead of dynamic names
    +
    # Use arrays instead of dynamic names
     declare -a var
     var[1]="hello world"
     n=1
     echo "${var[n]}"
     

    or

    -
    # Expand variable names dynamically
    +
    # Expand variable names dynamically
     var_1="hello world"
     n=1
     name="var_$n"
     echo "${!name}"
     

    POSIX sh:

    -
    # Expand dynamically with eval
    +
    # Expand dynamically with eval
     var_1="hello world"
     n=1
     eval "tmp=\$var_$n"
    @@ -29,7 +29,7 @@ 

    Correct code

    Rationale

    You can expand a variable var_1 with ${var_1}, but you can not generate the string var_1 with an embedded expansion, like ${var_$n}.

    Instead, if at all possible, you should use an array. Bash and ksh support both numerical and associative arrays, and an example is shown above.

    -

    If you can't use arrays, you can indirectly reference variables by creating a temporary variable with its name, e.g. myvar="var_$n" and then expanding it indirectly with ${!myvar}. This will give the contents of the variable var_1.

    +

    If you can't use arrays, you can indirectly reference variables by creating a temporary variable with its name, e.g. myvar="var_$n" and then expanding it indirectly with ${!myvar}. This will give the contents of the variable var_1.

    If using POSIX sh, where neither arrays nor ${!var} is available, eval can be used. You must be careful in sanitizing the data used to construct the variable name to avoid arbitrary code execution.

    Exceptions

    None

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.json index 0bd49b4..b63a08f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2082.json @@ -1,5 +1,5 @@ { - "title": "To expand via indirection, use name='foo$n'; echo '${!name}'.", + "title": "To expand via indirection, use name="foo$n"; echo "${!name}".", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.html index a22d4e7..da0c2ed 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.html @@ -1 +1,19 @@ -

    Don't add spaces after the slash in './file'.

    \ No newline at end of file +

    Don't add spaces after the slash in ./file.

    +

    Problematic code

    +
    gcc -o myfile file.c
    +./ myfile
    +
    +

    Correct code

    +
    gcc -o myfile file.c
    +./myfile
    +
    +

    Rationale

    +

    Contrary to popular belief, there is no command or syntax ./ that runs a file.

    +

    ./myfile is simply the shortest path equivalent to myfile that specifies a directory and therefore causes a shell to run it as-is, instead of trying to find its directory using $PATH.

    +

    Therefore, to run a file in the current directory, use ./myfile and not ./ myfile.

    +

    Exceptions

    +

    None

    +

    Related resources

    +
      +
    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
    • +
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.json index e0b7adc..049e897 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2083.json @@ -1,5 +1,5 @@ { - "title": "Don't add spaces after the slash in '.\/file'.", + "title": "Don't add spaces after the slash in .\/file.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.html index c9c5068..9e233c5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.html @@ -1,30 +1,30 @@ -

    Remove '$' or use '_=$((expr))' to avoid executing output.

    +

    Remove $ or use _=$((expr)) to avoid executing output.

    Problematic code

    -
    i=4
    +
    i=4
     $(( i++ ))
     

    Correct code

    Bash, Ksh:

    -
    i=4
    +
    i=4
     (( i++ ))
     

    POSIX (assuming ++ is supported):

    -
    i=4
    +
    i=4
     _=$(( i++ ))
     

    Alternative POSIX version that does not preserve the exit code:

    -
    : $(( i++ ))
    +
    : $(( i++ ))
     

    Rationale

    $((..)) expands to a number. If it's the only word on the line, the shell will try to execute this number as a command name:

    -
    $ i=4
    +
    $ i=4
     $ $(( i++ ))
     4: command not found
     $ echo $i
     5
     

    To avoid trying to execute the number as a command name, use one of the methods mentioned:

    -
    $ i=4
    +
    $ i=4
     $ _=$(( i++ ))
     $ echo $i
     5
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.json
    index dda1431..e4b46de 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.json
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2084.json
    @@ -1,5 +1,5 @@
     {
    -  "title": "Remove '$' or use '_=$((expr))' to avoid executing output.",
    +  "title": "Remove $ or use _=$((expr)) to avoid executing output.",
       "type": "CODE_SMELL",
       "status": "ready",
       "remediation": {
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2086.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2086.html
    index 1887e3a..884d1f7 100644
    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2086.html
    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2086.html
    @@ -1,61 +1,59 @@
    -

    Double quote to prevent globbing and word splitting.

    -

    Problematic code:

    -
    echo $1
    +

    Double quote to prevent globbing and word splitting.

    +

    Problematic code

    +
    echo $1
     for i in $*; do :; done # this one and the next one also apply to expanding arrays.
     for i in $@; do :; done
     
    -

    Correct code:

    -
    echo "$1"
    -for i in "$@"; do :; done # or, 'for i; do'
    +

    Correct code

    +
    echo "$1"
    +for i in "$@"; do :; done # or, 'for i; do'
     
    -

    Rationale

    -

    The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."

    -

    The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.

    +

    Rationale

    +

    The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."

    +

    The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.

    Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.

    Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:

    -
    $HOME/$dir/dist/bin/$file        # Unquoted (bad)
    +
    $HOME/$dir/dist/bin/$file        # Unquoted (bad)
     "$HOME"/"$dir"/dist/bin/"$file"  # Minimal quoting (good)
     "$HOME/$dir/dist/bin/$file"      # Canonical quoting (good)
     
    -

    When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.

    +

    When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.

    Note that $( ) starts a new context, and variables in it have to be quoted independently:

    -
    echo "This $variable is quoted $(but this $variable is not)"
    +
    echo "This $variable is quoted $(but this $variable is not)"
     echo "This $variable is quoted $(and now this "$variable" is too)"
     
    -

    Exceptions

    -

    Sometimes you want to split on spaces, like when building a command line:

    -
    options="-j 5 -B"
    +

    Exceptions +Sometimes you want to split on spaces, like when building a command line:

    +
    options="-j 5 -B"
     [[ $debug == "yes" ]] && options="$options -d"
     make $options file
     

    Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):

    -
    options=(-j 5 -B) # ksh88: set -A options -- -j 5 -B
    +
    options=(-j 5 -B) # ksh88: set -A options -- -j 5 -B
     [[ $debug == "yes" ]] && options=("${options[@]}" -d)
     make "${options[@]}" file
     

    or a function (POSIX):

    -
    make_with_flags() {
    +
    make_with_flags() {
       [ "$debug" = "yes" ] && set -- -d "$@"
       make -j 5 -B "$@"
     }
     make_with_flags file
     
    -

    To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.

    +

    To split on spaces but not perform glob expansion, POSIX has a set -f to disable globbing. You can disable word splitting by setting IFS=''.

    Similarly, you might want an optional argument:

    -
    debug=""
    +
    debug=""
     [[ $1 == "--trace-commands" ]] && debug="-x"
     bash $debug script
     
    -

    Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:

    -
    debug=""
    +

    Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:

    +
    debug=""
     [[ $1 == "--trace-commands" ]] && debug="yes"
     bash ${debug:+"-x"} script
     
    -

    This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.

    -
    +

    This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.

    Here are two common cases where this warning seems unnecessary but may still be beneficial:

    -
    cmd <<< $var         # Requires quoting on Bash 3 (but not 4+)
    -: ${var=default}     # Should be quoted to avoid DoS when var='*/*/*/*/*/*'
    +
    cmd <<< $var         # Requires quoting on Bash 3 (but not 4+)
    +: ${var=default}     # Should be quoted to avoid DoS when var='*/*/*/*/*/*'
     
    -
    -

    As always, this warning can be [[ignored|ignore]] on a case-by-case basis.

    +

    As always, this warning can be ignored on a case-by-case basis.

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.html index 8ed6151..25b86eb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.html @@ -1,32 +1,32 @@ -

    Quote 'EOF' to make here document expansions happen on the server side rather than on the client.

    +

    Quote EOF to make here document expansions happen on the server side rather than on the client.

    Problematic code

    -
    ssh host.example.com << EOF
    +
    ssh host.example.com << EOF
       echo "Logged in on $HOSTNAME"
     EOF
     

    Correct code

    -
    ssh host.example.com << "EOF"
    +
    ssh host.example.com << "EOF"
       echo "Logged in on $HOSTNAME"
     EOF
     

    Rationale

    When the end token of a here document is unquoted, parameter expansion and command substitution will happen on in contents of the here doc.

    -

    This means that before sending the commands to the server, the client replaces $HOSTNAME with localhost, thereby sending echo "Logged in on localhost" to the server. This has the effect of printing the client's hostname instead of the server's.

    +

    This means that before sending the commands to the server, the client replaces $HOSTNAME with localhost, thereby sending echo "Logged in on localhost" to the server. This has the effect of printing the client's hostname instead of the server's.

    Scripts with any kind of variable use are especially problematic because all references will be expanded before the script run. For example,

    -
    ssh host << EOF
    +
    ssh host << EOF
       x="$(uname -a)"
       echo "$x"
     EOF
     

    will never print anything, neither client nor server details, since before evaluation, it will be expanded to:

    -
      x="Linux localhost ... x86_64 GNU/Linux"
    +
      x="Linux localhost ... x86_64 GNU/Linux"
       echo ""
     
    -

    By quoting the here token, local expansion will not take place, so the server sees echo "Logged in on $HOSTNAME" which is expanded and printed with the server's hostname, which is usually the intention.

    +

    By quoting the here token, local expansion will not take place, so the server sees echo "Logged in on $HOSTNAME" which is expanded and printed with the server's hostname, which is usually the intention.

    Exceptions

    If the client should expand some or all variables, this message can and should be ignored.

    To expand a mix of local and remote variables, the here doc end token should be unquoted, and the remote variables should be escaped, e.g.

    -
    ssh host.example.com << EOF
    +
    ssh host.example.com << EOF
       echo "Logged in on \$HOSTNAME from $HOSTNAME"
     EOF
     
    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.json index c7347af..0156986 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2087.json @@ -1,5 +1,5 @@ { - "title": "Quote 'EOF' to make here document expansions happen on the server side rather than on the client.", + "title": "Quote EOF to make here document expansions happen on the server side rather than on the client.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2088.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2088.html index 58b26c1..f23eacc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2088.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2088.html @@ -1,13 +1,13 @@ -

    Tilde does not expand in quotes. Use $HOME.

    +

    Tilde does not expand in quotes. Use $HOME.

    Problematic code

    -
    rm "~/Desktop/$filename"
    +
    rm "~/Desktop/$filename"
     

    Correct code

    -
    rm "$HOME/Desktop/$filename"
    +
    rm "$HOME/Desktop/$filename"
     

    Rationale

    Tilde does not expand to the user's home directory when it's single or double quoted. Use double quotes and $HOME instead.

    -

    Alternatively, the ~/ can be left unquoted, as in rm ~/"Desktop/$filename".

    +

    Alternatively, the ~/ can be left unquoted, as in rm ~/"Desktop/$filename".

    Exceptions

    If you don't want the tilde to be expanded, you can ignore this message.

    Related resources

    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2089.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2089.html index a743892..1bfea35 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2089.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2089.html @@ -1,50 +1,57 @@

    Quotes/backslashes will be treated literally. Use an array.

    Problematic code

    -
    args='-lh "My File.txt"'
    +
    args='-lh "My File.txt"'
     ls $args
     

    Correct code

    In Bash/Ksh with arrays:

    -
    args=(-lh "My File.txt")
    +
    args=(-lh "My File.txt")
     ls "${args[@]}"
     
    -

    or in POSIX overwriting "$@":

    -
    set -- -lh "My File.txt"
    +

    or in POSIX overwriting "$@":

    +
    set -- -lh "My File.txt"
     ls "$@"
     

    or in POSIX via functions:

    -
    myls() { ls "-lh" "My File.txt"; }
    +
    myls() { ls "-lh" "My File.txt"; }
     myls
     

    Rationale

    Bash does not interpret data as code. Consider almost any other languages, such as Python:

    -
    print 1+1   # prints 2
    +
    print 1+1   # prints 2
     a="1+1"
     print a     # prints 1+1, not 2
     

    Here, 1+1 is Python syntax for adding numbers. However, passing a literal string containing this expression does not cause Python to interpret it, see the + and produce the calculated result.

    -

    Similarly, "My File.txt" is Bash syntax for a single word with a space in it. However, passing a literal string containing this expression does not cause Bash to interpret it, see the quotes and produce the tokenized result.

    +

    Similarly, "My File.txt" is Bash syntax for a single word with a space in it. However, passing a literal string containing this expression does not cause Bash to interpret it, see the quotes and produce the tokenized result.

    The solution is to use an array instead, whenever possible.

    If due to sh compatibility you can't use arrays, you can sometimes use functions instead. Instead of trying to create a set of arguments that has to be passed to a command, create a function that calls the function with arguments plus some more:

    -
    ffmpeg_with_args() {
    -  ffmpeg -filter_complex '[#0x2ef] setpts=PTS+1/TB [sub] ; [#0x2d0] [sub] overlay' "$@"
    +
    ffmpeg_with_args() {
    +  ffmpeg -filter_complex '[#0x2ef] setpts=PTS+1/TB [sub] ; [#0x2d0] [sub] overlay' "$@"
     }
     ffmpeg_with_args -i "My File.avi" "Output.avi"
     

    In other cases, you may have to use eval instead, though this is often fragile and insecure. If you get it wrong, it'll appear to work great in all test cases, and may still lead to various forms of security vulnerabilities and breakage:

    -
    quote() { local q=${1//\'/\'\\\'\'}; echo "'$q'"; }
    +
    quote() { local q=${1//\'/\'\\\'\'}; echo "'$q'"; }
     args="-lh $(quote "My File.txt")"
     eval ls "$args" # Do not use unless you understand implications
     

    If you ever accidentally forget to use proper quotes, such as with:

    -
    for f in *.txt; do
    -  args="-lh '$1'" # Example security exploit
    +
    for f in *.txt; do
    +  args="-lh '$1'" # Example security exploit
       eval ls "$args" # Do not copy and use
     done
     
    -

    Then you can use touch "'; rm -rf \$'\x2F'; '.txt" (or someone can trick you into downloading a file with this name, or create a zip file or git repo containing it, or changing their nick and have your chat client create the file for a chat log, or...), and running the script to list your files will run the command rm -rf /.

    +

    Then you can use touch "'; rm -rf \$'\x2F'; '.txt" (or someone can trick you into downloading a file with this name, or create a zip file or git repo containing it, or changing their nick and have your chat client create the file for a chat log, or...), and running the script to list your files will run the command rm -rf /.

    Exceptions

    -

    Few and far between.

    +

    Few and far between, such as, prompt variables. This from man bash "PROMPTING":

    +
    +
      After the string is decoded, it is expanded via parameter expansion, command
    +  substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell
    +  option (see the description of the shopt command under SHELL BUILTIN COMMANDS below).  This can have
    +  unwanted side effects if escaped portions of the string appear within command substitution or contain
    +  characters special to word expansion.
    +

    Additional resources

    • Wooledge BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!
    • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2091.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2091.html index 5b8d905..6200c63 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2091.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2091.html @@ -1,41 +1,40 @@ -

      Remove surrounding $() to avoid executing output (or use eval if intentional).

      +

      Remove surrounding $() to avoid executing output (or use eval if intentional).

      Problematic code

      -
      if $(which epstopdf)
      +
      if $(which epstopdf)
       then
         echo "Found epstopdf"
       fi
       

      or

      -
      make_command() {
      -  printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
      +
      make_command() {
      +  printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
       }
       $(make_command foo.html output/foo.html)
       

      Correct code

      -
      if which epstopdf
      +
      if which epstopdf
       then
         echo "Found epstopdf"
       fi
       

      or

      -
      make_command() {
      -  printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
      +
      make_command() {
      +  printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
       }
       eval "$(make_command foo.html output/foo.html)"
       

      Rationale

      ShellCheck has detected that you have a command that just consists of a command substitution. This often happens when you want to run a command (possibly from a variable name), without realizing that $(..) is for capturing and not for executing.

      For example, if you have this shell function:

      -
      sayhello() { echo "hello world"; }
      -
      +
      sayhello() { echo "hello world"; }

      Then $(sayhello) will:

        -
      1. Run sayhello, capturing "hello world"
      2. +
      3. Run sayhello, capturing "hello world"
      4. Run hello world, resulting in bash: hello: command not found

      Meanwhile, just sayhello will:

        -
      1. Run sayhello, outputting "hello world" to screen
      2. +
      3. Run sayhello, outputting "hello world" to screen

      Note that this is equally true if the command is in a variable, e.g. x=sayhello; $($x).

      If you do have a command that outputs a second command, similar to how ssh-agent outputs export commands to run, then you should do this via eval. This way, quotes, pipes, redirections, semicolons, and other shell constructs will work as expected. Note that this kind of design is best avoided when possible, since correctly escaping all values can be difficult and error prone.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.html index c4c3aa5..be2ce55 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.html @@ -1,19 +1,19 @@ -

      Remove "exec " if script should continue after this command.

      +

      Remove exec if script should continue after this command.

      Problematic code

      -
      echo "Starting compilation"
      +
      echo "Starting compilation"
       exec ./compile
       echo "Starting deployment"
       exec ./deploy
       

      Correct code

      -
      echo "Starting compilation"
      +
      echo "Starting compilation"
       ./compile
       echo "Starting deployment"
       ./deploy
       

      Rationale

      The script contains an exec command followed by other commands in the same block. This is likely an error because the script will not resume after an exec command.

      -

      Instead, "exec" refers to the Unix process model's idea of execution (see execve(2)), in which the current process stops its current program and replaces it with a new one. This is mainly used in wrapper scripts.

      +

      Instead, "exec" refers to the Unix process model's idea of execution (see execve(2)), in which the current process stops its current program and replaces it with a new one. This is mainly used in wrapper scripts.

      To execute another script or program and then continue, simply drop the exec as in the example.

      Exceptions

      If the code after the exec is only there to handle a failure in executing the command you can ignore this warning. For this reason, ShellCheck suppresses the warning if exec is only followed by echo/exit commands.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.json index e86adc7..32c0f2a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2093.json @@ -1,5 +1,5 @@ { - "title": "Remove 'exec ' if script should continue after this command.", + "title": "Remove exec if script should continue after this command.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.html index 88b4f2a..b9e1cbb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.html @@ -1,9 +1,9 @@ -

      SC2094 Make sure not to read and write the same file in the same pipeline.

      +

      Make sure not to read and write the same file in the same pipeline.

      Problematic code

      -
      grep foo file.txt | sed -e 's/foo/bar/g' > file.txt
      +
      grep foo file.txt | sed -e 's/foo/bar/g' > file.txt
       

      Correct code

      -
      grep foo file.txt  | sed -e 's/foo/bar/g' > tmpfile && mv tmpfile file.txt
      +
      grep foo file.txt  | sed -e 's/foo/bar/g' > tmpfile && mv tmpfile file.txt
       

      Rationale

      Each step in a pipeline runs in parallel.

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.json index 8e4837e..c87ac11 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2094.json @@ -1,5 +1,5 @@ { - "title": "SC2094 Make sure not to read and write the same file in the same pipeline.", + "title": "Make sure not to read and write the same file in the same pipeline.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2095.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2095.html index d86e782..fb17171 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2095.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2095.html @@ -1,25 +1,35 @@ -

      Use ssh -n to prevent ssh from swallowing stdin.

      +

      Use ssh -n to prevent ssh from swallowing stdin.

      The same error applies to multiple commands, like ffmpeg -nostdin and mplayer -noconsolecontrols.

      Problematic code

      -
      while read -r host
      +
      while read -r host
       do
         ssh "$host" "uptime"
       done < hosts.txt
       

      Correct code

      -
      while read -r host
      +
      while read -r host
       do
         ssh -n "$host" "uptime"
       done < hosts.txt
       

      or

      -
      while read -r host
      +
      while read -r host
       do
      -  ssh "$host" <<'EOF'
      +  ssh "$host" <<'EOF'
       uptime
       EOF
       done < hosts.txt
       
      +

      or

      +

      By using a pipe and avoiding the use of the stdin file descriptor, this ensures that commands in the loop are not interfered with.

      +
      exec 3< hosts.txt
      +while read -r host
      +do
      +  ssh "$host" "uptime"
      +done <&3
      +# Close the file descriptor
      +exec 3<&-
      +

      Rationale

      Commands that process stdin will compete with the read statement for input. This is especially tricky for commands you wouldn't expect reads from stdin, like ssh .. uptime, ffmpeg and mplayer.

      The most common symptom of this is a while read loop only running once, even though the input contains many lines. This is because the rest of the lines are swallowed by the offending command.

      @@ -30,5 +40,6 @@

      Exceptions

      Related resources

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2096.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2096.html index 34c9a97..917cbe8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2096.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2096.html @@ -1,9 +1,9 @@

      On most OS, shebangs can only specify a single parameter.

      Problematic code

      -
      #!/usr/bin/env bash -x
      +
      #!/usr/bin/env bash -x
       

      Correct code

      -
      #!/usr/bin/env bash
      +
      #!/usr/bin/env bash
       set -x
       

      Rationale

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2097.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2097.html index e9e8624..d125479 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2097.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2097.html @@ -1,25 +1,25 @@

      This assignment is only seen by the forked process.

      Problematic code

      -
      name=World cmd -m "Hello $name"
      +
      name=World cmd -m "Hello $name"
       

      Correct code

      -
      name=World
      +
      name=World
       cmd -m "Hello $name"
       

      If the original goal was to limit the scope of the variable, this can also be done in a subshell:

      -
      (
      +
      (
          name=World
          cmd -m "Hello $name"
      -) # 'name' does not leave this subshell
      +) # 'name' does not leave this subshell
       

      Rationale

      -

      In name=World cmd "$name", name=World is passed in as part of the environment to cmd (i.e., in the envp parameter to execve(2)). This means that cmd and its children will see the parameter, but no other processes will.

      -

      However, "$name" is not expanded by cmd. "$name" is expanded by the shell before cmd is ever executed, and thus it will not use the new value.

      +

      In name=World cmd "$name", name=World is passed in as part of the environment to cmd (i.e., in the envp parameter to execve(2)). This means that cmd and its children will see the parameter, but no other processes will.

      +

      However, "$name" is not expanded by cmd. "$name" is expanded by the shell before cmd is ever executed, and thus it will not use the new value.

      The solution is to set the variable first, then use it as a parameter. If limited scope is desired, a ( subshell ) can be used.

      Exceptions

      In the strange and fabricated scenarios where the script and a program uses a variable name for two different purposes, you can ignore this message. This is hard to conceive, since scripts should use lowercase variable names specifically to avoid collisions with the environment.

      Related resources

      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2099.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2099.html index 3d5464a..756e979 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2099.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2099.html @@ -1,26 +1,23 @@

      Use $((..)) for arithmetics, e.g. i=$((i + 2))

      Problematic code

      -
      i=3
      +
      i=3
       i=i + 2
       

      Correct code

      -
      i=3
      +
      i=3
       i=$((i + 2))
       

      Rationale

      Unlike most languages, variable assignments in shell scripts are space sensitive and (almost) always assign strings.

      To evaluate a mathematical expressions, use $((..)) as in the correct code:

      -
      i=$((i + 2))   # Spaces are fine inside $((...))
      -
      +
      i=$((i + 2))   # Spaces are fine inside $((...))

      In the problematic code, i=i + 2 will give an error +: command not found because the expression is interpreted similar to something like LC_ALL=C wc -c instead of numerical addition:

       Prefix assignment    Command  Argument
        LC_ALL=C             wc       -c
      - i=i                  +        2
      -
      + i=i + 2

      Exceptions

      If you wanted to assign a literal string, quote it:

      -
      game_score="0 - 2"
      -
      +
      game_score="0 - 2"

      Related resources

      • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
      • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2100.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2100.html index 22c1e53..4554e0e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2100.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2100.html @@ -1,10 +1,10 @@

        Use $((..)) for arithmetics, e.g. i=$((i + 2))

        Problematic code

        -
        i=3
        +
        i=3
         i=i+2
         

        Correct code

        -
        i=3
        +
        i=3
         i=$((i + 2))
         

        Rationale

        @@ -12,8 +12,7 @@

        Rationale

        To instead evaluate a mathematical expressions, use $((..)) as in the correct code.

        Exceptions

        If you wanted to assign a literal string, quote it:

        -
        description="friendly-looking"
        -
        +
        description="friendly-looking"

        ShellCheck (as of v0.5) doesn't recognize Bash/Ksh numeric variables created with declare -i where this syntax is valid. Using $((..)) still works, but you can also [[ignore]] this warning.

        Related resources

          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2101.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2101.html index 28e7ff7..c799535 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2101.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2101.html @@ -1,11 +1,11 @@ -

          Named class needs outer [], e.g. [[:digit:]].

          +

          Named class needs outer [], e.g. [[:digit:]].

          Problematic code

          -
          gzip file[:digit:]*.txt
          +
          gzip file[:digit:]*.txt
           

          Correct code

          -
          gzip file[[:digit:]]*.txt
          +
          gzip file[[:digit:]]*.txt
           

          Rationale

          -

          Predefined character groups are supposed to be used inside character ranges. [:digit:] matches one of "digt:" just like [abc] matches one of "abc". [[:digit:]] matches a digit.

          +

          Predefined character groups are supposed to be used inside character ranges. [:digit:] matches one of "digt:" just like [abc] matches one of "abc". [[:digit:]] matches a digit.

          Exceptions

          When passing an argument to tr which parses these by itself without relying on globbing, you should quote it instead, e.g. tr -d '[:digit:]'

          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2102.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2102.html index c68155d..be77fd6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2102.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2102.html @@ -1,25 +1,27 @@

          Ranges can only match single chars (mentioned due to duplicates).

          Problematic code

          -
          echo [100-999].txt
          +
          echo [100-999].txt
           

          Correct code

          -
          echo [1-9][0-9][0-9].txt
          +
          echo [1-9][0-9][0-9].txt
           

          Rationale

          ShellCheck found a glob range expression (such as [a-z]) that contains multiple of the same character.

          Range expressions can only be used to match a single character in a given set, so [ab] and [abba] will both match the same thing: either one a or one b.

          Having multiple of the same character often means you're trying to match more than one character, such as in the problematic example where someone tried to match any number from 100 to 999. Instead, it matches a single digit just like [0-9].txt, and specifies 0, 1 and 9 multiple times.

          In Bash, most uses can be rewritten using extglob and/or brace expansion. For example:

          -
          cat *.[dev,prod,test].conf   # Doesn't work
          +
          cat *.[dev,prod,test].conf   # Doesn't work
           cat *.{dev,prod,test}.conf   # Works in bash
           cat *.@(dev|prod|test).conf  # Works in bash with `shopt -s extglob`
           

          In POSIX sh, you may have to write multiple globs, one after the other:

          -
          cat *.dev.conf *.prod.conf *.test.conf
          +
          cat *.dev.conf *.prod.conf *.test.conf
           

          Exceptions

          There is currently a bug in which a range expression whose contents is a variable gets parsed verbatim, e.g. [$foo]. In this case, either ignore the warning or make the square brackets part of the variable contents instead.

          v0.7.2 and below would unintentionally show this warning for subscripts in arrays in [[ -v array[xx] ]] and other dereferencing operators. In these versions, you can either [[ignore]] the message or quote the word (as in [[ -v 'array[xx]' ]])

          +

          Note that IPv6 URLs trigger this warning, but the correct solution in this case is to quote them:

          +
          curl 'http://[2607:f8b0:4002:c0c::65]/'

          Related resources

          • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
          • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2103.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2103.html index 88fc416..72133a5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2103.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2103.html @@ -1,6 +1,6 @@ -

            Use a ( subshell ) to avoid having to cd back.

            +

            Use a ( subshell ) to avoid having to cd back.

            Problematic code

            -
            for dir in */
            +
            for dir in */
             do
               cd "$dir"
               convert index.png index.jpg
            @@ -8,7 +8,7 @@ 

            Problematic code

            done

            Correct code

            -
            for dir in */
            +
            for dir in */
             do
               (
               cd "$dir" || exit
            @@ -17,7 +17,7 @@ 

            Correct code

            done

            or

            -
            for dir in */
            +
            for dir in */
             do
               cd "$dir" || exit
               convert index.png index.jpg
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2104.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2104.html
            index 41fba8d..d7a85af 100644
            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2104.html
            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2104.html
            @@ -1,6 +1,6 @@
            -

            In functions, use return instead of break.

            +

            In functions, use return instead of break.

            Problematic code

            -
            foo() {
            +
            foo() {
               if [[ -z $1 ]]
               then
                 break
            @@ -9,7 +9,7 @@ 

            Problematic code

            }

            Correct code

            -
            foo() {
            +
            foo() {
               if [[ -z $1 ]]
               then
                 return 1
            @@ -29,13 +29,13 @@ 

            Exceptions

            When the function is called from a loop:

            • ksh keeps going and $? is 0.
            • -
            • bash version 4.4+ prints an error "break: only meaningful in a `for', `while', or `until' loop", the function keeps going, and $? is 0.
            • +
            • bash version 4.4+ prints an error "break: only meaningful in a `for', `while', or `until' loop", the function keeps going, and $? is 0.
            • bash versions before 4.4, will return from the function, break the loop calling the function, or exit a subshell if there's one in between.
            • dash, BusyBox ash: like above.

            When the function is not called from a loop:

              -
            • All bash versions print an error "break: only meaningful in a `for', `while', or `until' loop", the function keeps going, and $? is 0.
            • +
            • All bash versions print an error "break: only meaningful in a `for', `while', or `until' loop", the function keeps going, and $? is 0.
            • ksh, dash and ash silently keep going and $? is 0.

            Due to the many different implementations, many of which are not helpful, it's recommended to use proper flow control. A typical solution is making sure the function returns success/failure, and calling myfunction || break in the loop.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2105.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2105.html index 80120c5..21c7555 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2105.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2105.html @@ -1,6 +1,6 @@

            break is only valid in loops

            Problematic code

            -
            case "$1" in
            +
            case "$1" in
                 -v)
                    verbose=1
                    break
            @@ -10,7 +10,7 @@ 

            Problematic code

            esac

            Correct code

            -
            case "$1" in
            +
            case "$1" in
                 -v)
                    verbose=1
                    ;;
            @@ -19,7 +19,7 @@ 

            Correct code

            esac

            Rationale

            -

            break or continue was found outside a loop. These statements are only valid in loops. In particular, break is not required in case statements as there is no implicit fall-through.

            +

            break or continue was found outside a loop. These statements are valid only in loops. In particular, break is not required in case statements as there is no implicit fall-through.

            To return from a function or sourced script, use return. To exit a script, use exit.

            Exceptions

            -

            It's possible to break/continue in a function without a loop. The call will then affect the loop -- if any -- that the function is invoked from. This is obviously not good coding practice.

            +

            It's possible to break/continue in a function without a loop. The call will then affect the loop – if any – that the function is invoked from, but this is obviously not good coding practice.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.html index de542ae..05b0290 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.html @@ -1,24 +1,24 @@ -

            SC2106: This only exits the subshell caused by the pipeline.

            +

            This only exits the subshell caused by the pipeline.

            Problematic code

            -
            for i in a b c; do
            +
            for i in a b c; do
               echo hi | grep -q bye | break
             done
             

            Correct code

            -
            for i in a b c; do
            +
            for i in a b c; do
               echo hi | grep -q bye || break
             done
             

            Rationale

            The most common cause of this issue is probably using a single | when || was intended. The reason this message appears, though, is that a construction like this, intended to surface a failure inside of a loop:

            -
            for i in a b c; do false | break; done; echo ${PIPESTATUS[@]}
            +
            for i in a b c; do false | break; done; echo ${PIPESTATUS[@]}
             

            may appear to work:

            $ for i in a b c; do false | break; done; echo ${PIPESTATUS[@]}
             1 0
             

            What's actually happening, though, becomes clear if we add some echos; the entire loop completes, and the break has no effect.

            -
            $ for i in a b c; do echo $i; false | break; done; echo ${PIPESTATUS[@]}
            +
            $ for i in a b c; do echo $i; false | break; done; echo ${PIPESTATUS[@]}
             a
             b
             c
            @@ -33,7 +33,8 @@ 

            Rationale

            Related resources

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.json index d85646a..c969650 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2106.json @@ -1,5 +1,5 @@ { - "title": "SC2106: This only exits the subshell caused by the pipeline.", + "title": "This only exits the subshell caused by the pipeline.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2107.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2107.html index 86a6512..81b093d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2107.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2107.html @@ -1,9 +1,9 @@ -

            Instead of [ a && b ], use [ a ] && [ b ].

            +

            Instead of [ a && b ], use [ a ] && [ b ].

            Problematic code

            -
            [ "$1" = "-v" && -z "$2" ]
            +
            [ "$1" = "-v" && -z "$2" ]
             

            Correct code

            -
            [ "$1" = "-v" ] && [ -z "$2" ]
            +
            [ "$1" = "-v" ] && [ -z "$2" ]
             

            Rationale

            && can not be used in a [ .. ] test expression. Instead, make two [ .. ] expressions and put the && between them.

            @@ -11,6 +11,7 @@

            Exceptions

            None.

            Related resources

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2108.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2108.html index 91608b1..4ccd726 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2108.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2108.html @@ -1,9 +1,9 @@ -

            In [[..]], use && instead of -a.

            +

            In [[..]], use && instead of -a.

            Problematic code

            -
            [[ "$1" = "-v" -a -z "$2" ]]
            +
            [[ "$1" = "-v" -a -z "$2" ]]
             

            Correct code

            -
            [[ "$1" = "-v" && -z "$2" ]]
            +
            [[ "$1" = "-v" && -z "$2" ]]
             

            Rationale

            -a for logical AND is not supported in a [[ .. ]] expression. Use && instead.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2109.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2109.html index 4b79e0b..6337a9b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2109.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2109.html @@ -1,9 +1,9 @@ -

            Instead of [ a || b ], use [ a ] || [ b ].

            +

            Instead of [ a || b ], use [ a ] || [ b ].

            Problematic code

            -
            [ "$1" = "-v" || "$1" = "-help" ]
            +
            [ "$1" = "-v" || "$1" = "-help" ]
             

            Correct code

            -
            [ "$1" = "-v" ] || [ "$1" = "-help" ]
            +
            [ "$1" = "-v" ] || [ "$1" = "-help" ]
             

            Rationale

            || cannot be used in a [ .. ] test expression. Instead, make two [ .. ] expressions and put the || between them.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2110.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2110.html index 46162ff..86f21dd 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2110.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2110.html @@ -1,9 +1,9 @@ -

            In [[..]], use || instead of -o.

            +

            In [[..]], use || instead of -o.

            Problematic code

            -
            [[ "$1" = "-v" -o "$1" = "-help" ]]
            +
            [[ "$1" = "-v" -o "$1" = "-help" ]]
             

            Correct code

            -
            [[ "$1" = "-v" || "$1" = "-help" ]]
            +
            [[ "$1" = "-v" || "$1" = "-help" ]]
             

            Rationale

            -o for logical OR is not supported in a [[ .. ]] expression. Use || instead.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.html index b855f60..88fcedc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.html @@ -1 +1,34 @@ -

            ksh does not allow 'function' keyword and '()' at the same time.

            \ No newline at end of file +

            ksh does not allow function keyword and () at the same time.

            +

            Problematic code

            +
            #!/bin/ksh
            +function foo() {
            +  echo "Hello World"
            +}
            +
            +

            Correct code

            +
            # POSIX sh function
            +foo() {
            +  echo "Hello World"
            +}
            +
            +

            or

            +
            # ksh extended function
            +function foo {
            +  echo "Hello World"
            +}
            +
            +

            Rationale

            +

            Ksh allows two ways of defining functions: POSIX sh style foo() { ..; } and Ksh specific function foo { ..; }.

            +

            ShellCheck found a function definition that uses both at the same time, function foo() { ..; } which is not allowed. Use one or the other.

            +

            Note that the two are not identical, for example:

            +
              +
            • Using typeset in a function foo will create a local variable, while in foo() it will create a global variable.
            • +
            • function foo has its own trap context, while foo() shares them with the current process.
            • +
            • function foo will set $0 to foo, while foo() will inherit $0 from the current process.
            • +
            +

            Exceptions

            +

            In Bash, function foo() { ..; } is allowed, and function foo and foo() are identical. This warning does not trigger when the shebang is e.g. #!/bin/bash.

            +

            Related resources

            +
              +
            • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
            • +
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.json index 55a22d7..de0dd5b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2111.json @@ -1,5 +1,5 @@ { - "title": "ksh does not allow 'function' keyword and '()' at the same time.", + "title": "ksh does not allow function keyword and () at the same time.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.html index 354edd7..a9a1057 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.html @@ -1,16 +1,22 @@ -

            'function' keyword is non-standard. Delete it.

            +

            function keyword is non-standard. Delete it.

            Problematic code

            -
            #!/bin/sh
            +
            #!/bin/sh
             function hello() {
               echo "Hello World"
             }
             

            Correct code

            -
            #!/bin/sh
            +
            #!/bin/sh
             hello() {
               echo "Hello World"
             }
             
            +

            The function keyword is a feature of Bash and Ksh, so code that uses it may be intended to be a Bash or Ksh script instead:

            +
            #!/bin/bash
            +function hello() {
            +  echo "Hello World"
            +}
            +

            Rationale

            function is a non-standard keyword that can be used to declare functions in Bash and Ksh.

            In POSIX sh and dash, a function is instead declared without the function keyword as in the correct example.

            @@ -18,5 +24,6 @@

            Exceptions

            None.

            Related resources

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.json index 72d041e..8e0b4fc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2112.json @@ -1,5 +1,5 @@ { - "title": "'function' keyword is non-standard. Delete it.", + "title": "function keyword is non-standard. Delete it.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2113.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2113.html index 1ac9be9..e33ea53 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2113.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2113.html @@ -1,12 +1,12 @@

            function keyword is non-standard. Use foo() instead of function foo.

            Problematic code

            -
            #!/bin/sh
            +
            #!/bin/sh
             function hello {
                 echo "Hello World"
             }
             

            Correct code

            -
            #!/bin/sh
            +
            #!/bin/sh
             hello() {
                 echo "Hello World"
             }
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2114.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2114.html
            index 3ed3f7e..9e1f0b8 100644
            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2114.html
            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2114.html
            @@ -1,16 +1,16 @@
             

            Warning: deletes a system directory.

            Problematic code

            -
            rm -rf /usr /lib/nvidia-current/xorg/xorg
            +
            rm -rf /usr /lib/nvidia-current/xorg/xorg
             

            Correct code

            -
            rm -rf /usr/lib/nvidia-current/xorg/xorg
            +
            rm -rf /usr/lib/nvidia-current/xorg/xorg
             

            Rationale

            The example line of code was an actual bug in the Bumblebee NVIDIA driver.

            Due to an accidental space, it deleted /usr instead of just the particular directory.

            Exceptions

            If you do intend to delete a system directory, such as when working in a chroot or initramfs, you can disable this message with a directive:

            -
            # shellcheck disable=SC2114
            -rm -rf /usr
            +
            # shellcheck disable=SC2114
            +rm -rf /usr 
             

            Previous versions of shellcheck, up to and including 0.4.6, would ignore rm statements containing a -- (an arbitrary convention). This is no longer the case.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.html index ab15796..4a60912 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.html @@ -1,14 +1,14 @@ -

            Use "${var:?}" to ensure this never expands to /* .

            +

            Use "${var:?}" to ensure this never expands to /* .

            Problematic code

            -
            rm -rf "$STEAMROOT/"*
            +
            rm -rf "$STEAMROOT/"*
             

            Correct code

            -
            rm -rf "${STEAMROOT:?}/"*
            +
            rm -rf "${STEAMROOT:?}/"*
             

            Rationale

            If STEAMROOT is empty, this will end up deleting everything in the system's root directory.

            Using :? will cause the command to fail if the variable is null or unset. Similarly, you can use :- to set a default value if applicable.

            In the case command substitution, assign to a variable first and then use :?. This is relevant even if the command seems simple and obviously correct, since forks and execs can fail due to external system limits and conditions, resulting in a blank substitution.

            -

            For more details about :? see the "Parameter Expansion" section of the Bash man page.

            +

            For more details about :? see the "Parameter Expansion" section of the Bash man page.

            Exceptions

            None.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.json index 872cb6a..9906877 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2115.json @@ -1,5 +1,5 @@ { - "title": "Use '${var:?}' to ensure this never expands to \/* .", + "title": "Use "${var:?}" to ensure this never expands to \/* .", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.html index e9639e9..2492554 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.html @@ -1,11 +1,11 @@ -

            SC2116 Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.

            +

            Useless echo? Instead of cmd $(echo foo), just use cmd foo.

            Problematic code

            -
            greeting=$(echo "Hello, $name")
            +
            greeting=$(echo "Hello, $name")
             # or
             tar czf "$(echo "$(date +%F).tar.gz")" *
             

            Correct code

            -
            greeting="Hello, $name"
            +
            greeting="Hello, $name"
             # or
             tar czf "$(date +%F).tar.gz" *
             
            @@ -14,19 +14,19 @@

            Rationale

            You can just replace $(echo myvalue) with myvalue.

            Exceptions

            Sometimes this pattern is used because of side effect of echo or expansions. For example, here $(echo ..) is used to expand a glob.

            -
            glob="*.png"
            -files="$(echo $var)"
            +
            glob="*.png"
            +files="$(echo $var)"
             

            The echo is not useless, but this code is problematic because it concatenates filenames by spaces. This will break filenames containing spaces and other characters later when the list is split again. Better options are:

              -
            • Arrays, if supported by the shell: files=( $glob ); echo "The first file is ${files[0]}"
            • -
            • Positional parameters when possible: set -- $glob; echo "The first file is $1"
            • +
            • Arrays, if supported by the shell: files=( $glob ); echo "The first file is ${files[0]}"
            • +
            • Positional parameters when possible: set -- $glob; echo "The first file is $1"
            • Delaying expansion until it's needed: for file in $glob; do ...

            All three methods will let you avoid issues with special characters in filenames.

            As another example, here $(echo ..) is used to expand escape sequences:

            unexpanded='var\tvalue'
            -expanded="$(echo "$var")"
            +expanded="$(echo "$var")"
             

            In this case, use printf instead. It's well defined with regard to escape sequences.

            Finally, if you really do want to concatenate a series of elements by a character like space, consider doing it explicitly with for or printf (e.g. printf '%s\n' $glob).

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.json index cacb3dd..a6cad87 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2116.json @@ -1,5 +1,5 @@ { - "title": "SC2116 Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.", + "title": "Useless echo? Instead of cmd $(echo foo), just use cmd foo.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2117.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2117.html index df02deb..0b1577a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2117.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2117.html @@ -1,11 +1,11 @@ -

            To run commands as another user, use su -c or sudo.

            +

            To run commands as another user, use su -c or sudo.

            Problematic code

            -
            whoami
            +
            whoami
             su
             whoami
             

            Correct code

            -
            whoami
            +
            whoami
             sudo whoami
             

            Rationale

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.html index a09b714..2bcbf8a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.html @@ -1 +1,17 @@ -

            Ksh does not support |&. Use 2>&1 |.

            \ No newline at end of file +

            Ksh does not support |&. Use 2>&1 |

            +

            Problematic code

            +
            #!/usr/bin/ksh
            +make |& tee ~/log
            +
            +

            Correct code

            +
            #!/usr/bin/ksh
            +make 2>&1 | tee ~/log
            +
            +

            Rationale

            +

            You are using the Bash specific shorthand |&, but your script is running with Ksh. Rewrite it to its full, POSIX-compatible form as shown in the example.

            +

            Exceptions

            +

            None

            +

            Related resources

            +
              +
            • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
            • +
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.json index d7260d7..de37826 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2118.json @@ -1,5 +1,5 @@ { - "title": "Ksh does not support |&. Use 2>&1 |.", + "title": "Ksh does not support |&. Use 2>&1 |", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.html index ae283d2..d1fd3fc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.html @@ -1,2 +1,2 @@ -

            Use foo "$@" if function's $1 should mean script's $1.

            +

            Use foo "$@" if function's $1 should mean script's $1.

            See companion warning [[SC2120]].

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.json index e9b89cc..2863638 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2119.json @@ -1,5 +1,5 @@ { - "title": "Use foo '$@' if function's $1 should mean script's $1.", + "title": "Use foo "$@" if function's $1 should mean script's $1.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2120.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2120.html index 21b2ff8..54c75c2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2120.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2120.html @@ -1,23 +1,23 @@

            foo references arguments, but none are ever passed.

            Problematic code

            -
            sayhello() {
            +
            sayhello() {
               echo "Hello $1"
             }
             sayhello
             
            -

            ./myscript World just prints "Hello " instead of "Hello World".

            +

            ./myscript World just prints "Hello " instead of "Hello World".

            Correct code

            -
            sayhello() {
            +
            sayhello() {
               echo "Hello $1"
             }
             sayhello "$@"
             
            -

            ./myscript World now prints "Hello World".

            +

            ./myscript World now prints "Hello World".

            Rationale

            In a function, $1 and up refers to the function's parameters, not the script's parameters.

            -

            If you want to process your script's parameters in a function, you have to explicitly pass them. You can do this with myfunction "$@".

            -

            Note that "$@" refers to the current context's positional parameters, so if you call a function from a function, you have to pass in "$@" to both of them:

            -
            first() { second "$@"; }
            +

            If you want to process your script's parameters in a function, you have to explicitly pass them. You can do this with myfunction "$@".

            +

            Note that "$@" refers to the current context's positional parameters, so if you call a function from a function, you have to pass in "$@" to both of them:

            +
            first() { second "$@"; }
             second() { echo "The first script parameter is: $1"; }
             first "$@"
             
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.html index 038e7d1..d77bbcb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.html @@ -1,13 +1,13 @@ -

            To assign a variable, use just 'var=value', no 'set ..'.

            +

            To assign a variable, use just var=value, not set ...

            Problematic code

            -
            set var=42
            +
            set var=42
             set var 42
             

            Correct code

            -
            var=42
            +
            var=42
             

            Rationale

            set is not used to set or assign variables in Bourne shells. It's used to set shell options and positional parameters.

            To assign variables, use var=value with no set or other qualifiers.

            Exceptions

            -

            If you actually do want to set positional parameters, simply quoting them or using -- will make shellcheck stop warning, e.g. set -- var1 var2 or set "foo=bar".

            +

            If you actually do want to set positional parameters, simply quoting them or using -- will make shellcheck stop warning, e.g. set -- var1 var2 or set "foo=bar".

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.json index 1f64027..25c56c7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2121.json @@ -1,5 +1,5 @@ { - "title": "To assign a variable, use just 'var=value', no 'set ..'.", + "title": "To assign a variable, use just var=value, not set ...", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.html index facfd06..ae41490 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.html @@ -1,12 +1,11 @@ -

            = is not a valid operator. Use '! a < b' instead.

            -
            +

            >= is not a valid operator. Use ! a < b instead.

            Problematic code

            -
            [[ a &lt;= b ]]
            +
            [[ a &lt;= b ]]
             

            Correct code

            -
            [[ ! a > b ]]
            +
            [[ ! a > b ]]
             

            Rationale

            -

            The operators <= and >= are not supported by Bourne shells. Instead of "less than or equal", rewrite as "not greater than".

            +

            The operators <= and >= are not supported by Bourne shells. Instead of "less than or equal", rewrite as "not greater than".

            Exceptions

            None

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.json index 10d7175..adac804 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2122.json @@ -1,5 +1,5 @@ { - "title": "= is not a valid operator. Use '! a < b' instead.", + "title": ">= is not a valid operator. Use ! a < b instead.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2123.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2123.html index 533daac..8dbcc83 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2123.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2123.html @@ -1,15 +1,15 @@ -

            PATH is the shell search path. Use another name.

            +

            PATH is the shell search path. Use another name.

            Problematic code

            -
            PATH=/my/dir
            +
            PATH=/my/dir
             cat "$PATH/myfile"
             

            Correct code

            Good practice: always use lowercase for unexported variables.

            -
            path=/my/dir
            +
            path=/my/dir
             cat "$path/myfile"
             

            Bad practice: use another uppercase name.

            -
            MYPATH=/my/dir
            +
            MYPATH=/my/dir
             cat "$MYPATH/myfile"
             

            Rationale

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2124.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2124.html index 0fc4703..f114183 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2124.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2124.html @@ -1,27 +1,27 @@ -

            Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

            +

            Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

            Problematic code

            -
            # Want to store multiple elements in var
            +
            # Want to store multiple elements in var
             var=$@
             for i in $var; do ..; done
             

            or

            -
            set -- Hello World
            +
            set -- Hello World
             # Want to concatenate multiple elements into a single string
             msg=$@
             echo "You said $msg"
             

            Correct code

            -
            # Bash: use an array variable
            +
            # Bash: use an array variable
             var=( "$@" )
             for i in "${var[@]}"; do ..; done
             
             # POSIX sh: without array support, one possible workaround
             # is to store elements concatenated with a delimiter (here linefeed/newline)
            -var=$(printf '%s\n' "$@")
            -printf '%s\n' "$var" | while IFS='' read -r line; do ..; done
            +var=$(printf '%s\n' "$@")
            +printf '%s\n' "$var" | while IFS='' read -r line; do ..; done
             

            or

            -
            #!/bin/sh
            +
            #!/bin/sh
             set -- Hello World
             # Explicitly concatenates all the array elements into a single string
             msg=$*
            @@ -30,17 +30,17 @@ 

            Correct code

            Rationale

            Arrays and $@ can contain multiple elements. Simple variables contain only one. When assigning multiple elements to one element, the default behavior depends on the shell (bash concatenates with spaces, zsh concatenates with first char of IFS).

            Since doing this usually indicates a bug, ShellCheck warns and asks you to be explicit about what you want.

            -

            If you want to assign N elements as N elements in Bash or Ksh, use an array, e.g. myArray=( "$@" ).

            +

            If you want to assign N elements as N elements in Bash or Ksh, use an array, e.g. myArray=( "$@" ).

            Dash and POSIX sh do not support arrays. In this case, either concatenate the values with some delimiter that you can split on later (the example uses linefeeds and splits them back up with a while read loop), or keep the values as positional parameters without putting them in an intermediate variable.

            If you want to assign N elements as 1 element by concatenating them, use * instead of @, e.g. myVar=${myArray[*]} (this separates elements with the first character of IFS, usually space).

            The same is true for ${@: -1}, which results in 0 or 1 elements: var=${*: -1} assigns the last element or an empty string.

            Exceptions

            None.

            Adding an element to an array may trigger this warning when you forget the parenthesis.

            -
            filelist="${filelist[@]}" "$filename"
            +
            filelist="${filelist[@]}" "$filename"
             

            What was meant is:

            -
            filelist=("${filelist[@]}" "$filename")
            +
            filelist=("${filelist[@]}" "$filename")
             

            Note: This syntax is compatible with older shells; -in later shells (bash 3.1+ and zsh 4.2+), you can do filelist+=("$filename").

            +in later shells (bash 3.1+ and zsh 4.2+), you can do filelist+=("$filename").

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2125.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2125.html index 8d80252..834986a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2125.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2125.html @@ -1,22 +1,22 @@

            Brace expansions and globs are literal in assignments. Quote it or use an array.

            Problematic code

            -
            foo={1..9}
            +
            foo={1..9}
             echo "$foo"
             
            -
            foo="/some/path/*"
            +
            foo="/some/path/*"
             echo "$foo"
             

            Correct code

            -
            foo=( {1..9} )
            +
            foo=( {1..9} )
             echo "${foo[@]}"
             
            -
            foo=(/some/path/*)
            +
            foo=(/some/path/*)
             echo "${foo[@]}"
             
            -

            Note that either of these will trigger SC3030 ("In POSIX sh, array references are undefined") if you are using sh and not e.g. bash.

            +

            Note that either of these will trigger SC3030 ("In POSIX sh, array references are undefined") if you are using sh and not e.g. bash.

            Rationale

            echo *.png {1..9} expands to all png files and numbers from 1 to 9, but var=*.png or var={1..9} will just assign the literal strings '*.png' and '{1..9}'.

            To make the variable contain all png files or 1 through 9, use an array as demonstrated.

            -

            If you intended to assign these values as literals, quote them (e.g. var="*.png").

            +

            If you intended to assign these values as literals, quote them (e.g. var="*.png").

            Exceptions

            None.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2126.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2126.html index 141bfd9..8f229dd 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2126.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2126.html @@ -1,21 +1,21 @@

            Consider using grep -c instead of grep | wc

            Problematic code

            -
            grep foo | wc -l
            +
            grep foo | wc -l
             

            Correct code

            -
            grep -c foo
            +
            grep -c foo
             

            For multiple files

            Instead of:

            -
            grep foo *.log | wc -l
            +
            grep foo *.log | wc -l
             

            You can pipe all the file contents into grep (passing the files directly to grep causes -c to print each file's count separately, rather than the total):

            -
            cat *.log | grep foo -c
            +
            cat *.log | grep foo -c
             

            Rationale

            This is purely a stylistic issue. grep can count lines without piping to wc.

            Often this number is only used to see whether there are matches (i.e. == 0). In these cases it's clearer and more efficient to use grep -q and check its exit status:

            -
            if grep -q pattern file; then
            +
            if grep -q pattern file; then
               echo "The file contains the pattern"
             fi
             
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.html index 42e3901..217b69c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.html @@ -1,3 +1,32 @@ -

            To use cases with ;&, specify #!/usr/bin/env bash or #!/usr/bin/env ksh.

            -

            To use cases with ;;&, specify #!/usr/bin/env bash.

            -

            To use ${ ..; } command expansion, specify #!/usr/bin/env ksh.

            +

            To use ${ ..; }, specify #!/usr/bin/env ksh.

            +

            (Or "To use cases with ;;&, specify #!/usr/bin/env bash)

            +

            Problematic code

            +
            #!/usr/bin/env bash
            +var=${ mycmd; };
            +
            +

            or

            +
            #!/usr/bin/env ksh
            +case "$1" in
            +  foo) echo "Foo!" ;;&
            +  f*) echo "F-something at least" ;;
            +esac
            +
            +

            Correct code

            +
            #!/usr/bin/env ksh
            +var=${ mycmd; };
            +
            +

            or

            +
            #!/usr/bin/env bash
            +case "$1" in
            +  foo) echo "Foo!" ;;&
            +  f*) echo "F-something at least" ;;
            +esac
            +
            +

            Rationale

            +

            You are using a shell syntax feature not supported by the script's shell. Either rewrite the construct, or switch to a different shell interpreter.

            +

            Exceptions

            +

            None

            +

            Related resources

            +
              +
            • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
            • +
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.json index 8cba683..6dad36d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2127.json @@ -1,5 +1,5 @@ { - "title": "To use ${ ..; } command expansion or cases with ;;& or ;;&, specify #!\/usr\/bin\/env bash or ksh", + "title": "To use ${ ..; }, specify #!\/usr\/bin\/env ksh.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.html index a06da8e..33e2949 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.html @@ -1,27 +1,27 @@ -

            Expanding an array without an index only gives the first element.

            +

            Expanding an array without an index only gives the element in the index 0.

            Problematic code

            -
            myarray=(foo bar)
            +
            myarray=(foo bar)
             for f in $myarray
             do
               cat "$f"
             done
             

            Correct code

            -
            myarray=(foo bar)
            +
            myarray=(foo bar)
             for f in "${myarray[@]}"
             do
               cat "$f"
             done
             

            Rationale

            -

            When referencing arrays, $myarray is equivalent to ${myarray[0]} -- it results in only the first of multiple elements.

            -

            To get all elements as separate parameters, use the index @ (and make sure to double quote). In the example, echo "${myarray[@]}" is equivalent to echo "foo" "bar".

            -

            To get all elements as a single parameter, concatenated by the first character in IFS, use the index *. In the example, echo "${myarray[*]}" is equivalent to echo "foo bar".

            +

            When referencing arrays, $myarray is equivalent to ${myarray[0]} -- which is usually the first of multiple elements. This is also true for associative arrays. Therefore, if 0 (zero) is not a valid key, $myarray expands to an empty string.

            +

            To get all elements as separate parameters, use the index @ (and make sure to double quote). In the example, echo "${myarray[@]}" is equivalent to echo "foo" "bar".

            +

            To get all elements as a single parameter, concatenated by the first character in IFS, use the index *. In the example, echo "${myarray[*]}" is equivalent to echo "foo bar".

            Bugs

            There is a known issue with this check's handling of local variables, causing ShellCheck to flag variables that were previously declared as arrays, even if they are in different scopes.

            The easiest workaround is to simply use different variable names. Alternatively, you can [[ignore]] the check.

            It is also possible to satisfy ShellCheck by declaring the local variable separately from assigning to it, e.g.:

            -
            foo () {
            +
            foo () {
                local -a baz
                baz+=("foo" "bar")
                echo "${baz[@]}"
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.json
            index cca412d..23df999 100644
            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.json
            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2128.json
            @@ -1,5 +1,5 @@
             {
            -  "title": "Expanding an array without an index only gives the first element.",
            +  "title": "Expanding an array without an index only gives the element in the index 0.",
               "type": "CODE_SMELL",
               "status": "ready",
               "remediation": {
            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2129.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2129.html
            index 9216b81..d63549b 100644
            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2129.html
            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2129.html
            @@ -1,11 +1,12 @@
            -

            Consider using { cmd1; cmd2; } >> file instead of individual redirects.

            +

            Consider using { cmd1; cmd2; } >> file instead of individual redirects.

            Problematic code

            -
            echo foo >> file
            +
            echo foo >> file
             date >> file
             cat stuff  >> file
            +
             

            Correct code

            -
            {
            +
            {
               echo foo
               date
               cat stuff
            @@ -15,6 +16,7 @@ 

            Rationale

            Rather than adding >> something after every single line, you can simply group the relevant commands and redirect the group. So the file has to be opened and closed only once and it means a performance gain.

            Exceptions

            This is mainly a stylistic issue, and can freely be ignored.

            +

            Note: shell traps which would ordinarily emit output to stdout or stderr on catching their condition will have output swallowed into the redirect when the trap is triggered from within the grouping.

            Related resources

            • BashFaq: How can I redirect the output of multiple commands at once?
            • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2130.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2130.html index 75ab473..74cbef9 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2130.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2130.html @@ -1,9 +1,10 @@ -

              -eq is for integer comparisons. Use = instead.

              +

              -eq is for integer comparisons. Use = instead.

              +

              Note: This warning seems replace by SC2170 or SC2309. Removed in V0.4.2 - 2016-01-10

              Problematic code

              -
              [[ $foo -eq "Y" ]]
              +
              [[ $foo -eq "Y" ]]
               

              Correct code

              -
              [[ $foo = "Y" ]]
              +
              [[ $foo = "Y" ]]
               

              Rationale

              Shells have two sets of comparison operators: for integers (-eq, -gt, ...) and strings (=, >, ...). ShellCheck has noticed that you're using an integer comparison with string data.

              diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2139.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2139.html index 06745f7..b5f6f30 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2139.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2139.html @@ -1,18 +1,17 @@

              This expands when defined, not when used. Consider escaping.

              Problematic code

              -
              alias whereami="echo $PWD"
              +
              alias whereami="echo $PWD"
               

              Correct code

              -
              alias whereami='echo $PWD'
              +
              alias whereami='echo $PWD'
               

              Rationale

              With double quotes, this particular alias will be defined as echo /home/me, so it will always print the same path. This is rarely intended.

              By using single quotes or escaping any expansions, we define the alias as echo $PWD, which will be expanded when we use the alias. This is the far more common use case.

              Note that even if you expect that the variable will never change, it may still be better to quote it. This prevents a second round of evaluation later:

              -
              default="Can't handle failure, aborting"
              -trap "echo $default; exit 1" err
              -false 
              -
              +
              default="Can't handle failure, aborting"
              +trap "echo $default; exit 1" err
              +false 

              The trap now has a syntax error, because instead of running echo $default, it runs echo Can't handle .. which has an unmatched single quote. Avoid early expansion unless you're equally comfortable putting eval in there.

              Exceptions

              If you don't mind that your alias definition is expanded at define time (and its result expanded again at evaluation time), you can ignore this warning.

              diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.html index 381bf44..8ce6e33 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.html @@ -1,15 +1,15 @@ -

              Word is on the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"?

              +

              Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"?

              Problematic code

              -
              echo "<img src="foo.png" />" > file.html
              +
              echo "<img src="foo.png" />" > file.html
               

              or

              -
              export "var"="42"
              +
              export "var"="42"
               

              Correct code

              -
              echo "<img src=\"foo.png\" />" > file.html
              +
              echo "<img src=\"foo.png\" />" > file.html
               

              or

              -
              export "var=42"
              +
              export "var=42"
               

              Rationale

              This warning triggers when an unquoted literal string is found suspiciously sandwiched between two double quoted strings.

              @@ -19,17 +19,17 @@

              Rationale

            • quotes that are just plain unnecessary (like the export example).

            Without escaping, the inner two quotes of the sandwich (the end quote of the first section and the start quote of the second section) are no-ops. The following two statements are identical, so the quotes that were intended to be part of the html output are instead removed:

            -
            echo "<img src="foo.png" />" > file.html
            +
            echo "<img src="foo.png" />" > file.html
             echo "<img src=foo.png />" > file.html
             

            Similarly, these statements are identical, but work as intended:

            -
            export "var"="42"
            +
            export "var"="42"
             export "var=42"
             

            Exceptions

            If you know that the quotes are ineffectual but you prefer it stylistically, you can ignore this message.

            It's common not to realize that double quotes can span multiple elements, or to stylistically prefer to quote individual variables. For example, these statements are identical, but the first is laboriously and redundantly quoted:

            -
            http://"$user":"$password"@"$host"/"$path"
            +
            http://"$user":"$password"@"$host"/"$path"
             "http://$user:$password@$host/$path"
             

            When ShellCheck detects the first style (i.e. the double quotes include only a single element each), it will suppress the warning.

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.json index 2029381..105cffc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2140.json @@ -1,5 +1,5 @@ { - "title": "Word is on the form 'A'B'C' (B indicated). Did you mean 'ABC' or 'A\\'B\\'C'?", + "title": "Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\\"B\\"C"?", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2141.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2141.html index 377f2f8..823ac04 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2141.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2141.html @@ -1,16 +1,16 @@ -

            Did you mean IFS=$'\t' ?

            +

            Did you mean IFS=$'\t' ?

            Problematic code

            -
            IFS="\t"
            +
            IFS="\t"
             

            Correct code

            -
            IFS=$'\t'
            +
            IFS=$'\t'
             

            or POSIX:

            -
            IFS="$(printf '\t')"
            +
            IFS="$(printf '\t')"
             

            Rationale

            -

            IFS="\t" splits on backslash and the letter "t". IFS=$'\t' splits on tab.

            +

            IFS="\t" splits on backslash and the letter "t". IFS=$'\t' splits on tab.

            Exceptions

            -

            It's extremely rare to want to split on the letter "n" or "t", rather than linefeed or tab.

            +

            It's extremely rare to want to split on the letter "n" or "t", rather than linefeed or tab.

            Related resources

            -

            See https://github.com/koalaman/shellcheck/wiki/SC1012

            +

            See https://github.com/koalaman/shellcheck/wiki/SC1012

            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2142.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2142.html index bd254f0..e5f0cc7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2142.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2142.html @@ -1,15 +1,16 @@

            Aliases can't use positional parameters. Use a function.

            Problematic code

            -
            alias archive='mv "$@" /backup'
            +
            alias archive='mv "$@" /backup'
             

            Correct code

            -
            archive() { mv "$@" /backup; }
            +
            archive() { mv "$@" /backup; }
            +
             

            Rationale

            Aliases just substitute the start of a command with something else. They therefore can't use positional parameters, such as $1. Rewrite your alias as a function.

            Exceptions

            -

            If your alias ends up quoting the value, e.g. alias cut_first="awk '{print \$1}'", you can technically [[ignore]] this error. However, you should consider turning this alias into a more readable function instead: cut_first() { awk '{print $1}' "$@"; }

            -

            You can also [[ignore]] this warning if you intentionally referenced the positional parameters of its relevant context, knowing that it won't refer to the parameters of the alias itself. For example, alias whatisthis='echo "This is $0 -$-" #' will show the shell name with flags, i.e. This is dash -smi or This is bash -himBs, and is correct usage because it does not intend for $0 to reflect anything related to the whatisthis alias or its invocation.

            +

            If your alias ends up quoting the value, e.g. alias cut_first="awk '{print \$1}'", you can technically [[ignore]] this error. However, you should consider turning this alias into a more readable function instead: cut_first() { awk '{print $1}' "$@"; }

            +

            You can also [[ignore]] this warning if you intentionally referenced the positional parameters of its relevant context, knowing that it won't refer to the parameters of the alias itself. For example, alias whatisthis='echo "This is $0 -$-" #' will show the shell name with flags, i.e. This is dash -smi or This is bash -himBs, and is correct usage because it does not intend for $0 to reflect anything related to the whatisthis alias or its invocation.

            Related resources

            • BashFaq: How can I make an alias that takes an argument?
            • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2143.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2143.html index 31aba28..c1e5d7d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2143.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2143.html @@ -1,12 +1,12 @@ -

              Use grep -q instead of comparing output with [ -n .. ].

              +

              Use grep -q instead of comparing output with [ -n .. ].

              Problematic code

              -
              if [ "$(find . | grep 'IMG[0-9]')" ]
              +
              if [ -n "$(find . | grep 'IMG[0-9]')" ]
               then
                 echo "Images found"
               fi
               

              Correct code

              -
              if find . | grep -q 'IMG[0-9]'
              +
              if find . | grep -q 'IMG[0-9]'
               then
                 echo "Images found"
               fi
              diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2144.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2144.html
              index b2da7b5..0d4209a 100644
              --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2144.html
              +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2144.html
              @@ -1,12 +1,12 @@
              -

              -e doesn't work with globs. Use a for loop.

              +

              -e doesn't work with globs. Use a for loop.

              Problematic code

              -
              if [ -e dir/*.mp3 ]
              +
              if [ -e dir/*.mp3 ]
               then
                 echo "There are mp3 files."
               fi
               

              Correct code

              -
              for file in dir/*.mp3
              +
              for file in dir/*.mp3
               do
                 if [ -e "$file" ]
                 then
              @@ -20,7 +20,7 @@ 

              Rationale

              [[ -e file* ]] doesn't work at all.

              Instead, use a for loop to expand the glob and check each result individually.

              If you are looking for the existence of a directory, do:

              -
              for f in /path/to/your/files*; do
              +
              for f in /path/to/your/files*; do
               
                   ## Check if the glob gets expanded to existing files.
                   ## If not, f here will be exactly the pattern above
              @@ -32,7 +32,17 @@ 

              Rationale

              done

              Exceptions

              -

              If filename expansion (globbing) is disabled (set -f), then the [ statement works as expected.

              +

              If you are sure there will only ever be exactly 0 or 1 matches -- and nullglob is not enabled -- then the test happens to work.

              +

              You may still want to consider making this assumption explicit and failing fast if it's ever violated:

              +
              files=( dir/file* )
              +[ "${#files[@]}" -ge 2 ] && exit 1
              +if [ -e "${files[0]}" ]
              +then 
              +  echo "The file exists"
              +else
              +  echo "No such file"
              +fi
              +

              Related resources

              • BashFaq: How can I check whether a directory is empty or not? How do I check for any *.mpg files, or count how many there are?
              • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2145.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2145.html index 037238a..e44eabc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2145.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2145.html @@ -1,53 +1,52 @@ -

                Argument mixes string and array. Use * or separate argument.

                +

                Argument mixes string and array. Use * or separate argument.

                Problematic code

                -
                printf "Error: %s\n" "Bad parameters: $@"
                +
                printf "Error: %s\n" "Bad parameters: $@"
                 

                Correct code

                -
                printf "Error: %s\n" "Bad parameters: $*"
                +
                printf "Error: %s\n" "Bad parameters: $*"
                 

                Problematic code 2

                -
                printf "Error: %s\n" "Bad parameters: ${ARRAY_VAR[@]}"
                +
                printf "Error: %s\n" "Bad parameters: ${ARRAY_VAR[@]}"
                 

                Correct code 2

                -
                printf "Error: %s\n" "Bad parameters: " "${ARRAY_VAR[@]}"
                +
                printf "Error: %s\n" "Bad parameters: " "${ARRAY_VAR[@]}"
                 

                Rationale

                The behavior when concatenating a string and array is rarely intended. The preceding string is prefixed to the first array element, while the succeeding string is appended to the last one. The middle array elements are unaffected.

                -

                E.g., with the parameters foo,bar,baz, "--flag=$@" is equivalent to the three arguments "--flag=foo" "bar" "baz".

                +

                E.g., with the parameters foo,bar,baz, "--flag=$@" is equivalent to the three arguments "--flag=foo" "bar" "baz".

                If the intention is to concatenate all the array elements into one argument, use $*. This concatenates based on IFS.

                If the intention is to provide each array element as a separate argument, put the array expansion in its own argument.

                Exceptions

                The POSIX specified behavior of $@ (and by extension arrays) as part of other strings is often unexpected:

                -

                if the parameter being expanded was embedded within a word, the first field shall be joined with the beginning part of the original word and the last field shall be joined with the end part of the original word. In all other contexts the results of the expansion are unspecified. If there are no positional parameters, the expansion of '@' shall generate zero fields, even when '@' is within double-quotes; however, if the expansion is embedded within a word which contains one or more other parts that expand to a quoted null string, these null string(s) shall still produce an empty field, except that if the other parts are all within the same double-quotes as the '@', it is unspecified whether the result is zero fields or one empty field.

                +
                +

                if the parameter being expanded was embedded within a word, the first field shall be joined with the beginning part of the original word and the last field shall be joined with the end part of the original word. In all other contexts the results of the expansion are unspecified. If there are no positional parameters, the expansion of '@' shall generate zero fields, even when '@' is within double-quotes; however, if the expansion is embedded within a word which contains one or more other parts that expand to a quoted null string, these null string(s) shall still produce an empty field, except that if the other parts are all within the same double-quotes as the '@', it is unspecified whether the result is zero fields or one empty field.

                If you're aware of this and intend to take advantage of it, you can ignore this warning. However, you can also usually also rewrite it into a less surprising form. For example, here's a wrapper script that uses this behavior to substitute certain commands by defining a function for them:

                #!/bin/sh
                -fixed_fgrep() { grep -F "$@"; }
                -fixed_echo() { printf '%s\n' "$*"; }
                -fixed_seq() { echo "seq is not portable" >&2; return 1; }
                +fixed_fgrep() { grep -F "$@"; }
                +fixed_echo() { printf '%s\n' "$*"; }
                +fixed_seq() { echo "seq is not portable" >&2; return 1; }
                 
                -if command -v "fixed_$1" > /dev/null 2>&1
                +if command -v "fixed_$1" > /dev/null 2>&1
                 then
                -  # shellcheck disable=SC2145   # I know how fixed_"$@" behaves and it's correct!
                -  fixed_"$@"
                +  # shellcheck disable=SC2145   # I know how fixed_"$@" behaves and it's correct!
                +  fixed_"$@"
                 else
                -  "$@"
                -fi
                -
                + "$@" +fi

                Here's the same script without relying on this behavior:

                #!/bin/sh
                -fixed_fgrep() { grep -F "$@"; }
                -fixed_echo() { printf '%s\n' "$*"; }
                -fixed_seq() { echo "seq is not portable" >&2; return 1; }
                +fixed_fgrep() { grep -F "$@"; }
                +fixed_echo() { printf '%s\n' "$*"; }
                +fixed_seq() { echo "seq is not portable" >&2; return 1; }
                 
                -cmd="$1"
                +cmd="$1"
                 shift
                 
                -if command -v "fixed_$cmd" > /dev/null 2>&1
                +if command -v "fixed_$cmd" > /dev/null 2>&1
                 then
                  # Perhaps more straight forward with fewer surprises:
                -  "fixed_$cmd" "$@"
                +  "fixed_$cmd" "$@"
                 else
                -  "$cmd" "$@"
                -fi
                -
                + "$cmd" "$@" +fi
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2146.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2146.html index accc43e..0013874 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2146.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2146.html @@ -1,16 +1,17 @@ -

                This action ignores everything before the -o. Use \( \) to group.

                +

                This action ignores everything before the -o. Use \( \) to group.

                Problematic code

                -
                find . -name '*.avi' -o -name '*.mkv' -exec cp {} /media \;
                +
                find . -name '*.avi' -o -name '*.mkv' -exec cp {} /media \;
                 

                Correct code

                -
                find . \( -name '*.avi' -o -name '*.mkv' \) -exec cp {} /media \;
                +
                find . \( -name '*.avi' -o -name '*.mkv' \) -exec cp {} /media \;
                 
                +

                Note that the space between \( and -name in this command is significant.

                Rationale

                In find, two predicates with no operator between them is considered a logical, short-circuiting AND (as if using -a). E.g., -name '*.mkv' -exec .. is the same as -name '*.mkv' -a -exec ...

                -a has higher precedence than -o, so -name '*.avi' -o -name '*.mkv' -a -exec .. is equivalent to -name '*.avi' -o \( -name '*.mkv' -a -exec .. \).

                -

                In other words, the problematic code means "if name matches *.avi, do nothing. Otherwise, if it matches *.mkv, execute a command.".

                -

                In the correct code, we use \( \) to group to get the evaluation order we want. The correct code means "if name matches *.avi or *.mkv, then execute a command", which was what was intended.

                +

                In other words, the problematic code means "if name matches *.avi, do nothing. Otherwise, if it matches *.mkv, execute a command.".

                +

                In the correct code, we use \( \) to group to get the evaluation order we want. The correct code means "if name matches *.avi or *.mkv, then execute a command", which was what was intended.

                Exceptions

                If you're aware of this, you can either ignore this error or group to make it explicit. For example, to decompress all gz files except tar.gz, you can use:

                -
                find . -name '*.tar.gz' -o \( -name '*.gz' -exec gzip -d {} + \)
                +
                find . -name '*.tar.gz' -o \( -name '*.gz' -exec gzip -d {} + \)
                 
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2147.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2147.html index 62b1dda..2031650 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2147.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2147.html @@ -1,9 +1,9 @@

                Literal tilde in PATH works poorly across programs.

                Problematic code

                -
                PATH="$PATH:~/bin"
                +
                PATH="$PATH:~/bin"
                 

                Correct code

                -
                PATH="$PATH:$HOME/bin"
                +
                PATH="$PATH:$HOME/bin"
                 

                Rationale

                Having literal ~ in PATH is a bad idea. Bash handles it, but nothing else does.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2148.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2148.html index 7932b08..8d774c7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2148.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2148.html @@ -1,13 +1,13 @@

                Tips depend on target shell and yours is unknown. Add a shebang.

                Problematic code

                -
                echo "$RANDOM"   # Does this work?
                +
                echo "$RANDOM"   # Does this work?
                 

                Correct code

                -
                #!/bin/sh
                +
                #!/bin/sh
                 echo "$RANDOM"  # Unsupported in sh. Produces warning.
                 

                or

                -
                #!/bin/bash
                +
                #!/bin/bash
                 echo "$RANDOM"  # Supported in bash. No warnings.
                 

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2149.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2149.html index cf26d6a..74e47be 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2149.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2149.html @@ -1,11 +1,12 @@ -

                Remove $/${} for numeric index, or escape it for string.

                +

                Remove $/${} for numeric index, or escape it for string.

                +

                Note: Removed in v0.5.0 - 2018-06-01

                Problematic code

                -
                # Regular array
                +
                # Regular array
                 index=42
                 echo $((array[$index]))
                 

                or

                -
                # Associative array
                +
                # Associative array
                 index=banana
                 echo $((array[$index]))
                 
                @@ -15,15 +16,15 @@

                Correct code

                echo $((array[index]))

                or

                -
                # Associative array
                +
                # Associative array
                 index=banana
                 echo $((array[\$index]))
                 

                Rationale

                For a numerically indexed array, the $ is mostly pointless and can be removed like in [[SC2004]].

                For associative arrays, the $ should be escaped to avoid accidental dereferencing:

                -
                declare -A array
                -index='$1'
                +
                declare -A array
                +index='$1'
                 array[$index]=42
                 echo "$(( array[$index] ))"    # bash: array: bad array subscript
                 echo "$(( array[\$index] ))"   # 42
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2150.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2150.html
                index 13db5ee..ededa3a 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2150.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2150.html
                @@ -1,13 +1,13 @@
                -

                -exec does not automatically invoke a shell. Use -exec sh -c .. for that.

                +

                -exec does not automatically invoke a shell. Use -exec sh -c .. for that.

                Problematic code

                -
                find . -type f -exec 'cat {} | wc -l' \;
                +
                find . -type f -exec 'cat {} | wc -l' \;
                 

                Correct code

                -
                find . -type f -exec sh -c 'cat {} | wc -l' \;         # Insecure
                -find . -type f -exec sh -c 'cat "$1" | wc -l' _ {} \;  # Secure
                +
                find . -type f -exec sh -c 'cat {} | wc -l' \;         # Insecure
                +find . -type f -exec sh -c 'cat "$1" | wc -l' _ {} \;  # Secure
                 

                Sometimes the command can also be rewritten to not require find to invoke a shell:

                -
                find . -type f -exec wc -l {} \; | cut -d ' ' -f 1
                +
                find . -type f -exec wc -l {} \; | cut -d ' ' -f 1
                 

                Rationale

                find -exec and -execdir uses execve(2) style semantics, meaning it expects an executable and zero or more arguments that should be passed to it.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2151.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2151.html index 208fe3e..ac70eb0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2151.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2151.html @@ -1,11 +1,11 @@

                Only one integer 0-255 can be returned. Use stdout for other data.

                Problematic code

                -
                myfunc() {
                +
                myfunc() {
                   return foo bar
                 }
                 

                Correct code

                -
                myfunc() {
                +
                myfunc() {
                   echo foo
                   echo bar
                   return 0
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2152.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2152.html
                index ab799ba..f78dbf3 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2152.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2152.html
                @@ -1,11 +1,11 @@
                 

                Can only return 0-255. Other data should be written to stdout.

                Problematic code

                -
                myfunc() {
                +
                myfunc() {
                   return "Hello $USER"
                 }
                 

                Correct code

                -
                myfunc() {
                +
                myfunc() {
                   echo "Hello $USER"
                   return 0
                 }
                @@ -14,14 +14,14 @@ 

                Rationale

                In many languages, return is used to return from the function with a final result.

                In sh/bash, return can only be used to signal success or failure (0 = success, 1-255 = failure), more akin to throw/raise in other languages.

                Results should instead be written to stdout and captured:

                -
                message=$(myfunc)
                +
                message=$(myfunc)
                 echo "The function wrote: $message"
                 
                -

                In functions that return small integers, such as getting the cpu temperature, the value should still be written to stdout. return should be reserved for error conditions, such as "can't determine CPU temperature". Error or failure messages should be written to stderr.

                +

                In functions that return small integers, such as getting the cpu temperature, the value should still be written to stdout. return should be reserved for error conditions, such as "can't determine CPU temperature". Error or failure messages should be written to stderr.

                Note in particular that return -1 is equivalent to return 255, but that return 1 is the more canonical way of expressing the first possible error code.

                Exceptions

                None

                Related resources

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2153.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2153.html index ae7186e..a0a8eba 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2153.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2153.html @@ -1,10 +1,10 @@

                Possible Misspelling: MYVARIABLE may not be assigned. Did you mean MY_VARIABLE?

                Problematic code

                -
                MY_VARIABLE="hello world"
                +
                MY_VARIABLE="hello world"
                 echo "$MYVARIABLE"
                 

                Correct code

                -
                MY_VARIABLE="hello world"
                +
                MY_VARIABLE="hello world"
                 echo "$MY_VARIABLE"
                 

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2154.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2154.html index 63b632c..9f2b5f0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2154.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2154.html @@ -1,38 +1,38 @@

                var is referenced but not assigned.

                Problematic code

                -
                var=name
                +
                var=name
                 n=42
                 echo "$var_$n.jpg"   # overextended
                 

                or

                -
                target="world"
                +
                target="world"
                 echo "hello $tagret"  # misspelled
                 

                or

                -
                echo "Result: ${mycmd -a myfile}"  # trying to execute commands
                +
                echo "Result: ${mycmd -a myfile}"  # trying to execute commands
                 

                Correct code

                -
                var=name
                +
                var=name
                 n=42
                 echo "${var}_${n}.jpg"
                 

                or

                -
                target="world"
                +
                target="world"
                 echo "hello $target"
                 

                or

                -
                echo "Result: $(mycmd -a myfile)"
                +
                echo "Result: $(mycmd -a myfile)"
                 

                Rationale

                ShellCheck has noticed that you reference a variable that is not assigned. Double check that the variable is indeed assigned, and that the name is not misspelled.

                Note: This message only triggers for variables with lowercase characters in their name (foo and kFOO but not FOO) due to the standard convention of using lowercase variable names for unexported, local variables.

                Exceptions

                -

                ShellCheck intentionally does not attempt to figure out runtime or dynamic assignments like with source "$(date +%F).sh" or eval var=value. See [[SC2034]] for an extended discussion of why this is the case.

                -

                If you know for a fact that the variable is set, you can use ${var:?} to fail if the variable is unset (or empty), initialize it to a default value if uninitialized with : "${var:=}", or explicitly initialize/declare it with var="" or declare var. You can also disable the message with a [[directive]].

                -
                +

                ShellCheck intentionally does not attempt to figure out runtime or dynamic assignments like with source "$(date +%F).sh" or eval var=value. See [[SC2034]] for an extended discussion of why this is the case.

                +

                If you know for a fact that the variable is set, you can use ${var:?} to fail if the variable is unset (or empty), initialize it to a default value if uninitialized with : "${var:=}", or explicitly initialize/declare it with var="" or declare var. You can also disable the message with a [[directive]].

                +

                Read more:

                POSIX - Parameter expansion:

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2155.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2155.html index 0f748b8..cfd23fb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2155.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2155.html @@ -1,9 +1,9 @@

                Declare and assign separately to avoid masking return values.

                Problematic code in the case of export

                -
                export foo="$(mycmd)"
                +
                export foo="$(mycmd)"
                 

                Correct code:

                -
                foo="$(mycmd)"
                +
                foo="$(mycmd)"
                 export foo
                 

                Rationale:

                @@ -12,40 +12,39 @@

                Rationale:

                Note that ShellCheck does not warn about masking of local read-only variables, such as local -r foo=$(cmd), even though this also masks the return value. This is because the alternative local foo; foo=$(cmd); local -r foo is repetitive and cumbersome. To see warnings for this and many other additional cases of suppressed exit codes, enable check-extra-masked-returns.

                Exceptions:

                If you intend to ignore the return value of an assignment, you can either [[ignore]] this warning or use

                -
                foo=$(mycmd) || true
                +
                foo=$(mycmd) || true
                 export foo
                 

                Shellcheck does not warn about export foo=bar because bar is a literal and not a command substitution with an independent return value.

                Problematic code in the case of local

                -
                local foo="$(mycmd)"
                +
                local foo="$(mycmd)"
                 

                Correct code:

                -
                local foo
                +
                local foo
                 foo=$(mycmd)
                 

                Rationale

                The exit status of the command is overridden by the exit status of the creation of the local variable. For example:

                -
                $ f() { local foo=$(false) || echo foo; }; f
                -$ f() { local foo; foo=$(false) || echo foo; }; f
                -foo
                +
                $ f() { local foo=$(false) && echo "error was hidden"; }; f
                +error was hidden
                +$ f() { local foo; foo=$(false) && echo "error was hidden"; }; f
                 

                Problematic code in the case of readonly

                -
                readonly foo="$(mycmd)"
                +
                readonly foo="$(mycmd)"
                 

                Correct code:

                -
                foo="$(mycmd)"
                +
                foo="$(mycmd)"
                 readonly foo
                 

                Word splitting and quoting issue with dash, maybe others

                A serious quoting problem with dash is another reason to declare and assign separately. Dash is the default, /bin/sh shell on Ubuntu. More specifically, dash version 0.5.8-2.10 and others cannot run these two examples:

                -
                f(){ local e=$1; }
                +
                f(){ local e=$1; }
                 f "1 2"
                 
                -export g=$(printf '%s' "foo 2")
                +export g=$(printf '%s' "foo 2")
                 

                While this runs fine in other shells, dash doesn't treat any of these as assignments and fails both like this:

                local: 2: bad variable name
                 export: 2: bad variable name
                 

                The direct workaround to this bug is to quote the right-hand-side of the assignment. Separating declaraction and assignment also makes this runs fine in any shell.

                -

                (A rule to catch this problem is in the works at #1556).

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2156.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2156.html index 0fd8479..059f184 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2156.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2156.html @@ -1,12 +1,12 @@

                Injecting filenames is fragile and insecure. Use parameters.

                Problematic code

                -
                find . -name '*.mp3' -exec sh -c 'i="{}"; sox "$i" "${i%.mp3}.wav"' \;
                +
                find . -name '*.mp3' -exec sh -c 'i="{}"; sox "$i" "${i%.mp3}.wav"' \;
                 

                Correct code

                -
                find . -name '*.mp3' -exec sh -c 'i="$1"; sox "$i" "${i%.mp3}.wav"' shell {} \;
                +
                find . -name '*.mp3' -exec sh -c 'i="$1"; sox "$i" "${i%.mp3}.wav"' shell {} \;
                 

                Rationale

                In the problematic example, the filename is passed by injecting it into a shell string. Any shell metacharacters in the filename will be interpreted as part of the script, and not as part of the filename. This can break the script and allow arbitrary code execution exploits.

                -

                In the correct example, the filename is passed as a parameter. It will be safely treated as literal text. Note that when using the shell command with -c, the first parameter to the shell command (in the example "shell") becomes $0 in the shell command's environment, where it is used e.g. in shell error messages (you can set it to an arbitrary value, but it makes sense to set it to the shell's name). You should not use the first parameter to the shell command as a data processing parameter because you cannot, for example, access $0 via $* in the shell command (because $* starts with $1), and as previously mentioned, $0 is used in the shell command's error messages, which would be confusing.

                +

                In the correct example, the filename is passed as a parameter. It will be safely treated as literal text. Note that when using the shell command with -c, the first parameter to the shell command (in the example "shell") becomes $0 in the shell command's environment, where it is used e.g. in shell error messages (you can set it to an arbitrary value, but it makes sense to set it to the shell's name). You should not use the first parameter to the shell command as a data processing parameter because you cannot, for example, access $0 via $* in the shell command (because $* starts with $1), and as previously mentioned, $0 is used in the shell command's error messages, which would be confusing.

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2157.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2157.html index 0ba9145..d4c1bcc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2157.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2157.html @@ -1,13 +1,13 @@ -

                Argument to implicit -n is always true due to literal strings. +

                Argument to implicit -n is always true due to literal strings. (Or: Argument to -z is always false due to literal strings. )

                Problematic code

                -
                if [ "$foo " ]
                +
                if [ "$foo " ]
                 then
                   echo "this is always true because of the trailing space"
                 fi
                 

                Correct code

                -
                if [ "$foo" ]
                +
                if [ "$foo" ]
                 then
                   echo "correctly checks value"
                 fi
                @@ -17,9 +17,9 @@ 

                Rationale

                Equivalently, since [ -z str ] checks that the string is empty, any literal character in the string will cause the test to always be false.

                Double check the string: you may have added trailing characters, or bad quotes or syntax. Some examples include:

                  -
                • [ "$foo " ] like in the example, where the space becomes part of the string
                • -
                • [ "{$foo}" ] instead of [ "${foo}" ], where the { becomes part of the string
                • -
                • [ "$foo -gt 0" ] instead of [ "$foo" -gt "0" ], where the -gt becomes part of the string
                • +
                • [ "$foo " ] like in the example, where the space becomes part of the string
                • +
                • [ "{$foo}" ] instead of [ "${foo}" ], where the { becomes part of the string
                • +
                • [ "$foo -gt 0" ] instead of [ "$foo" -gt "0" ], where the -gt becomes part of the string

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2158.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2158.html index 3a6bace..24f3a36 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2158.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2158.html @@ -1,12 +1,12 @@ -

                [ false ] is true. Remove the brackets

                +

                [ false ] is true. Remove the brackets

                Problematic code

                -
                if [ false ]
                +
                if [ false ]
                 then
                   echo "triggers anyways"
                 fi
                 

                Correct code

                -
                if false
                +
                if false
                 then
                   echo "never triggers"
                 fi
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.html
                index 8641a21..c222ae4 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.html
                @@ -1,12 +1,12 @@
                -

                [ 0 ] is true. Use 'false' instead

                +

                [ 0 ] is true. Use false instead.

                Problematic code

                -
                if [ 0 ]
                +
                if [ 0 ]
                 then
                   echo "always triggers"
                 fi
                 

                Correct code

                -
                if false
                +
                if false
                 then
                   echo "never triggers"
                 fi
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.json
                index 103bc1b..545d8a0 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.json
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2159.json
                @@ -1,5 +1,5 @@
                 {
                -  "title": "[ 0 ] is true. Use 'false' instead",
                +  "title": "[ 0 ] is true. Use false instead.",
                   "type": "CODE_SMELL",
                   "status": "ready",
                   "remediation": {
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.html
                index d929565..0539fc9 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.html
                @@ -1,44 +1,45 @@
                -

                Instead of '[ true ]', just use 'true'.

                +

                Instead of [ true ], just use true.

                Problematic code

                -
                if [ true ]
                +
                if [ true ]
                 then
                   echo "always triggers"
                 fi
                 

                Correct code

                -
                if true
                +
                if true
                 then
                   echo "always triggers"
                 fi
                 

                Rationale

                This is a stylistic suggestion to use true instead of [ true ].

                -

                [ true ] seems to suggest that the value "true" is somehow relevant to the statement. This is not the case, it doesn't matter. You can replace it with [ false ] or [ wombat ], and it will still always be true:

                +

                [ true ] seems to suggest that the value "true" is somehow relevant to the statement. This is not the case, it doesn't matter. You can replace it with [ false ] or [ wombat ], and it will still always be true:

                - - - - + + + + + - - - + + + - - - + + + - - - + + +
                StringIn bracketsOutside brackets
                StringIn bracketsOutside brackets
                truetruetruetruetruetrue
                falsetruefalsefalsetruefalse
                wombattrueunknown commandwombattrueunknown command
                -

                It's therefore better to use it without brackets, so that the "true" actually matters.

                +

                It's therefore better to use it without brackets, so that the "true" actually matters.

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.json index a1e2c17..725923f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2160.json @@ -1,5 +1,5 @@ { - "title": "Instead of '[ true ]', just use 'true'.", + "title": "Instead of [ true ], just use true.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.html index fd0bd70..8313152 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.html @@ -1,19 +1,19 @@ -

                Instead of '[ 1 ]', use 'true'.

                +

                Instead of [ 1 ], use true.

                Problematic code

                -
                while [ 1 ]
                +
                while [ 1 ]
                 do
                   echo "infinite loop"
                 done
                 

                Correct code

                -
                while true
                +
                while true
                 do
                   echo "infinite loop"
                 done
                 

                Rationale

                This is a stylistic suggestion to use true instead of [ 1 ].

                -

                [ 1 ] seems to suggest that the value "1" is somehow relevant to the statement. This is not the case: it doesn't matter. You can replace it with [ 0 ] or [ wombat ], and it will still always be true.

                +

                [ 1 ] seems to suggest that the value "1" is somehow relevant to the statement. This is not the case: it doesn't matter. You can replace it with [ 0 ] or [ wombat ], and it will still always be true.

                If you instead use true, the value is actually considered and can be inverted by replacing with false.

                On bash, you can also use (( 1 )), which evaluates to true much like in C. (( 0 )) is similarly false.

                Exceptions

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.json index 752ce35..18de08e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2161.json @@ -1,5 +1,5 @@ { - "title": "Instead of '[ 1 ]', use 'true'.", + "title": "Instead of [ 1 ], use true.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.html index 68cba73..8388bb4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.html @@ -1,18 +1,28 @@ -

                read without -r will mangle backslashes

                +

                read without -r will mangle backslashes.

                Problematic code

                -
                echo "Enter name:"
                +
                echo "Enter name:"
                 read name
                 

                Correct code

                -
                echo "Enter name:"
                +
                echo "Enter name:"
                 read -r name
                 
                +

                or

                +
                echo "Enter name:"
                +IFS= read -r name
                +

                Rationale

                By default, read will interpret backslashes before spaces and line feeds (i.e. you can use backslashes in your string as an escape character). This is rarely expected or desired.

                Normally you just want to read data including backslashes which are part of the input string and have no special escape meaning, which is what read -r does. You should always use -r unless you have a good reason not to:

                -

                -r

                +
                +

                -r

                If this option is given, backslash does not act as an escape character.

                -

                Note that read -r will still strip leading and trailing spaces. IFS="" read -r prevents this.

                +

                Trimming whitespace

                +

                Even with read -r, leading and trailing whitespace will be stripped from the input. Although this may sometimes be desirable or harmless it is often surprising and difficult to catch. Clearing the IFS disables this behavior, so IFS= read -r is generally safest.

                +

                See Also

                +

                Exceptions

                If you want backslashes to affect field splitting and line terminators instead of being read, you can disable this message with a [[directive]].

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.json index c68497b..2cd83ed 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2162.json @@ -1,5 +1,5 @@ { - "title": "read without -r will mangle backslashes", + "title": "read without -r will mangle backslashes.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.html index bde4d91..39258c4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.html @@ -1,21 +1,21 @@ -

                This does not export 'FOO'. Remove $/${} for that, or use ${var?} to quiet.

                +

                This does not export FOO. Remove $/${} for that, or use ${var?} to quiet.

                Problematic code

                -
                MYVAR=foo
                +
                MYVAR=foo
                 export $MYVAR
                 

                Correct code

                -
                MYVAR=foo
                +
                MYVAR=foo
                 export MYVAR
                 

                Rationale

                export takes a variable name, but shellcheck has noticed that you give it an expanded variable instead. The problematic code does not export MYVAR but a variable called foo if any.

                Exceptions

                If this is intentional and you do want to export foo instead of MYVAR, you can either use a directive:

                -
                # shellcheck disable=SC2163
                +
                # shellcheck disable=SC2163
                 export "$MYVAR"
                 

                Or after (but not including) version 0.4.7, take advantage of the fact that ShellCheck only warns when no parameter expansion modifiers are applied:

                -
                export "${MYVAR}"    # ShellCheck warns
                +
                export "${MYVAR}"    # ShellCheck warns
                 export "${MYVAR?}"   # No warning
                 

                ${MYVAR?} fails when MYVAR is unset, which is fine since export would have failed too. The main side effect is an improved runtime error message in that case.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.json index 19721d0..a2cf111 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2163.json @@ -1,5 +1,5 @@ { - "title": "This does not export 'FOO'. Remove $\/${} for that, or use ${var?} to quiet.", + "title": "This does not export FOO. Remove $\/${} for that, or use ${var?} to quiet.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2164.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2164.html index 6a7dff4..c15710b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2164.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2164.html @@ -1,18 +1,18 @@ -

                Use cd ... || exit in case cd fails.

                +

                Use cd ... || exit in case cd fails.

                Problematic code

                -
                cd generated_files
                +
                cd generated_files
                 rm -r *.c
                 
                -
                func(){
                +
                func(){
                   cd foo
                   do_something
                 }
                 

                Correct code

                -
                cd generated_files || exit
                +
                cd generated_files || exit
                 rm -r *.c
                 
                -
                # For functions, you may want to use return:
                +
                # For functions, you may want to use return:
                 func(){
                   cd foo || return
                   do_something
                @@ -23,10 +23,10 @@ 

                Rationale

                If/when it does, the script will keep going and do all its operations in the wrong directory. This can be messy, especially if the operations involve creating or deleting a lot of files.

                To avoid this, make sure you handle the cases when cd fails. Ways to do this include

                  -
                • cd foo || exit as suggested to abort immediately, reusing exit code from failed cd command
                • -
                • cd foo || { echo "Failure"; exit 1; } abort with custom message
                • -
                • cd foo || ! echo "Failure" omitting "abort with custom message"
                • -
                • if cd foo; then echo "Ok"; else echo "Fail"; fi for custom handling
                • +
                • cd foo || exit as suggested to abort immediately, using exit code from failed cd command
                • +
                • cd foo || { echo "Failure"; exit 1; } abort with custom message
                • +
                • cd foo || ! echo "Failure" omitting "abort with custom message"
                • +
                • if cd foo; then echo "Ok"; else echo "Fail"; fi for custom handling
                • <(cd foo && cmd) as an alternative to <(cd foo || exit; cmd) in <(..), $(..) or ( )

                Exceptions

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2165.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2165.html index 1d859e0..45a8680 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2165.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2165.html @@ -1,7 +1,7 @@

                This nested loop overrides the index variable of its parent.

                -

                And companion warning "This parent loop has its index variable overridden."

                +

                And companion warning "This parent loop has its index variable overridden."

                Problematic code

                -
                for((i=0; i<10; i++))
                +
                for((i=0; i<10; i++))
                 do
                   for i in *
                   do
                @@ -10,7 +10,7 @@ 

                Problematic code

                done

                Correct code

                -
                for((i=0; i<10; i++))
                +
                for((i=0; i<10; i++))
                 do
                   for j in *
                   do
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.html
                index 8455441..adf513f 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.html
                @@ -1,17 +1,18 @@
                -

                Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.

                +

                Prefer [ p ] && [ q ] as [ p -a q ] is not well-defined.

                And likewise, prefer [ p ] || [ q ] over [ p -o q ].

                Problematic code

                -
                [ "$1" = "test" -a -z "$2" ]
                +
                [ "$1" = "test" -a -z "$2" ]
                 

                Correct code

                -
                [ "$1" = "test" ] && [ -z "$2" ]
                +
                [ "$1" = "test" ] && [ -z "$2" ]
                 

                Rationale

                -a and -o to mean AND and OR in a [ .. ] test expression is not well defined, and can cause incorrect results when arguments start with dashes or contain !. From POSIX:

                -

                The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:

                -

                test "$1" -a "$2"

                +
                +

                The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:

                +

                test "$1" -a "$2"

                should be written as:

                -

                test "$1" && test "$2"

                +

                test "$1" && test "$2"

                Using multiple [ .. ] expressions with shell AND/OR operators && and || is well defined and therefore preferred (but note that they have equal precedence, while -a/-o is unspecified but usually implemented as -a having higher precedence).

                Exceptions

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.json index 764534e..d647fc0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2166.json @@ -1,5 +1,5 @@ { - "title": "Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.", + "title": "Prefer [ p ] && [ q ] as [ p -a q ] is not well-defined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.html index 8ade3c8..8530d4f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.html @@ -1,10 +1,10 @@ -

                'local' is only valid in functions.

                +

                local is only valid in functions.

                Problematic code

                -
                local foo=bar
                +
                local foo=bar
                 echo "$foo"
                 

                Correct code

                -
                foo=bar
                +
                foo=bar
                 echo "$foo"
                 

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.json index cca6471..7c600bf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2168.json @@ -1,5 +1,5 @@ { - "title": "'local' is only valid in functions.", + "title": "local is only valid in functions.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2169.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2169.html index 6eae81d..46e4334 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2169.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2169.html @@ -1,3 +1,4 @@

                In dash, something is not supported.

                +

                Note: This warning has been retired in favor of individual SC3xxx warnings for each individual issue. Removed in V0.7.2 - 2021-04-19

                You are writing a script for dash, but you're using a feature that dash doesn't support. See [[SC2039]], the equivalent warning for POSIX sh, for possible workarounds.

                See also Ubuntu's DashAsBinSh migration guide for how to make bash-specific scripts dash-compatible.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2170.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2170.html index 1ea4b6f..9e9ea2e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2170.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2170.html @@ -1,6 +1,6 @@ -

                Invalid number for -eq. Use = to compare as string (or use $var to expand as a variable).

                +

                Invalid number for -eq. Use = to compare as string (or use $var to expand as a variable).

                Problematic code

                -
                read -r n
                +
                read -r n
                 if [ n -lt 0 ]
                 then
                    echo "bad input"
                @@ -12,7 +12,7 @@ 

                Problematic code

                fi

                Correct code

                -
                read -r n
                +
                read -r n
                 if [ "$n" -lt 0 ]        # Numerical comparison
                 then
                    echo "bad input"
                @@ -26,6 +26,6 @@ 

                Correct code

                Rationale

                You are comparing a string value with a numerical operator, such as -eq, -ne, -lt or -gt. These only work for numbers.

                If you want to compare the value as a string, switch to the equivalent string operator: =, != \< or \>.

                -

                If you want to compare it as a number, such as n=42; while [ n -gt 1024/8 ]; .., then keep the operator and expand the operands yourself with $var or $((expr)): while [ "$n" -gt $((1024/8)) ]

                +

                If you want to compare it as a number, such as n=42; while [ n -gt 1024/8 ]; .., then keep the operator and expand the operands yourself with $var or $((expr)): while [ "$n" -gt $((1024/8)) ]

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2171.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2171.html index 5883b36..726b358 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2171.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2171.html @@ -1,15 +1,15 @@ -

                Found trailing ] outside test. Add missing [ or quote if intentional.

                +

                Found trailing ] outside test. Add missing [ or quote if intentional.

                Problematic code

                -
                if foo -eq bar ]; then true; fi
                +
                if foo -eq bar ]; then true; fi
                 

                or

                -
                tr -d ]
                +
                tr -d ]
                 

                Correct code

                -
                if [ foo -eq bar ]; then true; fi
                +
                if [ foo -eq bar ]; then true; fi
                 

                or

                -
                tr -d ']'
                +
                tr -d ']'
                 

                Rationale

                ShellCheck found a non-test command that ends with ] or ]].

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.html index bcd9743..483c245 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.html @@ -1,9 +1,9 @@ -

                Trapping signals by number is not well defined. Prefer signal names.

                +

                Trapping signals by number is not well-defined. Prefer signal names.

                Problematic code

                -
                trap myfunc 28
                +
                trap myfunc 28
                 

                Correct code

                -
                trap myfunc WINCH
                +
                trap myfunc WINCH
                 

                Rationale

                Signal numbers can vary between platforms. Prefer signal names, which are fixed.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.json index d450657..c82ba7c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2172.json @@ -1,5 +1,5 @@ { - "title": "Trapping signals by number is not well defined. Prefer signal names.", + "title": "Trapping signals by number is not well-defined. Prefer signal names.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2173.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2173.html index fde05e4..2600e0a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2173.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2173.html @@ -1,6 +1,6 @@

                SIGKILL/SIGSTOP can not be trapped.

                Problematic code

                -
                trap 'echo "unkillable"' KILL
                +
                trap 'echo "unkillable"' KILL
                 

                Correct code

                Not applicable. This is not possible.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2174.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2174.html index c2c51f7..e95f64e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2174.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2174.html @@ -1,12 +1,12 @@ -

                When used with -p, -m only applies to the deepest directory.

                +

                When used with -p, -m only applies to the deepest directory.

                Problematic code

                -
                mkdir -p -m 0755 foo/bar/baz
                +
                mkdir -p -m 0755 foo/bar/baz
                 

                Correct code

                -
                mkdir -p foo/bar/baz
                +
                mkdir -p foo/bar/baz
                 chmod 0755 foo/bar/baz foo/bar foo
                 

                Rationale

                When using -m 0755, the mode of the directory created will be set to 0755. When using -p, parent directories which do not exist will be created, but the mode specified by -m will only be used on the last directory. The parent directories will get their access mode the default way, via umask(2).

                Exceptions

                -

                ShellCheck does not warn if the path only has one component, as in mkdir -p -m 0755 mydir, but will not attempt to determine whether this applies for a variable as in mkdir -p -m 0755 "$mydir". You can mkdir/chmod separately or [[ignore]] this message.

                +

                ShellCheck does not warn if the path only has one component, as in mkdir -p -m 0755 mydir, but will not attempt to determine whether this applies for a variable as in mkdir -p -m 0755 "$mydir". You can mkdir/chmod separately or [[ignore]] this message.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2175.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2175.html index 7e7da15..e826b56 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2175.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2175.html @@ -1,9 +1,9 @@

                Quote this invalid brace expansion since it should be passed literally to eval

                Problematic code

                -
                eval echo {1..$n}
                +
                eval echo {1..$n}
                 

                Correct code

                -
                eval "echo {1..$n}"
                +
                eval "echo {1..$n}"
                 

                Rationale

                Using eval somecommand {1..$n} depends both on bash silently failing to interpret the brace expansion, and on it passing failing brace expansions literally.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.html index 6d88dd9..9efaf96 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.html @@ -1,13 +1,13 @@ -

                'time' is undefined for pipelines. time single stage or bash -c instead.

                +

                time is undefined for pipelines. time single stage or bash -c instead.

                Problematic code

                -
                time foo | bar
                +
                time foo | bar
                 

                Correct code

                To time the most relevant stage:

                -
                foo | { time bar; }
                +
                foo | { time bar; }
                 

                To time everything in a pipeline:

                -
                time bash -c 'foo | bar'
                +
                time bash -c 'foo | bar'
                 

                Note that you can not use time sh -c to time an entire pipeline, because POSIX does not guarantee that anything other than the last stage is waited upon by the shell.

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.json index 6955724..3d58c12 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2176.json @@ -1,5 +1,5 @@ { - "title": "'time' is undefined for pipelines. time single stage or bash -c instead.", + "title": "time is undefined for pipelines. time single stage or bash -c instead.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.html index 88a5fef..16090e0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.html @@ -1,9 +1,9 @@ -

                'time' is undefined for compound commands, use time sh -c instead.

                +

                time is undefined for compound commands, use time sh -c instead.

                Problematic code

                -
                time for i in *.bmp; do convert "$i" "$i.png"; done
                +
                time for i in *.bmp; do convert "$i" "$i.png"; done
                 

                Correct code

                -
                time sh -c 'for i in *.bmp; do convert "$i" "$i.png"; done'
                +
                time sh -c 'for i in *.bmp; do convert "$i" "$i.png"; done'
                 

                Rationale

                time is only defined for Simple Commands by POSIX. Timing loops, command groups and similar is not.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.json index 82eb2ee..01cc92a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2177.json @@ -1,5 +1,5 @@ { - "title": "'time' is undefined for compound commands, use time sh -c instead.", + "title": "time is undefined for compound commands, use time sh -c instead.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2178.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2178.html index d9f92b5..e267da2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2178.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2178.html @@ -1,15 +1,16 @@

                Variable was used as an array but is now assigned a string.

                Problematic code

                -
                flags[0]="-r"
                +
                flags[0]="-r"
                 flags[1]="--delete-after"
                 
                 if [ "$dryrun" ]
                 then
                    flags="--dry-run"
                 fi
                +
                 

                Correct code

                -
                flags[0]="-r"
                +
                flags[0]="-r"
                 flags[1]="--delete-after"
                 
                 if [ "$dryrun" ]
                @@ -19,15 +20,15 @@ 

                Correct code

                Rationale

                ShellCheck noticed that you have used a variable as an array, but then assign it a string. array=foo is equivalent to array[0]=foo, and leaves the rest of the elements unaffected.

                -

                In the incorrect code, "${flags[@]}" would contain --dry-run --delete-after.

                +

                In the incorrect code, "${flags[@]}" would contain --dry-run --delete-after.

                To set an array to only a single, given element, you should use array=( foo ).

                -

                In the correct code, "${flags[@]}" will contain --dry-run only.

                +

                In the correct code, "${flags[@]}" will contain --dry-run only.

                Another possible cause is accidentally missing the $ on a previous assignment: var=(my command); var=bar instead of var=$(my command); var=bar. If the variable is not intended to be an array, ensure that it's never assigned as one.

                Bugs

                There is a known issue with this check's handling of local variables, causing ShellCheck to flag variables that were previously declared as arrays, even if they are in different scopes.

                The easiest workaround is to simply use different variable names. Alternatively, you can [[ignore]] the check.

                It is also possible to satisfy ShellCheck by declaring the local variable separately from assigning to it, e.g.:

                -
                foo () {
                +
                foo () {
                    local -a baz
                    baz+=("foo" "bar")
                    echo "${baz[@]}"
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.html
                index 7af3380..0c5c184 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.html
                @@ -1,10 +1,10 @@
                -

                Use array+=("item") to append items to an array.

                +

                Use array+=("item") to append items to an array.

                Problematic code

                -
                var=(one two)
                +
                var=(one two)
                 var+=three
                 

                Correct code

                -
                var=(one two)
                +
                var=(one two)
                 var+=( three )
                 

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.json index a927aff..020fc1d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2179.json @@ -1,5 +1,5 @@ { - "title": "Use array+=('item') to append items to an array.", + "title": "Use array+=("item") to append items to an array.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2180.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2180.html index 26c243e..61c72c3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2180.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2180.html @@ -1,20 +1,20 @@

                Bash does not support multidimensional arrays. Use 1D or associative arrays.

                Problematic code

                -
                foo[1][2]=bar
                +
                foo[1][2]=bar
                 echo "${foo[1][2]}"
                 

                Correct code

                In bash4, consider using associative arrays:

                -
                declare -A foo
                +
                declare -A foo
                 foo[1,2]=bar
                 echo "${foo[1,2]}"
                 

                Otherwise, do your own index arithmetic:

                -
                size=10
                +
                size=10
                 foo[1*size+2]=bar
                 echo "${foo[1*size+2]}"
                 

                Rationale

                -

                Bash does not support multidimensional arrays. Rewrite it to use 1D arrays. Associative arrays map arbitrary strings to values, and are therefore useful since you can construct keys like "1,2,3" or "val1;val2;val3" to index them.

                +

                Bash does not support multidimensional arrays. Rewrite it to use 1D arrays. Associative arrays map arbitrary strings to values, and are therefore useful since you can construct keys like "1,2,3" or "val1;val2;val3" to index them.

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.html index 85146de..682c3f0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.html @@ -1,6 +1,6 @@ -

                Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

                +

                Check exit code directly with e.g. if mycmd;, not indirectly with $?.

                Problematic code

                -
                make mytarget
                +
                make mytarget
                 
                 if [ $? -ne 0 ]
                 then
                @@ -8,23 +8,32 @@ 

                Problematic code

                fi

                Correct code

                -
                if ! make mytarget;
                +
                if ! make mytarget
                 then
                   echo "Build failed"
                 fi
                 
                +

                For the Solaris 10 Bourne shell:

                +
                if make mytarget
                +then
                +  :
                +else
                +  echo "Build failed"
                +fi
                +

                Rationale

                Running a command and then checking its exit status $? against 0 is redundant.

                Instead of just checking the exit code of a command, it checks the exit code of a command (e.g. [) that checks the exit code of a command.

                Apart from the redundancy, there are other reasons to avoid this pattern:

                  -
                • Since the command and its status test are decoupled, inserting an innocent command like echo "make finished" after make will cause the if statement to silently start comparing echo's status instead.
                • -
                • Scripts that run or are called with set -e aka errexit will exit immediately if the command fails, even though they're followed by a clause that handles failure.
                • -
                • The value of $? is overwritten by [/[[, so you can't get the original value in the relevant then/else block (e.g. if mycmd; then echo "Success"; else echo "Failed with $?"; fi).
                • +
                • Since the command and its status test are decoupled, inserting an innocent command like echo "make finished" after make will cause the if statement to silently start comparing echo's status instead.
                • +
                • Scripts that run or are called with set -e aka errexit will exit immediately if the command fails, even though they're followed by a clause that handles failure.
                • +
                • The value of $? is overwritten by [/[[, so you can't get the original value in the relevant then/else block (e.g. if mycmd; then echo "Success"; else echo "Failed with $?"; fi).

                To check that a command returns success, use if mycommand; then ....

                To check that a command returns failure, use if ! mycommand; then .... Notice that ! will overwrite $? value.

                To additionally capture output with command substitution: if ! output=$(mycommand); then ...

                This also applies to while/until loops.

                +

                The default Solaris 10 Bourne shell does not support negating exit statuses with !, so ! mycommand tries to invoke a utility named "!" instead. To test for failure, use if mycommand; then :; else ...; fi and until mycommand; do ...; done.

                Exceptions

                -

                The default Solaris 10 bourne shell does not support '!' outside of the test command (if ! mycommand; then ... returns !: not found)

                +

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.json index 8cdd721..a01dd1c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2181.json @@ -1,5 +1,5 @@ { - "title": "Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.", + "title": "Check exit code directly with e.g. if mycmd;, not indirectly with $?.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2182.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2182.html index 7b9582e..11248a6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2182.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2182.html @@ -1,10 +1,10 @@

                This printf format string has no variables. Other arguments are ignored.

                Problematic code

                -
                place="world"
                +
                place="world"
                 printf hello $place
                 

                Correct code

                -
                place="world"
                +
                place="world"
                 printf "hello %s\n" "$place"
                 

                Rationale

                @@ -15,13 +15,12 @@

                Rationale

                $ printf hello world\\n
                 hello
                 
                -$ printf "hello world\n"
                +$ printf "hello world\n"
                 hello world
                 
                -$ printf "hello %s\n" "world"
                -hello world
                -
                +$ printf "hello %s\n" "world" +hello world

                Exceptions

                If you wanted a no-op, use : instead.

                -
                : ${place=world}
                +
                : ${place=world}
                 
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.html index 8a25423..8faf806 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.html @@ -1,9 +1,9 @@ -

                This format string has 2 variables, but is passed 1 arguments.

                +

                This format string has 2 variables, but is passed 1 argument.

                Problematic code

                -
                printf "Hello %s, welcome to %s.\n" "$USER"
                +
                printf "Hello %s, welcome to %s.\n" "$USER"
                 

                Correct code

                -
                printf "Hello %s, welcome to %s.\n" "$USER" "$HOSTNAME"
                +
                printf "Hello %s, welcome to %s.\n" "$USER" "$HOSTNAME"
                 

                Rationale

                ShellCheck has noticed that you're using a printf format string with more %s variables than arguments to fill them.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.json index 5b3ff13..292f425 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2183.json @@ -1,5 +1,5 @@ { - "title": "This format string has 2 variables, but is passed 1 arguments.", + "title": "This format string has 2 variables, but is passed 1 argument.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2184.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2184.html index 9b4c277..3733cff 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2184.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2184.html @@ -1,13 +1,16 @@

                Quote arguments to unset so they're not glob expanded.

                Problematic code

                -
                unset foo[index]
                +
                unset foo[index]
                 

                Correct code

                -
                unset 'foo[index]'
                +
                unset 'foo[index]'
                 

                Rationale

                Arguments to unset are subject to regular glob expansion. This is especially relevant when unsetting indices in arrays, where [..] is considered a glob character group.

                In the problematic code, having a file called food in the current directory will result in unset foo[index] expanding to unset food, which will silently succeed without unsetting the element.

                Quoting so that the [..] is passed literally to unset solves the issue.

                +

                Note that you can unset element using variable for index name like this:

                +
                unset 'foo[$var]'
                +

                Exceptions

                -

                None.

                +

                If you know that pathname expansion is disabled you can ignore this message. set -o noglob (and variations like invoking the script with #!/bin/bash -f) will prevent glob expansion of arguments to unset.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.html index bf6f50b..22daebd 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.html @@ -1,9 +1,9 @@ -

                Some finds don't have a default path. Specify '.' explicitly.

                +

                Some finds don't have a default path. Specify . explicitly.

                Problematic code

                -
                find -type f
                +
                find -type f
                 

                Correct code

                -
                find . -type f
                +
                find . -type f
                 

                Rationale

                When not provided a search path, GNU and Busybox find will use a default path of ., the current directory.

                @@ -11,6 +11,5 @@

                Rationale

                Explicitly specifying a path works across all implementations, and is therefore preferred.

                Exceptions

                You will get a false positive if you concatenate a series of pre-path flags:

                -
                find -XLE .
                -
                +
                find -XLE .

                In such cases, please either use find -X -L -E . or [[ignore]] the message.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.json index 0e9409b..991eb07 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2185.json @@ -1,5 +1,5 @@ { - "title": "Some finds don't have a default path. Specify '.' explicitly.", + "title": "Some finds don't have a default path. Specify . explicitly.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2186.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2186.html index 39dd5be..f2d8307 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2186.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2186.html @@ -1,13 +1,14 @@

                tempfile is deprecated. Use mktemp instead.

                Problematic code

                -
                tmp=$(tempfile)
                +
                tmp=$(tempfile)
                 

                Correct code

                -
                tmp=$(mktemp)
                +
                tmp=$(mktemp)
                 

                Rationale

                tempfile is a Debian specific utility for creating temporary files. Its man page notes:

                -

                tempfile is deprecated; you should use mktemp(1) instead.

                +
                +

                tempfile is deprecated; you should use mktemp(1) instead.

                Neither tempfile nor mktemp are POSIX, but tempfile is Debian specific while mktemp works on GNU, OSX, BusyBox, *BSD and Solaris.

                Exceptions

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.html index 2f19350..e90ae3f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.html @@ -1,10 +1,10 @@ -

                Ash scripts will be checked as Dash. Add '# shellcheck shell=dash' to silence.

                +

                Ash scripts will be checked as Dash. Add # shellcheck shell=dash to silence.

                Problematic code

                -
                #!/bin/ash
                +
                #!/bin/ash
                 echo "Hello World"
                 

                Correct code

                -
                #!/bin/ash
                +
                #!/bin/ash
                 # shellcheck shell=dash
                 echo "Hello World"
                 
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.json index 2ea11c6..cdea3e0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2187.json @@ -1,5 +1,5 @@ { - "title": "Ash scripts will be checked as Dash. Add '# shellcheck shell=dash' to silence.", + "title": "Ash scripts will be checked as Dash. Add # shellcheck shell=dash to silence.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.html index 2324d60..98bc959 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.html @@ -1,6 +1,6 @@ -

                This redirection doesn't have a command. Move to its command (or use 'true' as no-op).

                +

                This redirection doesn't have a command. Move to its command (or use true as no-op).

                Problematic code

                -
                { 
                +
                { 
                   echo "Report for $(date +%F)"
                   uptime
                   df -h
                @@ -8,7 +8,7 @@ 

                Problematic code

                > report.txt

                Correct code

                -
                { 
                +
                { 
                   echo "Report for $(date +%F)"
                   uptime
                   df -h
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.json
                index b8cbf98..d6ac03b 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.json
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2188.json
                @@ -1,5 +1,5 @@
                 {
                -  "title": "This redirection doesn't have a command. Move to its command (or use 'true' as no-op).",
                +  "title": "This redirection doesn't have a command. Move to its command (or use true as no-op).",
                   "type": "BUG",
                   "status": "ready",
                   "remediation": {
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2189.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2189.html
                index f6235fa..e58c4ca 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2189.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2189.html
                @@ -1,9 +1,9 @@
                -

                You can't have | between this redirection and the command it should apply to.

                +

                You can't have | between this redirection and the command it should apply to.

                Problematic code

                -
                < file.txt | grep foo
                +
                < file.txt | grep foo
                 

                Correct code

                -
                < file.txt grep foo   # or more canonically: grep foo < file.txt
                +
                < file.txt grep foo   # or more canonically: grep foo < file.txt
                 

                Rationale

                ShellCheck has found a stage in a pipeline that consists of a redirection but no command. This doesn't make sense because a redirection without a command will not read or write any data.

                @@ -18,4 +18,4 @@

                Rationale

                grep bar < foo.txt

                Exceptions

                -

                It's technically valid to do e.g. echo foo | > "$(cat)" to truncate a file called "foo", but please consider rewriting such code.

                +

                It's technically valid to do e.g. echo foo | > "$(cat)" to truncate a file called "foo", but please consider rewriting such code.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2190.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2190.html index b9acfdb..ce2a83f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2190.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2190.html @@ -1,10 +1,10 @@ -

                Elements in associative arrays need index, e.g. array=( [index]=value ) .

                +

                Elements in associative arrays need index, e.g. array=( [index]=value ) .

                Problematic code

                -
                declare -A foo
                +
                declare -A foo
                 foo=( myvalue )
                 

                Correct code

                -
                declare -A foo
                +
                declare -A foo
                 foo=( [key]=myvalue )
                 

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2191.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2191.html index 2fb1c94..d7d96f7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2191.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2191.html @@ -1,14 +1,14 @@ -

                The = here is literal. To assign by index, use ( [index]=value ) with no spaces. To keep as literal, quote it.

                +

                The = here is literal. To assign by index, use ( [index]=value ) with no spaces. To keep as literal, quote it.

                Problematic code

                -
                array=( [index] = value )
                +
                array=( [index] = value )
                 

                Correct code

                -
                array=( [index]=value )
                +
                array=( [index]=value )
                 

                Rationale

                The shell doesn't care about the = sign in your array assignment because it's not part of a recognized index assignment. Instead, it's considered a literal character and becomes part of an array element's value.

                In the example problematic code, this is because the = was intended to set the index, but the shell will not recognize it when it is surrounded by spaces.

                Make sure to remove any spaces around the = when assigning by index, such as in the correct code.

                -

                If you wanted the = to be a literal part of the array element, add quotes around it, such as env=( "LC_CTYPE=C" ) or specialChars=( "=" "%" ";" ) .

                +

                If you wanted the = to be a literal part of the array element, add quotes around it, such as env=( "LC_CTYPE=C" ) or specialChars=( "=" "%" ";" ) .

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.html index 16f7eaa..35d5f1d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.html @@ -1,14 +1,14 @@ -

                This array element has no value. Remove spaces after = or use "" for empty string.

                +

                This array element has no value. Remove spaces after = or use "" for empty string.

                Problematic code

                -
                array=([1]=one [2]= two)
                +
                array=([1]=one [2]= two)
                 

                Correct code

                -
                array=([1]=one [2]=two)
                +
                array=([1]=one [2]=two)
                 

                Rationale

                You have an array element on the form [index]=. The shell will interpret this as an independent element with index index and value <empty string>.

                -

                This may happen as part of the expression [index]= value, where the space is not allowed and causes the shell to interpret it as [index]="" [index+1]=value.

                +

                This may happen as part of the expression [index]= value, where the space is not allowed and causes the shell to interpret it as [index]="" [index+1]=value.

                If you wanted the element to have a value, remove the spaces after =, e.g. [index]=value.

                -

                If you wanted to assign an empty string, explicitly use empty quotes: [index]="". This makes no difference to the shell, but will make your intention clear to shellcheck and other humans.

                +

                If you wanted to assign an empty string, explicitly use empty quotes: [index]="". This makes no difference to the shell, but will make your intention clear to shellcheck and other humans.

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.json index 379ccbd..ec18820 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2192.json @@ -1,5 +1,5 @@ { - "title": "This array element has no value. Remove spaces after = or use '' for empty string.", + "title": "This array element has no value. Remove spaces after = or use "" for empty string.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2193.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2193.html index 04e6ff6..c86545d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2193.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2193.html @@ -1,12 +1,12 @@

                The arguments to this comparison can never be equal. Make sure your syntax is correct.

                Problematic code

                -
                [ $var+1 == 5 ]              # Unevaluated math
                +
                [ $var+1 == 5 ]              # Unevaluated math
                 [ "{$var}" == "value" ]      # Swapped around $ and {
                 [ "$(cmd1) | cmd2" == "42" ] # Ended with ) too soon
                 [[ "$var " == *.png ]]       # Trailing space
                 

                Correct code

                -
                [ $((var+1)) == 5 ]          # Evaluated math
                +
                [ $((var+1)) == 5 ]          # Evaluated math
                 [ "${var}" == "value" ]      # Correct variable expansion
                 [ "$(cmd1 | cmd2)" == "42" ] # Correct command substitution
                 [[ "$var" == *.png ]]        # No trailing space
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2194.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2194.html
                index 82ff5f3..ec0a225 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2194.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2194.html
                @@ -1,11 +1,11 @@
                -

                This word is constant. Did you forget the $ on a variable?

                +

                This word is constant. Did you forget the $ on a variable?

                Problematic code

                -
                case foo in
                +
                case foo in
                   bar) echo "Match"
                 esac
                 

                Correct code

                -
                case $foo in
                +
                case $foo in
                   bar) echo "Match"
                 esac
                 
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2195.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2195.html index f57418e..1762af2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2195.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2195.html @@ -1,11 +1,11 @@

                This pattern will never match the case statement's word. Double check them.

                Problematic code

                -
                case "$var " in      # Trailing space
                +
                case "$var " in      # Trailing space
                   value) echo "Match"
                 esac
                 

                Correct code

                -
                case "${var}" in     # No trailing space
                +
                case "${var}" in     # No trailing space
                   value) echo "Match"
                 esac
                 
                @@ -14,8 +14,8 @@

                Rationale

                Often, this is due to mistakes in the case statement word that results in unintended literal characters. In the problematic code, there's a trailing space that will prevent the match from ever succeeding.

                For more examples of when this could happen, see SC2193 for the equivalent warning for [[ .. ]] statements.

                Note that ShellCheck warns about individual patterns in a branch, and will flag *.png in this example even though the branch is not dead:

                -
                case "${img}.jpg" in
                -  *.png | *.jpg) echo "It's an image"
                +
                case "${img}.jpg" in
                +  *.png | *.jpg) echo "It's an image"
                 esac
                 

                Exceptions

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2196.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2196.html index 6c539b7..f5759d8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2196.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2196.html @@ -1,16 +1,18 @@ -

                egrep is non-standard and deprecated. Use grep -E instead.

                +

                egrep is non-standard and deprecated. Use grep -E instead.

                Problematic code

                -
                egrep 'foo|bar' file
                +
                egrep 'foo|bar' file
                 

                Correct code

                -
                grep -E 'foo|bar' file
                +
                grep -E 'foo|bar' file
                 

                Rationale

                egrep is a non-standard command. Its functionality is provided in POSIX by grep -E. POSIX grep says:

                -

                This grep has been enhanced in an upwards-compatible way to provide the exact functionality of the historical egrep and fgrep commands as well. It was the clear intention of the standard developers to consolidate the three greps into a single command.

                +
                +

                This grep has been enhanced in an upwards-compatible way to provide the exact functionality of the historical egrep and fgrep commands as well. It was the clear intention of the standard developers to consolidate the three greps into a single command.

                man grep for GNU says:

                -

                Direct invocation as either egrep or fgrep is deprecated

                +
                +

                Direct invocation as either egrep or fgrep is deprecated

                Exceptions

                ShellCheck will fail to recognize when functions override egrep. Consider giving it a different name or [[ignore]] this error.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2197.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2197.html index 6863ec6..c05dcb2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2197.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2197.html @@ -1,16 +1,18 @@ -

                fgrep is non-standard and deprecated. Use grep -F instead.

                +

                fgrep is non-standard and deprecated. Use grep -F instead.

                Problematic code

                -
                fgrep '*.*' file
                +
                fgrep '*.*' file
                 

                Correct code

                -
                grep -F '*.*' file
                +
                grep -F '*.*' file
                 

                Rationale

                fgrep is a non-standard command. Its functionality is provided in POSIX by grep -F. POSIX grep says:

                -

                This grep has been enhanced in an upwards-compatible way to provide the exact functionality of the historical egrep and fgrep commands as well. It was the clear intention of the standard developers to consolidate the three greps into a single command.

                +
                +

                This grep has been enhanced in an upwards-compatible way to provide the exact functionality of the historical egrep and fgrep commands as well. It was the clear intention of the standard developers to consolidate the three greps into a single command.

                man grep for GNU says:

                -

                Direct invocation as either egrep or fgrep is deprecated

                +
                +

                Direct invocation as either egrep or fgrep is deprecated

                Exceptions

                ShellCheck will fail to recognize when functions override fgrep. Consider giving it a different name or [[ignore]] this error.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2198.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2198.html index fcf888e..6bdfd1a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2198.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2198.html @@ -1,11 +1,11 @@ -

                Arrays don't work as operands in [ ]. Use a loop (or concatenate with * instead of @).

                +

                Arrays don't work as operands in [ ]. Use a loop (or concatenate with * instead of @).

                Problematic code

                -
                ext=png
                +
                ext=png
                 allowedExt=(jpg bmp png)
                 [ "$ext" = "${allowedExt[@]}" ] && echo "Extension is valid"
                 

                Correct code

                -
                ext=png
                +
                ext=png
                 allowedExt=(jpg bmp png)
                 for value in "${allowedExt[@]}"
                 do
                @@ -14,8 +14,8 @@ 

                Correct code

                Rationale

                Array expansions become a series of words in [ .. ]. Operators expect single words only.

                -

                The problematic code is equivalent to [ "$ext" = jpg bmp png ], which is invalid syntax. A typical error message is bash: [: too many arguments or dash: somefile: unexpected operator.

                +

                The problematic code is equivalent to [ "$ext" = jpg bmp png ], which is invalid syntax. A typical error message is bash: [: too many arguments or dash: somefile: unexpected operator.

                Instead, use a for loop to iterate over values, and apply your condition to each.

                -

                Alternatively, if you want to concatenate all the values in the array into a single string for your test, use "$*" or "${array[*]}".

                +

                Alternatively, if you want to concatenate all the values in the array into a single string for your test, use "$*" or "${array[*]}".

                Exceptions

                -

                If you are dynamically building an a test expression, make your array the only thing in the test expression. ShellCheck will not emit a warning for: set -- 1 -lt 2; [ "$@" ]

                +

                If you are dynamically building an a test expression, make your array the only thing in the test expression. ShellCheck will not emit a warning for: set -- 1 -lt 2; [ "$@" ]

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2199.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2199.html index 34be2b4..025e6ab 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2199.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2199.html @@ -1,11 +1,11 @@ -

                Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @).

                +

                Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @).

                Problematic code

                -
                ext=png
                +
                ext=png
                 allowedExt=(jpg bmp png)
                 [[ "$ext" = "${allowedExt[@]}" ]] && echo "Extension is valid"
                 

                Correct code

                -
                ext=png
                +
                ext=png
                 allowedExt=(jpg bmp png)
                 for value in "${allowedExt[@]}"
                 do
                @@ -13,8 +13,8 @@ 

                Correct code

                done

                Rationale

                -

                Array expansions in [[ .. ]] will implicitly concatenate into a single string, much like in assignments. The problematic code is equivalent to [ "$ext" = "jpg bmp png" ].

                +

                Array expansions in [[ .. ]] will implicitly concatenate into a single string, much like in assignments. The problematic code is equivalent to [ "$ext" = "jpg bmp png" ].

                Instead, use a for loop to iterate over values, and apply your condition to each.

                -

                Alternatively, if you do want to concatenate all the values in the array into a single string for your test, use "$*" or "${array[*]}" to make this explicit.

                +

                Alternatively, if you do want to concatenate all the values in the array into a single string for your test, use "$*" or "${array[*]}" to make this explicit.

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2200.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2200.html index 5e00923..4ded28c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2200.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2200.html @@ -1,16 +1,17 @@ -

                Brace expansions don't work as operands in [ ]. Use a loop.

                +

                Brace expansions don't work as operands in [ ]. Use a loop.

                Problematic code

                -
                [ "$file" = index.{htm,html,php} ] && echo "This is the main file"
                +
                [ "$file" = index.{htm,html,php} ] && echo "This is the main file"
                +
                 

                Correct code

                -
                for main in index.{htm,html,php}
                +
                for main in index.{htm,html,php}
                 do
                   [ "$file" = "$main" ] && echo "This is the main file"
                 done
                 

                Rationale

                Brace expansions in [ ] will expand to a sequence of words. Operators work on single words.

                -

                The problematic code is equivalent to [ "$file" = index.htm index.html index.php ], which is invalid syntax. A typical error message is bash: [: too many arguments or dash: somefile: unexpected operator.

                +

                The problematic code is equivalent to [ "$file" = index.htm index.html index.php ], which is invalid syntax. A typical error message is bash: [: too many arguments or dash: somefile: unexpected operator.

                Instead, use a for loop to iterate over values, and apply your condition to each.

                Exceptions

                None.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2201.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2201.html index 907ecaa..8269f32 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2201.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2201.html @@ -1,9 +1,9 @@

                Brace expansion doesn't happen in [[ ]]. Use a loop.

                Problematic code

                -
                [[ "$file" = index.{htm,html,php} ]] && echo "This is the main file"
                +
                [[ "$file" = index.{htm,html,php} ]] && echo "This is the main file"
                 

                Correct code

                -
                for main in index.{htm,html,php}
                +
                for main in index.{htm,html,php}
                 do
                   [[ "$file" = "$main" ]] && echo "This is the main file"
                 done
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2202.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2202.html
                index a606e38..5c44fc8 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2202.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2202.html
                @@ -1,9 +1,10 @@
                -

                Globs don't work as operands in [ ]. Use a loop.

                +

                Globs don't work as operands in [ ]. Use a loop.

                Problematic code

                -
                [ current.log -nt backup/*.log ] && echo "This is the latest file"
                +
                [ current.log -nt backup/*.log ] && echo "This is the latest file"
                +
                 

                Correct code

                -
                newerThanAll=true
                +
                newerThanAll=true
                 for log in backup/*.log
                 do
                   [ current.log -nt "$log" ] || newerThanAll=false
                @@ -17,7 +18,7 @@ 

                Rationale

                Exceptions

                If you know your glob will only ever match one file, you can check this explicitly and use the first file:

                set -- backup/*.log
                -[ $# -eq 1 ] || { echo "There are too many matches."; exit 1; }
                -[ file.log -nt "$1" ] && echo "This is the latest file"
                +[ $# -eq 1 ] || { echo "There are too many matches."; exit 1; }
                +[ file.log -nt "$1" ] && echo "This is the latest file"
                 

                Alternatively, [[ignore]] this warning.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2203.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2203.html index bd90e00..a15fc82 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2203.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2203.html @@ -1,9 +1,10 @@ -

                Globs are ignored in [[ ]] except right of =/!=. Use a loop.

                +

                Globs are ignored in [[ ]] except right of =/!=. Use a loop.

                Problematic code

                -
                [[ current.log -nt backup/*.log ]] && echo "This is the latest file"
                +
                [[ current.log -nt backup/*.log ]] && echo "This is the latest file"
                +
                 

                Correct code

                -
                newerThanAll=true
                +
                newerThanAll=true
                 for log in backup/*.log
                 do
                   [[ current.log -nt "$log" ]] || newerThanAll=false
                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2204.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2204.html
                index a9c36e8..9e3da4a 100644
                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2204.html
                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2204.html
                @@ -1,14 +1,14 @@
                -

                (..) is a subshell. Did you mean [ .. ], a test expression?

                +

                (..) is a subshell. Did you mean [ .. ], a test expression?

                Problematic code

                -
                if ( -d mydir )
                +
                if ( -d mydir )
                 then
                -  echo "It's a directory"
                +  echo "It's a directory"
                 fi
                 

                Correct code

                -
                if [ -d mydir ]
                +
                if [ -d mydir ]
                 then
                -  echo "It's a directory"
                +  echo "It's a directory"
                 fi
                 

                Rationale

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2205.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2205.html index 74ca042..24c2e53 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2205.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2205.html @@ -1,12 +1,12 @@ -

                (..) is a subshell. Did you mean [ .. ], a test expression?

                +

                (..) is a subshell. Did you mean [ .. ], a test expression?

                Problematic code

                -
                if ( 1 -lt 2 )
                +
                if ( 1 -lt 2 )
                 then
                   echo "1 is less than 2"
                 fi
                 

                Correct code

                -
                if [ 1 -lt 2 ]
                +
                if [ 1 -lt 2 ]
                 then
                   echo "1 is less than 2"
                 fi
                @@ -16,5 +16,5 @@ 

                Rationale

                ( .. ) is completely unrelated, and is a subshell mostly used to scope shell modifications. They should not be used in if or while statements in shell scripts.

                If you wanted to test a condition, rewrite the ( .. ) to [ .. ].

                Exceptions

                -

                This error is triggered by having a binary operator as the first parameter in a subshell, and could falsely trigger on e.g. if ( grep -eq "foo|bar" file ). In these cases, check whether the subshell is actually needed.

                +

                This error is triggered by having a binary operator as the first parameter in a subshell, and could falsely trigger on e.g. if ( grep -eq "foo|bar" file ). In these cases, check whether the subshell is actually needed.

                Note that there's a similar looking error [[SC2204]] with a low false positive rate.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2206.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2206.html index 86b3fe8..e13c11c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2206.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2206.html @@ -1,20 +1,20 @@ -

                Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.

                +

                Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.

                Problematic code

                -
                array=( $var )
                +
                array=( $var )
                 

                Correct code

                If the variable should be a single element:

                -
                array=( "$var" )
                +
                array=( "$var" )
                 

                If it's multiple lines, each of which should be an element:

                -
                # For bash
                +
                # For bash
                 mapfile -t array <<< "$var"
                 
                 # For ksh
                -printf '%s\n' "$var" | while IFS="" read -r line; do array+=("$line"); done
                +printf '%s\n' "$var" | while IFS="" read -r line; do array+=("$line"); done
                 

                If it's a line with multiple words (separated by spaces, other delimiters can be chosen with IFS), each of which should be an element:

                -
                # For bash
                +
                # For bash
                 IFS=" " read -r -a array <<< "$var"
                 
                 # For ksh
                @@ -29,4 +29,4 @@ 

                Rationale

              This prevents the shell from doing unwanted splitting and glob expansion, and therefore avoiding problems with data containing spaces or special characters.

              Exceptions

              -

              If you have already taken care (through setting IFS and set -f) to have word splitting work the way you intend, you can ignore this warning.

              +

              If you have already taken care (through setting IFS and set -f) to have word splitting work the way you intend, you can [[ignore]] this warning.

              diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2207.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2207.html index 05f2e82..a0e5f70 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2207.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2207.html @@ -1,22 +1,22 @@ -

              Prefer mapfile or read -a to split command output (or quote to avoid splitting).

              +

              Prefer mapfile or read -a to split command output (or quote to avoid splitting).

              Problematic code

              -
              array=( $(mycommand) )
              +
              array=( $(mycommand) )
               

              Correct code

              If it outputs multiple lines, each of which should be an element:

              -
              # For bash 4.x, must not be in posix mode, may use temporary files
              +
              # For bash 4.4+, must not be in posix mode, may use temporary files
               mapfile -t array < <(mycommand)
               
               # For bash 3.x+, must not be in posix mode, may use temporary files
               array=()
              -while IFS='' read -r line; do array+=("$line"); done < <(mycommand)
              +while IFS='' read -r line; do array+=("$line"); done < <(mycommand)
               
               # For ksh, and bash 4.2+ with the lastpipe option enabled (may require disabling monitor mode)
               array=()
               mycommand | while IFS="" read -r line; do array+=("$line"); done
               
              -

              If it outputs a line with multiple words (separated by spaces), other delimiters can be chosen with IFS, each of which should be an element:

              -
              # For bash, uses temporary files
              +

              If it outputs a line with multiple words (separated by spaces, other delimiters can be chosen with IFS), each of which should be an element:

              +
              # For bash, uses temporary files
               IFS=" " read -r -a array <<< "$(mycommand)"
               
               # For bash 4.2+ with the lastpipe option enabled (may require disabling monitor mode)
              @@ -27,7 +27,7 @@ 

              Correct code

              IFS=" " read -r -A array <<< "$(mycommand)"

              If the output should be a single element:

              -
              array=( "$(mycommand)" )
              +
              array=( "$(mycommand)" )
               

              Rationale

              You are doing unquoted command expansion in an array. This will invoke the shell's sloppy word splitting and glob expansion.

              @@ -39,5 +39,4 @@

              Rationale

              This prevents the shell from doing unwanted splitting and glob expansion, and therefore avoiding problems with output containing spaces or special characters.

              Exceptions

              If you have already taken care (through setting IFS and set -f) to have word splitting work the way you intend, you can ignore this warning.

              -

              Another exception is the wish for error handling: array=( $(mycommand) ) || die-with-error works the way it looks while a similar mapfile construct like mapfile -t array < <(mycommand) doesn't fail and you will have to write more code for error handling. -```

              +

              Another exception is the wish for error handling: array=( $(mycommand) ) || die-with-error works the way it looks while a similar mapfile construct like mapfile -t array < <(mycommand) doesn't fail and you will have to write more code for error handling.

              diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2208.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2208.html index 6577d2f..238c77d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2208.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2208.html @@ -1,9 +1,9 @@ -

              Use [[ ]] or quote arguments to -v to avoid glob expansion.

              +

              Use [[ ]] or quote arguments to -v to avoid glob expansion.

              Problematic code

              -
              [ -v foo[0] ]
              +
              [ -v foo[0] ] 
               

              Correct code

              -
              [ -v 'foo[0]' ]
              +
              [ -v 'foo[0]' ]
               

              Rationale

              With [, arguments will undergo glob expansion. If a file foo0 exists when the problematic code is run, it will check for the variable foo0 instead of the array entry foo[0]. If there additionally exists a foo1, it will simply fail with an error.

              diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2209.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2209.html index c3b8a0f..7255dcf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2209.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2209.html @@ -1,11 +1,11 @@ -

              Use var=$(command) to assign output (or quote to assign string).

              +

              Use var=$(command) to assign output (or quote to assign string).

              Problematic code

              -
              user=whoami         # Want to run whoami and assign output
              +
              user=whoami         # Want to run whoami and assign output
               
               PAGER=cat git log   # Want to assign the string "cat"
               

              Correct code

              -
              user=$(whoami)
              +
              user=$(whoami)
               
               PAGER="cat" git log
               
              @@ -14,7 +14,7 @@

              Rationale

              If you do want to assign a literal string, use quotes to make this clear to shellcheck and humans alike.

              Exceptions

              None.

              -

              Quoting a single command (as in PAGER="cat" above) doesn't change how the script works. It's purely to show shellcheck (and humans) that a literal assignment of a command name is intentional.

              +

              Quoting a single command (as in PAGER="cat" above) doesn't change how the script works. It's purely to show shellcheck (and humans) that a literal assignment of a command name is intentional.

              This warning triggers generally when a variable is assigned an unquoted command name (from a list of hard coded names). See related warning [[SC2037]] which detects the same kind of error through the patterns var=value -flag and var=value *glob*.

              Related resources

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2210.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2210.html index 564f76a..f9a411c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2210.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2210.html @@ -1,22 +1,22 @@

                This is a file redirection. Was it supposed to be a comparison or fd operation?

                Problematic code

                -
                if x > 5; then echo "true"; fi
                +
                if x > 5; then echo "true"; fi
                 

                or

                -
                foo > /dev/null 2>1
                +
                foo > /dev/null 2>1
                 

                Correct code

                -
                if (( x > 5 )); then echo "true"; fi
                +
                if (( x > 5 )); then echo "true"; fi
                 

                or

                -
                foo > /dev/null 2>&1
                +
                foo > /dev/null 2>&1
                 

                Rationale

                You are redirecting to or from a filename that is an integer. For example, ls > file where file happens to be 3.

                This is not likely to be intentional. The most common causes are:

                  -
                1. Trying to compare two numbers, as in x > 5. This should instead be [ "$x" -gt 5 ] or (( x > 5 )).
                2. -
                3. Trying similarly to compare command output, as in grep -c foo file > 100 instead of [ "$(grep -c foo file)" -gt 100 ]
                4. +
                5. Trying to compare two numbers, as in x > 5. This should instead be [ "$x" -gt 5 ] or (( x > 5 )).
                6. +
                7. Trying similarly to compare command output, as in grep -c foo file > 100 instead of [ "$(grep -c foo file)" -gt 100 ]
                8. Malformed FD operations, such as writing 1>2 instead of 1>&2.

                If you do want to create a file named 4, you can quote it to silence shellcheck and make it more clear to humans that it's not supposed to be taken numerically.

                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2211.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2211.html index 05a8f51..db201ec 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2211.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2211.html @@ -1,18 +1,18 @@ -

                This is a glob used as a command name. Was it supposed to be in ${..}, array, or is it missing quoting?

                +

                This is a glob used as a command name. Was it supposed to be in ${..}, array, or is it missing quoting?

                Problematic code

                -
                for f in $(*.png); do echo "$f"; done   # Trying to loop over a glob
                +
                for f in $(*.png); do echo "$f"; done   # Trying to loop over a glob
                 array=$(*.txt)                          # Trying to assign an array
                 echo "$(array[1])"                      # Trying to expand an array
                 

                Correct code

                -
                for f in *.png; do echo "$f"; done
                +
                for f in *.png; do echo "$f"; done
                 array=(*.txt)
                 echo "${array[1]}"
                 

                Rationale

                You are using a glob as a command name. This is usually a mistake caused by one of the following:

                  -
                • Trying to use `*foo*` or $(*foo*) to expand a glob.
                • +
                • Trying to use `*foo*` or $(*foo*) to expand a glob.
                • Using var=$(*.txt) instead of var=(*.txt) to assign an array.
                • Using $(..) instead of ${..} when expanding an array element.
                • Running a program with a name or directory that contains glob characters without escaping them.
                • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.html index 0bb9681..197aaa3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.html @@ -1,12 +1,12 @@ -

                  Use 'false' instead of empty [/[[ conditionals.

                  +

                  Use false instead of empty [/[[ conditionals.

                  Problematic code

                  -
                  if [ ]
                  +
                  if [ ]
                   then
                     echo "Temporarily disabled"
                   fi
                   

                  Correct code

                  -
                  if false
                  +
                  if false
                   then
                     echo "Temporarily disabled"
                   fi
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.json
                  index 0e38b68..000e392 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.json
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2212.json
                  @@ -1,5 +1,5 @@
                   {
                  -  "title": "Use 'false' instead of empty [\/[[ conditionals.",
                  +  "title": "Use false instead of empty [\/[[ conditionals.",
                     "type": "CODE_SMELL",
                     "status": "ready",
                     "remediation": {
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.html
                  index 8b9b15d..47ad4e8 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.html
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.html
                  @@ -1,6 +1,6 @@
                  -

                  getopts specified -n, but it's not handled by this 'case'.

                  +

                  getopts specified -n, but it's not handled by this case.

                  Problematic code

                  -
                  while getopts "vrn" n
                  +
                  while getopts "vrn" n
                   do
                     case "$n" in
                       v) echo "Verbose" ;;
                  @@ -10,7 +10,7 @@ 

                  Problematic code

                  done

                  Correct code

                  -
                  while getopts "vrn" n
                  +
                  while getopts "vrn" n
                   do
                     case "$n" in
                       v) echo "Verbose" ;;
                  @@ -25,14 +25,14 @@ 

                  Rationale

                  Either add a case to handle the flag, or remove it from the getopts option string.

                  Exceptions

                  ShellCheck may not correctly recognize less canonical uses of while getopts ..; do case ..;, such as when modifying the variable before using it:

                  -
                  while getopts "rf-:" OPT; do
                  -  if [ "$OPT" = "-" ]; then   # long option: reformulate OPT and OPTARG
                  -    OPT="${OPTARG%%=*}"       # extract long option name
                  -    OPTARG="${OPTARG#$OPT}"   # extract long option argument (may be empty)
                  -    OPTARG="${OPTARG#=}"      # if long option argument, remove assigning `=`
                  +
                  while getopts "rf-:" OPT; do
                  +  if [ "$OPT" = "-" ]; then   # long option: reformulate OPT and OPTARG
                  +    OPT="${OPTARG%%=*}"       # extract long option name
                  +    OPTARG="${OPTARG#$OPT}"   # extract long option argument (may be empty)
                  +    OPTARG="${OPTARG#=}"      # if long option argument, remove assigning `=`
                     fi
                   
                  -  case "$OPT" in
                  +  case "$OPT" in
                       r) ... ;;
                       f) ... ;;
                       my-long-option) ... ;;
                  @@ -42,6 +42,6 @@ 

                  Exceptions

                  In such cases you can do one of:

                  • [[Ignore]] the warning.
                  • -
                  • Use the external tool getopt (no "s") which supports long options natively.
                  • +
                  • Use the external tool getopt (no "s") which supports long options natively.
                  • Rewrite to not modify the variable first, in this case by instead doing it in a -) branch.
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.json index c4a9e78..75b5f5a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2213.json @@ -1,5 +1,5 @@ { - "title": "getopts specified -n, but it's not handled by this 'case'.", + "title": "getopts specified -n, but it's not handled by this case.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2214.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2214.html index 04625d3..c478f62 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2214.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2214.html @@ -1,6 +1,6 @@

                  This case is not specified by getopts.

                  Problematic code

                  -
                  while getopts "vr" n
                  +
                  while getopts "vr" n
                   do
                     case "$n" in
                       v) echo "Verbose" ;;
                  @@ -11,7 +11,7 @@ 

                  Problematic code

                  done

                  Correct code

                  -
                  while getopts "vrn" n    # 'n' added here
                  +
                  while getopts "vrn" n    # 'n' added here
                   do
                     case "$n" in
                       v) echo "Verbose" ;;
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2215.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2215.html
                  index 41bd114..444ad9d 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2215.html
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2215.html
                  @@ -1,29 +1,29 @@
                   

                  This flag is used as a command name. Bad line break or missing [ .. ]?

                  Problematic code

                  -
                  if -e .bashrc
                  +
                  if -e .bashrc
                   then
                     echo ".bashrc already exists"
                   fi
                   

                  or

                  -
                  find . -name '*.mkv'
                  +
                  find . -name '*.mkv'
                          -exec mplayer {} \;
                   

                  Correct code

                  -
                  if [ -e .bashrc ]
                  +
                  if [ -e .bashrc ]
                   then
                     echo ".bashrc already exists"
                   fi
                   

                  or

                  -
                  find . -name '*.mkv' \
                  +
                  find . -name '*.mkv' \
                          -exec mplayer {} \;
                   

                  Rationale

                  You are using a name that starts with a dash as a command name. This is almost always a bug.

                  There are two typical ways in which this happens:

                    -
                  • Missing [ .. ] or [[ .. ]] around a test expression, like in the first example example.
                  • +
                  • Missing [ .. ] or [[ .. ]] around a test expression, like in the first example example.
                  • An invalid line break that splits a command in two, like in the second example.

                  Exceptions

                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.html index e6d43eb..69a08a3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.html @@ -1,12 +1,12 @@ -

                  Piping to 'rm', a command that doesn't read stdin. Wrong command or missing xargs?

                  +

                  Piping to rm, a command that doesn't read stdin. Wrong command or missing xargs?

                  Problematic code

                  -
                  ls | echo                      # Want to print result
                  +
                  ls | echo                      # Want to print result
                   cat files | rm                 # Want to delete items from a file
                  -find . -type f | cp dir        # Want to process 'find' output
                  +find . -type f | cp dir        # Want to process 'find' output
                   rm file | true                 # Want to ignore errors
                   

                  Correct code

                  -
                  ls
                  +
                  ls
                   cat files | while IFS= read -r file; do rm -- "$file"; done
                   find . -type f -exec cp {} dir \;
                   rm file || true
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.json
                  index b091ffb..e46799e 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.json
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2216.json
                  @@ -1,5 +1,5 @@
                   {
                  -  "title": "Piping to 'rm', a command that doesn't read stdin. Wrong command or missing xargs?",
                  +  "title": "Piping to rm, a command that doesn't read stdin. Wrong command or missing xargs?",
                     "type": "CODE_SMELL",
                     "status": "ready",
                     "remediation": {
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.html
                  index 5b1d1e4..6f7ef99 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.html
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.html
                  @@ -1,11 +1,11 @@
                  -

                  Redirecting to 'echo', a command that doesn't read stdin. Bad quoting or missing xargs?

                  +

                  Redirecting to echo, a command that doesn't read stdin. Bad quoting or missing xargs?

                  Problematic code

                  -
                  echo << eof
                  +
                  echo << eof
                     Hello World
                   eof
                   

                  Correct code

                  -
                  cat << eof
                  +
                  cat << eof
                     Hello World
                   eof
                   
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.json index 65651df..8c838a8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2217.json @@ -1,5 +1,5 @@ { - "title": "Redirecting to 'echo', a command that doesn't read stdin. Bad quoting or missing xargs?", + "title": "Redirecting to echo, a command that doesn't read stdin. Bad quoting or missing xargs?", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2218.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2218.html index fc7737d..4ebe016 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2218.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2218.html @@ -1,6 +1,6 @@

                  This function is only defined later. Move the definition up.

                  Problematic code

                  -
                  #!/bin/sh
                  +
                  #!/bin/sh
                   myfunction
                   
                   myfunction() {
                  @@ -8,7 +8,7 @@ 

                  Problematic code

                  }

                  Correct code

                  -
                  #!/bin/sh
                  +
                  #!/bin/sh
                   myfunction() {
                     echo "Hello World"
                   }
                  @@ -16,12 +16,12 @@ 

                  Correct code

                  Rationale

                  You are calling a function that you are defining later in the file. The function definition must come first.

                  -

                  Function definitions are much like variable assignments, and define a name at the point the definition is "executed". This is why they must happen before their first use.

                  +

                  Function definitions are much like variable assignments, and define a name at the point the definition is "executed". This is why they must happen before their first use.

                  This is especially apparent when defining functions conditionally:

                  -
                  case "$(uname -s)" in
                  -  Linux) hi() { echo "Hello from Linux"; } ;;
                  -  Darwin) hi() { echo "Hello from macOS"; } ;;
                  -  *) hi() { echo "Hello from something else"; } ;;
                  +
                  case "$(uname -s)" in
                  +  Linux) hi() { echo "Hello from Linux"; } ;;
                  +  Darwin) hi() { echo "Hello from macOS"; } ;;
                  +  *) hi() { echo "Hello from something else"; } ;;
                   esac
                   
                   hi
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2219.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2219.html
                  index 29069d5..20f3ab2 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2219.html
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2219.html
                  @@ -1,18 +1,19 @@
                   

                  Instead of let expr, prefer (( expr )) .

                  Problematic code

                  -
                  let a++
                  +
                  let a++
                   

                  Correct code

                  -
                  (( a++ )) || true
                  +
                  (( a++ )) || true
                   
                  -

                  Note, || true bits ignore error status code when incrementing from 0 to 1

                  +
                  +

                  Note, || true bits ignore error status code when incrementing from 0 to 1

                  Rationale

                  The (( .. )) arithmetic compound command evaluates expressions in the same way as let, except it's not subject to glob expansion and therefore requires no additional quoting or escaping.

                  -

                  This warning only triggers in Bash/Ksh scripts. In Sh/Dash, neither let nor (( .. )) are defined, but can be simulated with [ $(( expr )) -ne 0 ] to retain exit code, or : $(( expr )) to ignore it.

                  +

                  This warning only triggers in Bash/Ksh scripts. In Sh/Dash, neither let nor (( .. )) are defined, but can be simulated with [ $(( expr )) -ne 0 ] to retain exit code, or : $(( expr )) to ignore it. For portability, the $(( expr )) syntax is defined in POSIX standard.

                  Exceptions

                  None.

                  More information

                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2220.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2220.html index 96ce446..736bef5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2220.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2220.html @@ -1,6 +1,6 @@

                  Invalid flags are not handled. Add a *) case.

                  Problematic code

                  -
                  #!/bin/sh
                  +
                  #!/bin/sh
                   while getopts "vr" f
                   do
                     case "$f" in
                  @@ -10,7 +10,7 @@ 

                  Problematic code

                  done

                  Correct code

                  -
                  #!/bin/sh
                  +
                  #!/bin/sh
                   while getopts "vr" f
                   do
                     case "$f" in
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2221.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2221.html
                  index d45870f..acf0472 100644
                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2221.html
                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2221.html
                  @@ -1,15 +1,15 @@
                   

                  This pattern always overrides a later one.

                  Problematic code

                  -
                  case "$1" in
                  +
                  case "$1" in
                     -?) echo "Usage: $0 [-n]";;
                     -n) echo "Hello World";;
                      *) exit 1;;
                   esac
                   

                  Correct code

                  -
                  case "$1" in
                  +
                  case "$1" in
                     -\?) echo "Usage: $0 [-n]";;
                  -# '-?') echo "Usage: $0 [-n]";;  # Also valid
                  +# '-?') echo "Usage: $0 [-n]";;  # Also valid
                     -n) echo "Hello World";;
                      *) exit 1;;
                   esac
                  @@ -19,12 +19,11 @@ 

                  Rationale

                  In the example, -? actually matches a dash followed by any character, such as -n. This means that the later -n branch will never trigger. In this case, the correct solution is to escape the -\? so that it doesn't match -n.

                  Another common reason for this is accidentally duplicating a branch. In this case, fix or delete the duplicate branch.

                  Exceptions

                  -

                  None. One could argue that having -*|--*) echo "Invalid flag"; is a readability issue, even though the second pattern follows from the first. In this case, you can either rearrange the pattern from most to least specific, i.e. --*|-*) or ignore the error.

                  +

                  None. One could argue that having -*|--*) echo "Invalid flag"; is a readability issue, even though the second pattern follows from the first. In this case, you can either rearrange the pattern from most to least specific, i.e. --*|-*) or ignore the error.

                  When ignoring this error, remember that ShellCheck directives have to go in front of the case statement, and not in front of the branch:

                  # shellcheck disable=SC2221,SC2222
                  -case "$1" in
                  +case "$1" in
                     -n) ...;;
                     # no directive here
                  -  -*|--*) echo "Unknown flag" ;;
                  -esac
                  -
                  + -*|--*) echo "Unknown flag" ;; +esac
                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2223.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2223.html index e2d7b6d..3454062 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2223.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2223.html @@ -1,9 +1,9 @@

                  This default assignment may cause DoS due to globbing. Quote it.

                  Problematic code

                  -
                  : ${COLUMNS:=80}
                  +
                  : ${COLUMNS:=80}
                   

                  Correct code

                  -
                  : "${COLUMNS:=80}"
                  +
                  : "${COLUMNS:=80}"
                   

                  Rationale

                  This statement is an idiomatic way of assigning a default value to an environment variable. However, even though it's passed to : which ignores arguments, it's better to quote it.

                  @@ -11,7 +11,6 @@

                  Rationale

                  The correct code uses double quotes to avoid glob expansion, and therefore does not have this problem.

                  When quoting, make sure to update any inner quotes:

                  : ${var:='foo'}    # Assigns foo without quotes
                  -: "${var:='foo'}"  # Assigns 'foo' with quotes
                  -
                  +: "${var:='foo'}" # Assigns 'foo' with quotes

                  Exceptions

                  None, though this issue is largely theoretical.

                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2224.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2224.html index 8538ba8..32a0507 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2224.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2224.html @@ -1,9 +1,9 @@ -

                  This mv has no destination. Check the arguments.

                  +

                  This mv has no destination. Check the arguments.

                  Problematic code

                  -
                  mv "$file $dir"
                  +
                  mv "$file $dir"
                   

                  Correct code

                  -
                  mv "$file" "$dir"
                  +
                  mv "$file" "$dir"
                   

                  Rationale

                  ShellCheck found an mv command with a single parameter. This may be because the source and destination was accidentally merged into a single argument, or because the line was broken in an invalid way.

                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2225.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2225.html index 9d2a325..c019c37 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2225.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2225.html @@ -1,9 +1,9 @@ -

                  This cp has no destination. Check the arguments.

                  +

                  This cp has no destination. Check the arguments.

                  Problematic code

                  -
                  cp "$file $dir"
                  +
                  cp "$file $dir"
                   

                  Correct code

                  -
                  cp "$file" "$dir"
                  +
                  cp "$file" "$dir"
                   

                  Rationale

                  ShellCheck found a cp command with a single parameter. This may be because the source and destination was accidentally merged into a single argument, or because the line was broken in an invalid way.

                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.html index 7fbff65..77cb576 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.html @@ -1,15 +1,15 @@ -

                  This ln has no destination. Check the arguments, or specify '.' explicitly.

                  +

                  This ln has no destination. Check the arguments, or specify . explicitly.

                  Problematic code

                  -
                  ln "$file $dir"
                  +
                  ln "$file $dir"
                   

                  or

                  -
                  ln /foo/bar/baz
                  +
                  ln /foo/bar/baz
                   

                  Correct code

                  -
                  ln "$file" "$dir"
                  +
                  ln "$file" "$dir"
                   

                  or

                  -
                  ln /foo/bar/baz .
                  +
                  ln /foo/bar/baz .
                   

                  Rationale

                  ShellCheck found a ln command with a single parameter. This may be because the source and destination was accidentally merged into a single argument, because the line was broken in an invalid way, or because you're using a non-standard invocation of ln that defaults to linking the argument into the current directory.

                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.json index c74ea41..6cedfd9 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2226.json @@ -1,5 +1,5 @@ { - "title": "This ln has no destination. Check the arguments, or specify '.' explicitly.", + "title": "This ln has no destination. Check the arguments, or specify . explicitly.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2227.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2227.html index f7e8e10..eabf1b4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2227.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2227.html @@ -1,9 +1,9 @@

                  Redirection applies to the find command itself. Rewrite to work per action (or move to end).

                  Problematic code

                  -
                  find . -name '*.ppm' -exec pnmtopng {} > {}.png \;
                  +
                  find . -name '*.ppm' -exec pnmtopng {} > {}.png \;
                   

                  Correct code

                  -
                  find . -name '*.ppm' -exec sh -c 'pnmtopng "$1" > "$1.png"' _ {} \;
                  +
                  find . -name '*.ppm' -exec sh -c 'pnmtopng "$1" > "$1.png"' _ {} \;
                   

                  Rationale

                  ShellCheck detected a find command with a redirection in the middle.

                  @@ -14,14 +14,12 @@

                  Rationale

                  # Is the same as this { find . -name '*.ppm' -exec pnmtopng {} \; -} > {}.png -
                  +} > {}.png

                  To perform a redirection per action, rewrite it with e.g. -exec sh -c '...' _ {} \;

                  Exceptions

                  If the redirection is something like > /dev/null where you don't mind it applying to the whole find and not individual results, you can move the redirection to the end of command to make it clear to ShellCheck (and humans) that it's not meant per command:

                  find . -exec foo {} > /dev/null \;     # Ambiguous syntax. Is it per -exec or not?
                  -find . -exec foo {} \;  > /dev/null    # Identical command with clear intent.
                  -
                  +find . -exec foo {} \; > /dev/null # Identical command with clear intent.

                  There is no difference in behavior between the two.

                  Related resources

                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.html index 6c6e431..e4676f3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.html @@ -1,23 +1,22 @@ -

                    This does not read 'foo'. Remove $/${} for that, or use ${var?} to quiet.

                    +

                    This does not read foo. Remove $/${} for that, or use ${var?} to quiet.

                    Problematic code

                    -
                    read $foo
                    +
                    read $foo
                     

                    Correct code

                    -
                    read foo
                    +
                    read foo
                     

                    Rationale

                    read takes a variable name, but shellcheck has noticed that you give it an expanded variable instead. This will populate whatever the variable expans to instead of the variable itself. For example:

                    foo=bar
                     read $foo   # Reads data into 'bar', not into 'foo'
                    -read foo    # Reads data into 'foo'
                    -
                    +read foo # Reads data into 'foo'

                    Exceptions

                    If this is intentional and you do want to read a variable through an indirect reference, you can silence this warning with a directive:

                    -
                    # shellcheck disable=SC2229
                    +
                    # shellcheck disable=SC2229
                     read "$foo"
                     

                    Or take advantage of the fact that ShellCheck only warns when no parameter expansion modifiers are applied:

                    -
                    read "${foo}"    # ShellCheck warns
                    +
                    read "${foo}"    # ShellCheck warns
                     read "${foo?}"   # No warning
                     

                    ${foo?} fails when foo is unset, which is fine since read would have failed too. The main side effect is an improved runtime error message in that case.

                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.json index b9b4c2c..41fcfc4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2229.json @@ -1,5 +1,5 @@ { - "title": "This does not read 'foo'. Remove $\/${} for that, or use ${var?} to quiet.", + "title": "This does not read foo. Remove $\/${} for that, or use ${var?} to quiet.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.html index eac71f8..abbddf5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.html @@ -1,10 +1,10 @@ -

                    which is non-standard. Use builtin 'command -v' instead.

                    -

                    Note: This check is opt-in only in ShellCheck 0.7.1+ (shellcheck -o deprecate-which).

                    +

                    which is non-standard. Use builtin command -v instead.

                    +

                    This is an [[optional]] suggestion. It must be explicitly enabled with a [[directive]] enable=deprecate-which in a # shellcheck comment or .shellcheckrc

                    Problematic code

                    -
                    which grep
                    +
                    which grep
                     

                    Correct code

                    -
                    # For the path of a single, unaliased, external command,
                    +
                    # For the path of a single, unaliased, external command,
                     # or to check whether this will just "run" in this shell:
                     command -v grep
                     # To check whether commands exist, without obtaining a reusable path:
                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.json
                    index 4474f22..eaffe76 100644
                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.json
                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2230.json
                    @@ -1,5 +1,5 @@
                     {
                    -  "title": "which is non-standard. Use builtin 'command -v' instead.",
                    +  "title": "which is non-standard. Use builtin command -v instead.",
                       "type": "CODE_SMELL",
                       "status": "ready",
                       "remediation": {
                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.html
                    index 5500c16..ce48b71 100644
                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.html
                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.html
                    @@ -1,21 +1,21 @@
                    -

                    Quote expansions in this for loop glob to prevent wordsplitting, e.g. "$dir"/*.txt .

                    +

                    Quote expansions in this for loop glob to prevent word splitting, e.g. "${dir}"/*.txt.

                    Problematic code

                    -
                    for file in $dir/*.txt
                    +
                    for file in ${dir}/*.txt
                     do
                    -  echo "Found $file"
                    +  echo "Found ${file}"
                     done
                     

                    Correct code

                    -
                    for file in "$dir"/*.txt
                    +
                    for file in "${dir}"/*.txt
                     do
                    -  echo "Found $file"
                    +  echo "Found ${file}"
                     done
                     

                    Rationale

                    When iterating over globs containing expansions, you can still quote all expansions in the path to better handle whitespace and special characters.

                    -

                    Just make sure glob characters are outside quotes. "$dir/*.txt" will not glob expand, but "$dir"/*.txt or "$dir"/*."$ext" will.

                    +

                    Just make sure glob characters are outside quotes. "${dir}/*.txt" will not glob expand, but "${dir}"/*.txt or "${dir}"/*."${ext}" will.

                    Exceptions

                    -

                    Exceptions similar to [[SC2086]] apply. If the variable is expected to contain globs, such as if dir="tmp/**" in the example, you can ignore this message.

                    +

                    Exceptions similar to [[SC2086]] apply. If the variable is expected to contain globs, such as if dir="tmp/**" in the example, you can ignore this message.

                    Related resources

                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                    • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.json index 528638e..7457346 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2231.json @@ -1,5 +1,5 @@ { - "title": "Quote expansions in this for loop glob to prevent wordsplitting, e.g. '$dir'\/*.txt .", + "title": "Quote expansions in this for loop glob to prevent word splitting, e.g. "${dir}"\/*.txt.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2232.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2232.html index c6043f4..1d0f8c0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2232.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2232.html @@ -1,18 +1,17 @@ -

                      Can't use sudo with builtins like cd. Did you want sudo sh -c .. instead?

                      +

                      Can't use sudo with builtins like cd. Did you want sudo sh -c .. instead?

                      Problematic code

                      -
                      sudo cd /root
                      +
                      sudo cd /root
                       pwd
                       

                      Correct code

                      -
                      sudo sh -c 'cd /root && pwd'
                      +
                      sudo sh -c 'cd /root && pwd'
                       

                      Rationale

                      Due to the Unix process model, sudo can only change the privileges of a new, external process. It can not grant privileges to a currently running process.

                      This means that shell builtins -- commands that are interpreted by the current shell rather than through program invocation -- cannot be run with sudo. This includes cd, source, read, and others.

                      Instead you can run a shell with sudo, and have that shell run the builtins you want. Just be aware that what happens in that shell stays in that shell:

                      sudo sh -c 'cd /root && pwd'  # This shows /root
                      -pwd                           # This shows the original directory
                      -
                      +pwd # This shows the original directory

                      Exceptions

                      None.

                      Related resources

                      diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2233.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2233.html index 6f231ae..ba76a10 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2233.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2233.html @@ -1,21 +1,21 @@

                      Remove superfluous (..) around condition to avoid subshell overhead.

                      Problematic code

                      -
                      if ([ "$x" -gt 0 ])
                      +
                      if ([ "$x" -gt 0 ])
                       then true; fi
                       

                      Correct code

                      -
                      if [ "$x" -gt 0 ]
                      +
                      if [ "$x" -gt 0 ]
                       then true; fi
                       

                      Rationale

                      The shell syntax is if cmd, elif cmd, while cmd and until cmd without any parentheses. Instead, parentheses are an independent construct used to create subshells.

                      ShellCheck has noticed that you're wrapping (..) around one or more test commands. This is unnecessary, and the resulting fork adds quite a lot of overhead:

                      -
                      $ i=0; time while ( [ "$i" -lt 10000 ] ); do i=$((i+1)); done
                      +
                      $ i=0; time while ( [ "$i" -lt 10000 ] ); do i=$((i+1)); done
                       real    0m6.998s
                       user    0m3.453s
                       sys     0m3.464s
                       
                      -$ i=0; time while [ "$i" -lt 10000 ]; do i=$((i+1)); done
                      +$ i=0; time while [ "$i" -lt 10000 ]; do i=$((i+1)); done
                       real    0m0.055s
                       user    0m0.054s
                       sys     0m0.001s
                      @@ -23,7 +23,7 @@ 

                      Rationale

                      Just delete the surrounding (..) since they serve no purpose and only slows the script down.

                      Exceptions

                      This issue only affects performance, not correctness, so it can be safely [[ignore]]d.

                      -

                      If you are considering doing it to stylistically match C-like languages, please note that this is not conventional and that you'd probably recommend someone use if (1 == 2) over if (system("[ 1 = 2 ]")) in C no matter which language they're used to.

                      +

                      If you are considering doing it to stylistically match C-like languages, please note that this is not conventional and that you'd probably recommend someone use if (1 == 2) over if (system("[ 1 = 2 ]")) in C no matter which language they're used to.

                      Related resources

                      • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                      • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2234.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2234.html index 3edfa7b..c80927b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2234.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2234.html @@ -1,18 +1,18 @@

                        Remove superfluous (..) around test command to avoid subshell overhead.

                        Problematic code

                        -
                        ([ "$x" -gt 0 ]) && foo
                        +
                        ([ "$x" -gt 0 ]) && foo
                         

                        Correct code

                        -
                        [ "$x" -gt 0 ] && foo
                        +
                        [ "$x" -gt 0 ] && foo
                         

                        Rationale

                        You are wrapping a single test command in (..), creating an unnecessary subshell. This serves no purpose, but is significantly slower:

                        -
                        $ i=0; time while ( [ "$i" -lt 10000 ] ); do i=$((i+1)); done
                        +
                        $ i=0; time while ( [ "$i" -lt 10000 ] ); do i=$((i+1)); done
                         real    0m6.998s
                         user    0m3.453s
                         sys     0m3.464s
                         
                        -$ i=0; time while [ "$i" -lt 10000 ]; do i=$((i+1)); done
                        +$ i=0; time while [ "$i" -lt 10000 ]; do i=$((i+1)); done
                         real    0m0.055s
                         user    0m0.054s
                         sys     0m0.001s
                        diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2235.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2235.html
                        index b0e4a79..59aa4ae 100644
                        --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2235.html
                        +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2235.html
                        @@ -1,21 +1,21 @@
                         

                        Use { ..; } instead of (..) to avoid subshell overhead.

                        Problematic code

                        -
                        ([ "$x" ] || [ "$y" ]) && [ "$z" ]
                        +
                        ([ "$x" ] || [ "$y" ]) && [ "$z" ]
                         

                        Correct code

                        -
                        { [ "$x" ] || [ "$y" ]; } && [ "$z" ]
                        +
                        { [ "$x" ] || [ "$y" ]; } && [ "$z" ]
                         

                        Rationale

                        You appear to be using (..) to group test commands. This creates a subshell, making it unnecessarily slow. Avoid this by using { ..; } to group.

                        Be careful to note that unlike (..), this requires both a space after the { and a semicolon before the }.

                        For example, (cmd), (cmd;) and ( cmd ) are all valid, but {cmd}, {cmd;} and { cmd } are all syntax errors because they lack either or both of the spaces and semicolon. The correct form is { cmd; }

                        Here's a small benchmark showing that the subshell version is more than 100x slower:

                        -
                        $ i=0; time for i in {1..10000}; do ([ "$x" ] || [ "$y" ]) && [ "$z" ]; done
                        +
                        $ i=0; time for i in {1..10000}; do ([ "$x" ] || [ "$y" ]) && [ "$z" ]; done
                         real    0m7.122s
                         user    0m4.204s
                         sys     0m2.825s
                         
                        -$ i=0; time for i in {1..10000}; do { [ "$x" ] || [ "$y" ]; } && [ "$z" ]; done
                        +$ i=0; time for i in {1..10000}; do { [ "$x" ] || [ "$y" ]; } && [ "$z" ]; done
                         real    0m0.055s
                         user    0m0.055s
                         sys     0m0.000s
                        diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2236.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2236.html
                        index 613d51b..b20c247 100644
                        --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2236.html
                        +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2236.html
                        @@ -1,11 +1,11 @@
                         

                        Use -n instead of ! -z.

                        -

                        (or "Use -z instead of ! -n")

                        +

                        (or "Use -z instead of ! -n")

                        Problematic code

                        -
                        if [ ! -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                        +
                        if [ ! -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                         if [ ! -z "$STY" ];       then echo "You are already running screen"; fi
                         

                        Correct code

                        -
                        if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                        +
                        if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                         if [ -n "$STY" ];       then echo "You are already running screen"; fi
                         

                        Rationale

                        @@ -16,12 +16,12 @@

                        Rationale

                        # Identical tests to verify that a value is empty [ ! -n foo ] # Not is non-empty -[ -z foo ] # Is empty -
                        +[ -z foo ] # Is empty

                        Exceptions

                        This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can [[Ignore]] it with a directive or flag.

                        Related resources

                        • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                        • Bash reference manual for -z and -n.
                        • +
                        • Note: Be careful with quoting variables (which you should always do anyway): [ ! -z $var ] might work, but [ -n $var] will not. [ -n "$var" ] will do what you expect.
                        diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2237.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2237.html index 3688c7e..b7d1e8c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2237.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2237.html @@ -1,11 +1,11 @@

                        Use [ -n .. ] instead of ! [ -z .. ].

                        -

                        (or "Use [ -z .. ] instead of ! [ -n .. ].)

                        +

                        (or "Use [ -z .. ] instead of ! [ -n .. ].)

                        Problematic code

                        -
                        if ! [ -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                        +
                        if ! [ -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                         if ! [ -z "$STY" ];       then echo "You are already running screen"; fi
                         

                        Correct code

                        -
                        if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                        +
                        if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
                         if [ -n "$STY" ];       then echo "You are already running screen"; fi
                         

                        Rationale

                        @@ -16,8 +16,7 @@

                        Rationale

                        # Identical tests to verify that a value is empty ! [ -n foo ] # Not is non-empty -[ -z foo ] # Is empty -
                        +[ -z foo ] # Is empty

                        Exceptions

                        This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can't not [[Ignore]] it with a directive or flag.

                        Related resources

                        diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2238.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2238.html index 039fca4..8314312 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2238.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2238.html @@ -1,11 +1,11 @@

                        Redirecting to/from command name instead of file. Did you want pipes/xargs (or quote to ignore)?

                        Problematic code

                        -
                        cat file > tr -d '\r'
                        +
                        cat file > tr -d '\r'
                         cat file > rm
                         

                        Correct code

                        -
                        cat file | tr -d '\r'         # tr reads stdin
                        -cat file | xargs -d '\n' rm   # rm reads arguments
                        +
                        cat file | tr -d '\r'         # tr reads stdin
                        +cat file | xargs -d '\n' rm   # rm reads arguments
                         

                        Rationale

                        You are using file redirection, but the filename is an unquoted command name. Instead of running the command and feeding data to it, this just writes to a file with the same name.

                        @@ -17,8 +17,8 @@

                        Rationale

                        Note that xargs has many pitfalls when it comes to spaces and quotes. cat file | xargs rm will appear to work during testing, but fails for filenames like My File.txt or Can't_Fight_This_Feeling.mp3. The example uses the GNU extension -d '\n' to more safely handle these names.

                        Exceptions

                        If you actually did want to write a file named after a command, simply quote the filename to let ShellCheck know you meant it literally and not as a command name. This does not change anything about how the script works:

                        -
                        # Write to a file literally named 'rm', does not try to delete anything
                        -echo "A potentially dangerous command" > "rm"
                        +
                        # Write to a file literally named 'rm', does not try to delete anything
                        +echo "A potentially dangerous command" > "rm" 
                         

                        Related resources

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2239.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2239.html index a5f174c..03cb4d5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2239.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2239.html @@ -1,17 +1,17 @@

                          Ensure the shebang uses the absolute path to the interpreter.

                          Problematic code

                          -
                          #!bin/sh
                          +
                          #!bin/sh
                           echo "Hello World"
                           

                          Correct code

                          -
                          #!/bin/sh
                          +
                          #!/bin/sh
                           echo "Hello World"
                           

                          Rationale

                          The script's interpreter, as specified in the shebang, does not start with a /.

                          The interpreter should always be specified by absolute path to ensure that the script can be executed from any directory. When it's not, it's generally a typo like in the problematic example.

                          If you don't know where the interpreter is and you hoped to use #! bash, this is not an option. Use /usr/bin/env instead:

                          -
                          #!/usr/bin/env bash
                          +
                          #!/usr/bin/env bash
                           echo "Hello World"
                           

                          While not required by POSIX, env can essentially always be found in /usr/bin and will search the PATH for the specified executable.

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2240.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2240.html index d107d89..afe70d8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2240.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2240.html @@ -1,11 +1,11 @@

                          The dot command does not support arguments in sh/dash. Set them as variables.

                          Problematic code

                          -
                          #!/bin/sh
                          +
                          #!/bin/sh
                           . include/myscript example.com 80
                           

                          Correct code

                          -
                          #!/bin/sh
                          -host=example.com port=80 . include/myscript
                          +
                          #!/bin/sh
                          +host=example.com port=80 . include/myscript 
                           

                          Rationale

                          In Bash and Ksh, you can use . myscript arg1 arg2.. to set $1 and $2 in the sourced script.

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2241.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2241.html index a169010..adf2223 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2241.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2241.html @@ -1,9 +1,9 @@

                          The exit status can only be one integer 0-255. Use stdout for other data.

                          Problematic code

                          -
                          exit foo bar
                          +
                          exit foo bar
                           

                          Correct code

                          -
                          echo foo
                          +
                          echo foo
                           echo bar
                           exit
                           
                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2242.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2242.html index 2c5c9a3..5e73d91 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2242.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2242.html @@ -1,18 +1,18 @@

                          Can only exit with status 0-255. Other data should be written to stdout/stderr.

                          Problematic code

                          -
                          exit "Bad filename"
                          +
                          exit "Bad filename"
                           

                          Correct code

                          -
                          echo "Bad filename" >&2
                          +
                          echo "Bad filename" >&2
                           exit 1
                           

                          Rationale

                          exit can only be used to signal success or failure (0 = success, 1-255 = failure). It can not be used to return string data, and it can not be used to print error messages.

                          String data should be written stdout, before an exit 0 to exit with success.

                          Errors should instead be written to stderr, with an exit 1 (or higher) to exit with failure:

                          -
                          if [ ! -f "$1" ]
                          +
                          if [ ! -f "$1" ]
                           then
                          -   echo "$1 is not a regular file" >&2
                          +   echo "$1 is not a regular file" >&2
                              exit 1
                           fi
                           
                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.html index c1d8f74..1eb3a6d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.html @@ -1,12 +1,12 @@ -

                          Prefer explicit -n to check for output (or run command without [/[[ to check for success)

                          +

                          Prefer explicit -n to check for output (or run command without [/[[ to check for success)

                          Problematic code

                          -
                          if [ "$(mycommand --myflags)" ]
                          +
                          if [ "$(mycommand --myflags)" ]
                           then
                             echo "True"
                           fi
                           

                          Correct code

                          -
                          # Check that the command outputs something on stdout
                          +
                          # Check that the command outputs something on stdout
                           if [ -n "$(mycommand --myflags)" ]
                           then
                             echo "The command had output on stdout"
                          @@ -18,9 +18,9 @@ 

                          Correct code

                          echo "The command reported success" fi
                          -

                          (if the command instead outputs "0" or "false", see [[SC2244]] for integer and "boolean" comparisons)

                          +

                          (if the command instead outputs "0" or "false", see [[SC2244]] for integer and "boolean" comparisons)

                          Rationale

                          -

                          [ "$(mycommand)" ] is equivalent to [ -n "$(mycommand)" ] and checks whether the command's output on stdout was non-empty.

                          +

                          [ "$(mycommand)" ] is equivalent to [ -n "$(mycommand)" ] and checks whether the command's output on stdout was non-empty.

                          Users more familiar with other languages are often surprised to learn that it is nothing like e.g. if (myfunction()), since it does not care about what the command/function returns.

                          Using an explicit -n helps clarify that this is purely a string operation. And of course, if the intention was to check whether the command ran successfully, now would be a good time to fix it as in the alternate example.

                          Exceptions

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.json index a5028a9..fec3609 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2243.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.html index f5471ed..ca9adfa 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.html @@ -1,12 +1,12 @@ -

                          Prefer explicit -n to check non-empty string (or use =/-ne to check boolean/integer).

                          +

                          Prefer explicit -n to check non-empty string (or use =/-ne to check boolean/integer).

                          Problematic code

                          -
                          if [ "$1" ]
                          +
                          if [ "$1" ]
                           then
                             echo "True"
                           fi
                           

                          Correct code

                          -
                          # Check if $1 is empty or non-empty
                          +
                          # Check if $1 is empty or non-empty
                           if [ -n "$1" ]
                           then
                             echo "True, $1 is a non-empty value"
                          @@ -20,17 +20,18 @@ 

                          Correct code

                          # Check instead if $1 is defined (even if just assigned the empty string) or undefined [ "${1+x}" = "x" ] +

                          Rationale

                          -

                          [ "$var" ] is equivalent to [ -n "$var" ] and checks that a string is non-empty.

                          -

                          Users more familiar with other languages are often surprised to learn that [ "$var" ] is true when:

                          +

                          [ "$var" ] is equivalent to [ -n "$var" ] and checks that a string is non-empty.

                          +

                          Users more familiar with other languages are often surprised to learn that [ "$var" ] is true when:

                          • var=false
                          • var=0
                          • var=null
                          • -
                          • var=" "
                          • +
                          • var=" "
                          -

                          Adding the explicit -n helps clarify that this is a string comparison, and not related to any concept of boolean values or "truthiness" as it is in most languages.

                          +

                          Adding the explicit -n helps clarify that this is a string comparison, and not related to any concept of boolean values or "truthiness" as it is in most languages.

                          Exceptions

                          If you are familiar with the semantics of [, you can [[ignore]] this stylistic suggestion with no ill effects.

                          Related resources

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.json index d3f4876..de1bfee 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2244.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2245.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2245.html index 2c8c1f9..164e150 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2245.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2245.html @@ -1,12 +1,16 @@

                          -d only applies to the first expansion of this glob. Use a loop to check any/all.

                          Problematic code

                          -
                          if [ -f ksh* ]
                          +
                          #!/bin/ksh
                          +
                          +if [ -f ksh* ]
                           then
                             echo "The file exists"
                           fi
                           

                          Correct code

                          -
                          for f in ksh*
                          +
                          #!/bin/ksh
                          +
                          +for f in ksh*
                           do
                             if [ -f "$f" ]
                             then
                          @@ -16,18 +20,17 @@ 

                          Correct code

                          Rationale

                          Ksh has the curious behavior of ignoring anything after an unrecognized flag to test/[, which means that file checking operators against globs will effectively apply the operator to the first expansion:

                          -
                          [ -f ksh* ]                              # This
                          +
                          [ -f ksh* ]                              # This
                           [ -f ksh93u ksh93u.tar ksh93u.tar.gz ]   # Becomes this
                           [ -f ksh93u ]                            # And is interpreted like this
                           

                          This is an issue when you have multiple matches for a glob. Instead of checking some or all, it only checks the first result and ignores the rest. To ensure that all results are considered (either to check that any or all results match the operator), use a loop explicitly.

                          If you really only want to match the first result of the glob expansion as sorted alphabetically in the current locale, you can make this intention explicit:

                          matches=( ksh* )
                          -if [ -f "${matches[0]}" ]
                          +if [ -f "${matches[0]}" ]
                           then
                          -  echo "The first result is a file"
                          -fi
                          -
                          + echo "The first result is a file" +fi

                          Exceptions

                          If you only care that entries exists, use -e. ShellCheck does not warn in this case, since all files resulting from glob expansion necessarily exist.

                          Related resources

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2246.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2246.html index 5eb26c7..2226a7f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2246.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2246.html @@ -1,10 +1,10 @@

                          This shebang specifies a directory. Ensure the interpreter is a file.

                          Problematic code

                          -
                          #!/bin/sh/
                          +
                          #!/bin/sh/
                           echo "Hello World"
                           

                          Correct code

                          -
                          #!/bin/sh
                          +
                          #!/bin/sh
                           echo "Hello World"
                           

                          Rationale

                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.html index 5954779..a0e5e75 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.html @@ -1,18 +1,17 @@ -

                          Flip leading $ and " if this should be a quoted substitution.

                          +

                          Flip leading $ and " if this should be a quoted substitution.

                          Problematic code

                          -
                          var=$"(whoami)"
                          +
                          var=$"(whoami)"
                           

                          Correct code

                          -
                          var="$(whoami)"
                          +
                          var="$(whoami)"
                           

                          Rationale

                          -

                          ShellCheck has found a $"( or $"{ . This is most likely due to flipping the dollar-sign and double quote:

                          -
                          echo $"(cmd)"  # Supposed to be "$(cmd)"
                          -echo $"{var}"  # Supposed to be "${var}"
                          -
                          -

                          Instead of quoted substitutions, these will be interpreted as localized string resources ($"..") containing literal parentheses or curly braces. If this was not intentional, you should flip the " and $ like in the example.

                          +

                          ShellCheck has found a $"( or $"{ . This is most likely due to flipping the dollar-sign and double quote:

                          +
                          echo $"(cmd)"  # Supposed to be "$(cmd)"
                          +echo $"{var}"  # Supposed to be "${var}"
                          +

                          Instead of quoted substitutions, these will be interpreted as localized string resources ($"..") containing literal parentheses or curly braces. If this was not intentional, you should flip the " and $ like in the example.

                          Exceptions

                          -

                          If you intentionally wanted a localized string literal $".." that starts with ( or {, either [[ignore]] this error or start it with a different character.

                          +

                          If you intentionally wanted a localized string literal $".." that starts with ( or {, either [[ignore]] this error or start it with a different character.

                          Related resources

                          • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                          • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.json index 0fc96c2..665b8ca 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2247.json @@ -1,5 +1,5 @@ { - "title": "Flip leading $ and ' if this should be a quoted substitution.", + "title": "Flip leading $ and " if this should be a quoted substitution.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.html index 4206e42..25f3c17 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.html @@ -1,12 +1,12 @@ -

                            Warn about variable references without braces.

                            -

                            This error is optional and not enabled by default.

                            +

                            Prefer double quoting even when variables don't contain special characters.

                            +

                            This is an [[optional]] suggestion. It must be explicitly enabled with a [[directive]] enable=quote-safe-variables in a # shellcheck comment or .shellcheckrc

                            Problematic code:

                            -
                            subdir='example'
                            +
                            subdir='example'
                             
                             cd ${subdir}
                             

                            Correct code:

                            -
                            subdir='example'
                            +
                            subdir='example'
                             
                             cd "${subdir}"
                             
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.json index b019243..291c179 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2248.json @@ -1,5 +1,5 @@ { - "title": "Warn about variable references without braces.", + "title": "Prefer double quoting even when variables don't contain special characters.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2249.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2249.html index e596c57..c16bd57 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2249.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2249.html @@ -1,6 +1,6 @@ -

                            Consider adding a default *) case, even if it just exits with error.

                            +

                            Consider adding a default *) case, even if it just exits with error.

                            Problematic code

                            -
                            case "$1" in
                            +
                            case "$1" in
                               start) start_service ;;
                               stop)  stop_service ;;
                               restart|reload|force-reload)
                            @@ -9,7 +9,7 @@ 

                            Problematic code

                            esac

                            Correct code

                            -
                            case "$1" in
                            +
                            case "$1" in
                               start) start_service ;;
                               stop)  stop_service ;;
                               restart|reload|force-reload)
                            @@ -30,17 +30,17 @@ 

                            Rationale

                            Exceptions

                            This suggestion only triggers in verbose mode (-S verbose).

                            If you don't have a default case because the default should be to take no action, consider adding a comment to other humans:

                            -
                            case "$(uname)" in
                            +
                            case "$(uname)" in
                               CYGWIN*) cygwin=1;;
                               MINGW*) mingw=1;;
                               *) ;; # No special workarounds identified
                             esac
                             

                            If you believe that it's impossible for the expression to have any other value, it's considered good practice to add the equivalent of an assert(0) to fail fast if this assumption should turn out to be incorrect in the current or future versions:

                            -
                            case "$result" in
                            +
                            case "$result" in
                               true)  proceed;;
                               false) cancel;;
                            -  *) echo >&2 "Submit bug report: '$result' should be true or false."
                            +  *) echo >&2 "Submit bug report: '$result' should be true or false."
                                  exit 127
                             esac
                             
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.html index b6efc43..e1b369a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.html @@ -1,12 +1,12 @@ -

                            Prefer putting braces around variable references even when not strictly required.

                            -

                            This error is optional and not enabled by default.

                            +

                            Prefer putting braces around variable references even when not strictly required.

                            +

                            This is an [[optional]] suggestion. It must be explicitly enabled with a [[directive]] enable=require-variable-braces in a # shellcheck comment or .shellcheckrc

                            Problematic code:

                            -
                            partial_path='example'
                            +
                            partial_path='example'
                             
                             curl "http://example.com/$partial_path_version/explain.html"
                             

                            Correct code:

                            -
                            partial_path='example'
                            +
                            partial_path='example'
                             
                             curl "http://example.com/${partial_path}_version/explain.html"
                             
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.json index 6a54e7e..90eae51 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2250.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.html index bad109e..9d402cf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.html @@ -1,16 +1,17 @@ -

                            This ! is not on a condition and skips errexit. Use && exit 1 instead, or make sure $? is checked.

                            +

                            This ! is not on a condition and skips errexit. Add || exit 1 or make sure $? is checked.

                            Problematic code

                            -
                            set -e
                            +
                            set -e
                             ! false
                            +rest
                             

                            Correct code

                            -
                            set -e
                            -false && exit 1
                            +
                            set -e
                            +! false || exit 1
                             

                            Rationale

                            ShellCheck has found a command inverted with ! that may have no effect. In particular, it does not appear as a condition in an if statement or while loop, or as the final command in a script or function.

                            The most common reason for this is thinking that it'll trigger set -e aka errexit if a command succeeds, as in the example. This is not the case: ! will inhibit errexit both on success and failure of the inverted command.

                            -

                            Using && exit will instead exit when failure when the command succeeds.

                            +

                            Adding || exit will instead exit with failure when the command succeeds.

                            Exceptions

                            ShellCheck will not detect cases where $? is implicitly or explicitly used to check the value afterwards:

                            set -e;
                            @@ -22,4 +23,5 @@ 

                            Exceptions

                            Related resources

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.json index e7bddcc..f3f736c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2251.json @@ -1,5 +1,5 @@ { - "title": "This ! is not on a condition and skips errexit. Use && exit 1 instead, or make sure $? is checked.", + "title": "This ! is not on a condition and skips errexit. Add || exit 1 or make sure $? is checked.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2252.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2252.html index 2291711..0823fc1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2252.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2252.html @@ -1,19 +1,19 @@ -

                            You probably wanted && here, otherwise it's always true.

                            +

                            You probably wanted && here, otherwise it's always true.

                            Problematic code

                            -
                            if [ "$1" != foo ] || [ "$1" != bar ]
                            +
                            if [ "$1" != foo ] || [ "$1" != bar ]
                             then
                               echo "$1 is not foo or bar"
                             fi
                             

                            Correct code

                            -
                            if [ "$1" != foo ] && [ "$1" != bar ]
                            +
                            if [ "$1" != foo ] && [ "$1" != bar ]
                             then
                               echo "$1 is not foo or bar"
                             fi
                             

                            Rationale

                            This is not a bash issue, but a simple, common logical mistake applicable to all languages.

                            -

                            [ "$1" != foo ] || [ "$1" != bar ] is always true (when foo != bar):

                            +

                            [ "$1" != foo ] || [ "$1" != bar ] is always true (when foo != bar):

                            • If $1 = foo then $1 != bar is true, so the statement is true.
                            • If $1 = bar then $1 != foo is true, so the statement is true.
                            • @@ -25,7 +25,7 @@

                              Rationale

                            • If $1 = bar, then $1 != bar is false, so the statement is false.
                            • If $1 = cow, then both $1 != foo and $1 != bar is true, so the statement is true.
                            -

                            This statement is identical to ! [ "$1" = foo ] || [ "$1" = bar ], which also works correctly (by De Morgan's law)

                            +

                            This statement is identical to ! [ "$1" = foo ] || [ "$1" = bar ], which also works correctly (by De Morgan's law)

                            This warning is equivalent to [[SC2055]] and [[SC2056]], which trigger for intra-test expressions and arithmetic contexts respectively.

                            Exceptions

                            Rare.

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2253.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2253.html index d07e63e..0924009 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2253.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2253.html @@ -1,10 +1,10 @@ -

                            Use -R to recurse, or explicitly a-r to remove read permissions.

                            +

                            Use -R to recurse, or explicitly a-r to remove read permissions.

                            Problematic code

                            -
                            chmod -r 0700 dir
                            +
                            chmod -r 0700 dir
                             chmod -r file
                             

                            Correct code

                            -
                            chmod -R 0700 dir
                            +
                            chmod -R 0700 dir
                             chmod a-r file
                             

                            Rationale

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2254.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2254.html index d71228f..5c2bc99 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2254.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2254.html @@ -1,12 +1,12 @@

                            Quote expansions in case patterns to match literally rather than as a glob.

                            Problematic code

                            -
                            case $input in
                            +
                            case $input in
                               -       ) echo "Reading from stdin..." ;;
                               $output ) echo "Input should be different from output" ;;
                             esac
                             

                            Correct code

                            -
                            case $input in
                            +
                            case $input in
                               -         ) echo "Reading from stdin..." ;;
                               "$output" ) echo "Input should be different from output" ;;
                             esac
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2255.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2255.html
                            index 7b1d89f..0548751 100644
                            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2255.html
                            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2255.html
                            @@ -1,9 +1,9 @@
                             

                            [ ] does not apply arithmetic evaluation. Evaluate with $((..)) for numbers, or use string comparator for strings.

                            Problematic code

                            -
                            [ 2*3 -eq array[i] ]
                            +
                            [ 2*3 -eq array[i] ]
                             

                            Correct code

                            -
                            [ $((2*3)) -eq $((array[i])) ]
                            +
                            [ $((2*3)) -eq $((array[i])) ]
                             

                            Rationale

                            When using [[ .. ]] with numerical comparators (-eq, -lt, etc), the value on either side will be evaluated as an arithmetic expression. This means that 2*3 will be evaluated to 6, and x will be evaluated to the contents of the variable $x.

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.html index 185d102..605fba5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.html @@ -1,15 +1,15 @@ -

                            This translated string is the name of a variable. Flip leading $ and " if this should be a quoted substitution.

                            +

                            This translated string is the name of a variable. Flip leading $ and " if this should be a quoted substitution.

                            Problematic code

                            -
                            var="foo"
                            +
                            var="foo"
                             echo $"var"
                             

                            Correct code

                            -
                            var="foo"
                            +
                            var="foo"
                             echo "$var"
                             

                            Rationale

                            -

                            $".." is a localized string, for example, echo $"Hello $USER" along with the proper translation files can be used to have the script say "Bonjour, youruser" in French locales.

                            -

                            In this case, ShellCheck found a localized string whose contents is also the name of a variable. This could have happened because the user wanted a far more common quoted substitution, e.g. "$var", but accidentally switched the leading $ and ".

                            +

                            $".." is a localized string, for example, echo $"Hello $USER" along with the proper translation files can be used to have the script say "Bonjour, youruser" in French locales.

                            +

                            In this case, ShellCheck found a localized string whose contents is also the name of a variable. This could have happened because the user wanted a far more common quoted substitution, e.g. "$var", but accidentally switched the leading $ and ".

                            Exceptions

                            If you do want a localized string whose contents is also an active variable, you can [[ignore]] this warning or rename the variable.

                            Related resources

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.json index 4eeb5b6..d7f8fe8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2256.json @@ -1,5 +1,5 @@ { - "title": "This translated string is the name of a variable. Flip leading $ and ' if this should be a quoted substitution.", + "title": "This translated string is the name of a variable. Flip leading $ and " if this should be a quoted substitution.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2257.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2257.html index 711408d..62b7ac2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2257.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2257.html @@ -1,15 +1,15 @@

                            Arithmetic modifications in command redirections may be discarded. Do them separately.

                            Problematic code

                            -
                            curl "$URL" > "image$((i++)).jpg"
                            +
                            curl "$URL" > "image$((i++)).jpg"
                             

                            Correct code

                            -
                            i=$((i+1))
                            +
                            i=$((i+1))
                             curl "$URL" > "image$i.jpg"
                             

                            Rationale

                            You are using an arithmetic expression that modifies a variable, e.g. $((x+=1)) or $((x++)), in the name of a file to redirect from/to, in a here document, or in a here string.

                            The scope of these modifications depends on whether the command itself will fork:

                            -
                            echo foo > $((var++)).txt  # Updates in BusyBox and Bash
                            +
                            echo foo > $((var++)).txt  # Updates in BusyBox and Bash
                             cat  foo > $((var++)).txt  # Updates in Busybox, not in Bash
                             gcc  foo > $((var++)).txt  # Does not update in either
                             
                            @@ -22,5 +22,5 @@ 

                            Exceptions

                            Related resources

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2258.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2258.html index 19a80fe..48a5d81 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2258.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2258.html @@ -1 +1,27 @@ -

                            The trailing comma is part of the value, not a separator. Delete or quote it.

                            \ No newline at end of file +

                            The trailing comma is part of the value, not a separator. Delete or quote it.

                            +

                            Problematic code

                            +
                            for f in foo, bar, baz
                            +do
                            +  echo "$f"
                            +done
                            +
                            +

                            Correct code

                            +
                            for f in foo bar baz
                            +do
                            +  echo "$f"
                            +done
                            +
                            +

                            or

                            +
                            for f in "foo," "bar," "baz,"
                            +do
                            +  echo "$f"
                            +done
                            +
                            +

                            Rationale

                            +

                            ShellCheck found a for loop where the items appear to be separated by commas. These will be treated as literal commas. If the commas are part of the value, enclose them in quotes, or remove them.

                            +

                            Exceptions

                            +

                            None

                            +

                            Related resources

                            +
                              +
                            • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                            • +
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2259.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2259.html index 7da76e3..172006d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2259.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2259.html @@ -1,9 +1,9 @@

                            This redirection overrides piped input. To use both, merge or pass filenames.

                            Problematic code

                            -
                            gzcat yesterday.log.gz | grep "$USER" < today.log
                            +
                            gzcat yesterday.log.gz | grep "$USER" < today.log
                             

                            Correct code

                            -
                            # Specify non-piped inputs as filenames
                            +
                            # Specify non-piped inputs as filenames
                             gzcat yesterday.log.gz | grep "$USER" - today.log
                             
                             # Or merge multiple inputs into a single stream
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.html
                            index ff80933..5bbc72f 100644
                            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.html
                            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.html
                            @@ -1,9 +1,9 @@
                            -

                            This redirection overrides the output pipe. Use 'tee' to output to both.

                            +

                            This redirection overrides the output pipe. Use tee to output to both.

                            Problematic code

                            -
                            env > environment.txt | grep '^ANDROID'
                            +
                            env > environment.txt | grep '^ANDROID'
                             

                            Correct code

                            -
                            env | tee environment.txt | grep '^ANDROID'
                            +
                            env | tee environment.txt | grep '^ANDROID'
                             

                            Rationale

                            A process only has a single standard output stream. Pipes and output redirections both overwrite it, so you can't use both at the same time. If you try, the redirection takes precedence and the output pipe is closed.

                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.json index 9c0e329..cc8241b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2260.json @@ -1,5 +1,5 @@ { - "title": "This redirection overrides the output pipe. Use 'tee' to output to both.", + "title": "This redirection overrides the output pipe. Use tee to output to both.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2261.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2261.html index 8e930c7..f8ad6c7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2261.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2261.html @@ -1,10 +1,10 @@ -

                            Multiple redirections compete for stdout. Use cat, tee, or pass filenames instead.

                            +

                            Multiple redirections compete for stdout. Use cat, tee, or pass filenames instead.

                            (or stdin, or stderr, or FD 3)

                            Problematic code

                            -
                            grep foo < input1 < input2 > output1 > output2 > output3
                            +
                            grep foo < input1 < input2 > output1 > output2 > output3
                             

                            Correct code

                            -
                            # Merge inputs into a single stream, write outputs individually
                            +
                            # Merge inputs into a single stream, write outputs individually
                             cat input1 input2 | grep foo | tee output1 output2 > output3
                             
                             # Pass inputs as filenames, write outputs individually
                            diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2262.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2262.html
                            index d5fe151..e7ef730 100644
                            --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2262.html
                            +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2262.html
                            @@ -1,12 +1,12 @@
                             

                            This alias can't be defined and used in the same parsing unit. Use a function instead.

                            Problematic code

                            -
                            function checksum() {
                            +
                            function checksum() {
                               type md5 && alias md5sum=md5
                               md5sum "$@"  # This calls `md5sum`, not `md5`
                             }
                             

                            Correct code

                            -
                            function checksum() {
                            +
                            function checksum() {
                               type md5 && md5sum() { md5 "$@"; }
                               md5sum "$@"  # Now this would call `md5` when applicable
                             }
                            @@ -34,8 +34,7 @@ 

                            Rationale

                            unit 4 }; { unit 4 -} -
                            +}

                            Any alias defined in a command in unit 1 would not take effect until unit 2 and beyond. Similarly, an alias defined in unit 2 will only take effect in unit 3 and 4.

                            In the problematic example, the alias is defined and used in a function. Since a function definition is a single compound command, it's considered a single parsing unit. The alias would therefore not have an effect (this is true even if the function is invoked twice, because it's only parsed once).

                            Does this sound confusing and counter-intuitive? It is. Save yourself the trouble and always use functions instead of aliases.

                            @@ -49,8 +48,7 @@

                            Exceptions

                            alias foo=bar # With either Option A or B, this SC2263 message is auto-suppressed foo -fi -
                            +fi

                            Related resources

                            • Unix&Linux SE: bash aliases do not expand even with shopt expand_aliases
                            • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.html index ef41339..afac1ab 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.html @@ -1,6 +1,6 @@

                              This function unconditionally re-invokes itself. Missing command?

                              Problematic code

                              -
                              ls() {
                              +
                              ls() {
                                 ls --color=always "$@"
                               }
                               
                              @@ -10,7 +10,7 @@ 

                              Problematic code

                              Correct code

                              Note that command is the literal name of a shell builtin. You should not replace it:

                              -
                              ls() {
                              +
                              ls() {
                                 command ls --color=always "$@"
                               }
                               
                              @@ -25,8 +25,7 @@ 

                              Rationale

                              Exceptions

                              ShellCheck does not intend to warn about infinite recursion or fork bombs in general. This warning is purely meant for unintentional bugs in well meaning wrapper functions.

                              If ShellCheck is triggering on an intentionally malicious fork bomb, either [[ignore]] the issue, or simply add a leading command or condition:

                              -
                              :() { true && :|: & }
                              -
                              +
                              :() { true && :|: & }

                              Related resources

                              • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                              • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.json index 6e45f70..74fa746 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2264.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Blocker" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.html index f1496cb..5210300 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.html @@ -1 +1,21 @@ -

                                Use && for logical AND. Single & will background and return true.

                                \ No newline at end of file +

                                Use && for logical AND. Single & will background and return true.

                                +

                                Problematic code

                                +
                                if [ "$1" = "install" ] & [ "$USER" != "root" ]
                                +then
                                +  echo "Must be root to install"
                                +fi
                                +
                                +

                                Correct code

                                +
                                if [ "$1" = "install" ] && [ "$USER" != "root" ]
                                +then
                                +  echo "Must be root to install"
                                +fi
                                +
                                +

                                Rationale

                                +

                                ShellCheck found a test command followed by a &. This runs the test in the background, effectively ignoring it. To specify "logical AND" between two commands, use &&.

                                +

                                Exceptions

                                +

                                None

                                +

                                Related resources

                                +
                                  +
                                • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                • +
                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.json index 1710706..13c5a67 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2265.json @@ -1,5 +1,5 @@ { - "title": "Use && for logical AND. Single & will background and return true.", + "title": "Use && for logical AND. Single & will background and return true.", "type": "CODE_SMELL", "status": "ready", "remediation": { @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Blocker" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.html index 5082080..16ac06d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.html @@ -1 +1,21 @@ -

                                Use || for logical OR. Single | will pipe.

                                \ No newline at end of file +

                                Use && for logical AND. Single & will background and return true.

                                +

                                Problematic code

                                +
                                if [ "$1" = "--verbose" ] | [ "$1" = "-v" ]
                                +then
                                +  verbose=1
                                +fi
                                +
                                +

                                Correct code

                                +
                                if [ "$1" = "--verbose" ] || [ "$1" = "-v" ]
                                +then
                                +  verbose=1
                                +fi
                                +
                                +

                                Rationale

                                +

                                ShellCheck found a test command followed by a |. This was undoubtedly intended as a logical OR (||).

                                +

                                Exceptions

                                +

                                None

                                +

                                Related resources

                                +
                                  +
                                • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                • +
                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.json index 0784099..7df0875 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2266.json @@ -1,5 +1,5 @@ { - "title": "Use || for logical OR. Single | will pipe.", + "title": "Use && for logical AND. Single & will background and return true.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2267.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2267.html index 82009eb..0f16bdf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2267.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2267.html @@ -1,13 +1,13 @@ -

                                GNU xargs -i is deprecated in favor of -I{}

                                +

                                GNU xargs -i is deprecated in favor of -I{}

                                Problematic code

                                -
                                # Implicit replacement string
                                +
                                # Implicit replacement string
                                 xargs -i ls {}
                                 
                                 # Explicit replacement string
                                 xargs -imyfilename ls myfilename
                                 

                                Correct code

                                -
                                xargs -I {} ls {}
                                +
                                xargs -I {} ls {}
                                 
                                 xargs -I filename ls filename
                                 
                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.html index 5db2b93..3fcab50 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.html @@ -1,24 +1,24 @@

                                Avoid x-prefix in comparisons as it no longer serves a purpose.

                                Problematic code

                                -
                                [ "x$pass" = "xswordfish" ]
                                +
                                [ "x$pass" = "xswordfish" ]
                                 
                                -test x"$var" = x
                                +test x"$var" = x 
                                 

                                Correct code

                                -
                                [ "$pass" = "swordfish" ] 
                                +
                                [ "$pass" = "swordfish" ] 
                                 
                                 test "$var" = ""
                                 

                                Rationale

                                Some older shells would get confused if the first argument started with a dash, or consisted of ! or (. As a workaround, people would prefix variables and values to be compared with x to ensure the left-hand side always started with an alphanumeric character.

                                -

                                POSIX ensures this is not necessary, and all modern shells now follow suite.

                                +

                                POSIX ensures this is not necessary, and all modern shells now follow suit.

                                Examples

                                Bash 1.14 from 1992 incorrectly fails this test. This was fixed for Bash 2.0 in 1996:

                                -
                                var='!'
                                +
                                var='!'
                                 [ "$var" = "!" ]
                                 

                                Dash 0.5.4 from 2007 incorrectly passes this test. This was fixed for Dash 0.5.5 in 2008:

                                -
                                x='(' y=')'
                                +
                                x='(' y=')'
                                 [ "$x" = "$y" ]
                                 

                                Zsh (while not supported by ShellCheck) fixed the same problem in 2015.

                                @@ -26,7 +26,7 @@

                                Exceptions

                                If you are targeting especially old shells, you can ignore this warning (or use a different letter).

                                Related resources

                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.json index a264142..06538eb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2268.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2269.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2269.html index 1997056..248e8af 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2269.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2269.html @@ -1,9 +1,9 @@

                                This variable is assigned to itself, so the assignment does nothing.

                                Problematic code

                                -
                                var="$var"
                                +
                                var="$var"
                                 

                                Correct code

                                -
                                # If the goal is to do nothing
                                +
                                # If the goal is to do nothing
                                 true
                                 

                                Rationale

                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.html index d612053..c3a5d42 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.html @@ -1,32 +1,32 @@ -

                                To assign positional parameters, use 'set -- first second ..' (or use [ ] to compare).

                                +

                                To assign positional parameters, use set -- first second .. (or use [ ] to compare).

                                Problematic code

                                -
                                if [ -z "$1" ]
                                +
                                if [ -z "$1" ]
                                 then
                                   $1="help"
                                 fi
                                 

                                or

                                -
                                if $1="help"
                                +
                                if $1="help"
                                 then
                                   echo "Usage: $0 filename"
                                 fi
                                 

                                Correct code

                                -
                                if [ -z "$1" ]
                                +
                                if [ -z "$1" ]
                                 then
                                   set -- "help"
                                 fi
                                 

                                or

                                -
                                if [ $1 = "help" ]
                                +
                                if [ $1 = "help" ]
                                 then
                                   echo "Usage: $0 filename"
                                 fi
                                 

                                Rationale

                                You have a command on the form $2=value.

                                -

                                If the goal is to assign a new value to the positional parameters, use the set builtin: set -- one two .. will cause $1 to be "one" and $2 to be "two".

                                -

                                If you instead want to compare the value, use [ ] and add spaces: [ "$1" = "foo" ]

                                +

                                If the goal is to assign a new value to the positional parameters, use the set builtin: set -- one two .. will cause $1 to be "one" and $2 to be "two".

                                +

                                If you instead want to compare the value, use [ ] and add spaces: [ "$1" = "foo" ]

                                Exceptions

                                None

                                Related resources

                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.json index cf91b00..a08b8cf 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2270.json @@ -1,5 +1,5 @@ { - "title": "To assign positional parameters, use 'set -- first second ..' (or use [ ] to compare).", + "title": "To assign positional parameters, use set -- first second .. (or use [ ] to compare).", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.html index af51536..badb834 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.html @@ -1,35 +1,35 @@ -

                                For indirection, use arrays, declare "var$n=value", or (for sh) read/eval

                                +

                                For indirection, use arrays, declare "var$n=value", or (for sh) read/eval

                                Problematic code

                                -
                                n=1
                                +
                                n=1
                                 var$n="hello"
                                 

                                Correct code

                                For integer indexing in ksh/bash, consider using an indexed array:

                                -
                                n=1
                                +
                                n=1
                                 var[n]="hello"
                                 echo "${var[n]}"
                                 

                                For string indexing in ksh/bash, use an associative array:

                                -
                                typeset -A var
                                +
                                typeset -A var
                                 n="greeting"
                                 var[$n]="hello"
                                 echo "${var[$n]}"
                                 

                                If you actually need a variable with the constructed name in bash, use declare:

                                -
                                n="Foo"
                                +
                                n="Foo"
                                 declare "var$n=42"
                                 echo "$varFoo"
                                 

                                For sh, with single line contents, consider read:

                                -
                                n="Foo"
                                +
                                n="Foo"
                                 read -r "var$n" << EOF
                                 hello
                                 EOF
                                 echo "$varFoo"
                                 

                                or with careful escaping, eval:

                                -
                                n=Foo
                                -eval "var$n='hello'"
                                +
                                n=Foo
                                +eval "var$n='hello'"
                                 echo "$varFoo"
                                 

                                Rationale

                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.json index c68b195..d60dff8 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2271.json @@ -1,5 +1,5 @@ { - "title": "For indirection, use arrays, declare 'var$n=value', or (for sh) read\/eval", + "title": "For indirection, use arrays, declare "var$n=value", or (for sh) read\/eval", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.html index 5e7413c..d11e24b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.html @@ -1,9 +1,9 @@ -

                                Command name contains ==. For comparison, use [ "$var" = value ].

                                +

                                Command name contains ==. For comparison, use [ "$var" = value ].

                                Problematic code

                                -
                                $a/$b==foo/bar
                                +
                                $a/$b==foo/bar
                                 

                                Correct code

                                -
                                [ "$a/$b" = "foo/bar" ]
                                +
                                [ "$a/$b" = "foo/bar" ] 
                                 

                                Rationale

                                ShellCheck found a command name that contains a ==. Most likely, this was intended as a kind of comparison.

                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.json index ddc1258..920fe50 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2272.json @@ -1,5 +1,5 @@ { - "title": "Command name contains ==. For comparison, use [ '$var' = value ].", + "title": "Command name contains ==. For comparison, use [ "$var" = value ].", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2273.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2273.html index 20b0d62..0d46a6b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2273.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2273.html @@ -1,13 +1,13 @@ -

                                Sequence of ===s found. Merge conflict or intended as a commented border?

                                +

                                Sequence of ===s found. Merge conflict or intended as a commented border?

                                Problematic code

                                -
                                =======
                                +
                                =======
                                 

                                Correct code

                                Either resolve the merge conflict, or use # ======= for a border

                                Rationale

                                ShellCheck found a series of =======s. If this was supposed to be a border or separator, use a comment.

                                However, it could also be left behind from a source control merge conflict:

                                -
                                <<<<<<< HEAD
                                +
                                <<<<<<< HEAD
                                 echo "Goodbye World"
                                 =======
                                 echo "Hello World!"
                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2274.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2274.html
                                index d6632a4..d48e0e8 100644
                                --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2274.html
                                +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2274.html
                                @@ -1,9 +1,9 @@
                                -

                                Command name starts with ===. Intended as a commented border?

                                +

                                Command name starts with ===. Intended as a commented border?

                                Problematic code

                                -
                                ===================== MAIN SECTION =======================
                                +
                                ===================== MAIN SECTION =======================
                                 

                                Correct code

                                -
                                # ===================== MAIN SECTION =======================
                                +
                                # ===================== MAIN SECTION =======================
                                 

                                Rationale

                                ShellCheck found a command that starts with a series of ===s. This may have been intended as a border, but is missing the # to turn it into a harmless comment.

                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2275.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2275.html index 7f41830..63af0f1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2275.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2275.html @@ -1,16 +1,16 @@ -

                                Command name starts with =. Bad line break?

                                +

                                Command name starts with =. Bad line break?

                                Problematic code

                                -
                                my_variable
                                +
                                my_variable
                                   =value
                                 

                                Correct code

                                -
                                myvariable=value
                                +
                                myvariable=value
                                 

                                Rationale

                                ShellCheck found a command name starting with a =. This was likely not meant as a new command, but instead a continuation from a previous line.

                                Make sure the = is used correctly.

                                Exceptions

                                -

                                None, though you can quote the value to make ShellCheck ignore it, e.g. "=foo".

                                +

                                None, though you can quote the value to make ShellCheck ignore it, e.g. "=foo".

                                Related resources

                                • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.html index 578c617..d56daf7 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.html @@ -1,13 +1,13 @@ -

                                  This is interpreted as a command name containing '='. Bad assignment or comparison?

                                  +

                                  This is interpreted as a command name containing =. Bad assignment or comparison?

                                  Problematic code

                                  -
                                  "$var"=42
                                  +
                                  "$var"=42
                                   if "$var"=42
                                   then
                                     true
                                   fi
                                   

                                  Correct code

                                  -
                                  var=42
                                  +
                                  var=42
                                   if [ "$var" = 42 ]
                                   then
                                     true
                                  @@ -15,10 +15,10 @@ 

                                  Correct code

                                  Rationale

                                  ShellCheck found a command name containing an unquoted equals sign =. This was likely intended as either a comparison or an assignment.

                                  -

                                  To compare two values, use e.g. [ "$var" = "42" ]

                                  -

                                  To assign a value, use e.g. var="42"

                                  +

                                  To compare two values, use e.g. [ "$var" = "42" ]

                                  +

                                  To assign a value, use e.g. var="42"

                                  Exceptions

                                  -

                                  None, though you can quote the = to make ShellCheck ignore it: "$var=42".

                                  +

                                  None, though you can quote the = to make ShellCheck ignore it: "$var=42".

                                  Related resources

                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                  • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.json index bb3ef20..5eb0458 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2276.json @@ -1,5 +1,5 @@ { - "title": "This is interpreted as a command name containing '='. Bad assignment or comparison?", + "title": "This is interpreted as a command name containing =. Bad assignment or comparison?", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.html index 15d7c8c..2c791b3 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.html @@ -1,15 +1,15 @@ -

                                    Use BASH_ARGV0 to assign to $0 in bash (or use [ ] to compare).

                                    +

                                    Use BASH_ARGV0 to assign to $0 in bash (or use [ ] to compare).

                                    Problematic code

                                    -
                                    #!/bin/bash
                                    +
                                    #!/bin/bash
                                     $0=myscriptname
                                     

                                    Correct code

                                    -
                                    #!/bin/bash
                                    +
                                    #!/bin/bash
                                     BASH_ARGV0=myscriptname
                                     

                                    Rationale

                                    You appear to be trying to assign a new value to $0 in a Bash script. To do this, instead assign to the special variable BASH_ARGV0.

                                    -

                                    If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                    +

                                    If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                    Exceptions

                                    None.

                                    Related resources

                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.json index 14e0381..247e9e5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2277.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Blocker" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2278.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2278.html index 5166346..8fea9ae 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2278.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2278.html @@ -1,11 +1,11 @@ -

                                    $0 can't be assigned in Ksh (but it does reflect the current function).

                                    +

                                    $0 can't be assigned in Ksh (but it does reflect the current function).

                                    Problematic code

                                    -
                                    #!/bin/ksh
                                    +
                                    #!/bin/ksh
                                     $0=myname
                                     echo "Usage: $0 --help"
                                     

                                    Correct code

                                    -
                                    #!/bin/ksh
                                    +
                                    #!/bin/ksh
                                     myname() {
                                       echo "Usage: $0 --help"
                                     }
                                    @@ -15,7 +15,7 @@ 

                                    Rationale

                                    You appear to be trying to assign a new value to $0 in Ksh.

                                    This is not possible. However, $0 will reflect the current function name, so if you wrap your code in a function with your chosen name, you can have $0 expand to it.

                                    Exceptions

                                    -

                                    If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                    +

                                    If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                    Related resources

                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                    • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2279.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2279.html index 494f077..bdc5112 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2279.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2279.html @@ -1,6 +1,6 @@ -

                                      $0 can't be assigned in Dash. This becomes a command name.

                                      +

                                      $0 can't be assigned in Dash. This becomes a command name.

                                      Problematic code

                                      -
                                      #!/bin/dash
                                      +
                                      #!/bin/dash
                                       $0=myname
                                       

                                      Correct code

                                      @@ -9,7 +9,7 @@

                                      Rationale

                                      You appear to be trying to assign a new value to $0 in Dash.

                                      Dash does not support this. Write around it, or switch to Bash.

                                      Exceptions

                                      -

                                      If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                      +

                                      If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                      Related resources

                                      • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                      • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2280.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2280.html index 01ce4d5..f2ac0c2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2280.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2280.html @@ -1,6 +1,6 @@ -

                                        $0 can't be assigned this way, and there is no portable alternative.

                                        +

                                        $0 can't be assigned this way, and there is no portable alternative.

                                        Problematic code

                                        -
                                        #!/bin/sh
                                        +
                                        #!/bin/sh
                                         $0=myname
                                         

                                        Correct code

                                        @@ -9,7 +9,7 @@

                                        Rationale

                                        You appear to be trying to assign a new value to $0 in a sh script.

                                        There is no portable way to do this. Write around it, or switch to Bash.

                                        Exceptions

                                        -

                                        If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                        +

                                        If you instead wanted to compare the value of $0, use a comparison like [ "$0" = "myname" ].

                                        Related resources

                                        • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                        • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.html index b13059c..8ea5fb5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.html @@ -1,18 +1,18 @@ -

                                          Don't use $/${} on the left side of assignments.

                                          +

                                          Don't use $/${} on the left side of assignments.

                                          Problematic code

                                          -
                                          $greeting="Hello World"
                                          +
                                          $greeting="Hello World"
                                           ${greeting}="Hello World"
                                           

                                          Correct code

                                          -
                                          greeting="Hello World"
                                          +
                                          greeting="Hello World"
                                           

                                          Alternatively, if the goal was to assign to a variable whose name is in another variable (indirection), use declare:

                                          -
                                          name=foo
                                          +
                                          name=foo
                                           declare "$name=hello world"
                                           echo "$foo"
                                           

                                          Or if you actually wanted to compare the value, use a test expression:

                                          -
                                          if [ "$greeting" = "hello world" ]
                                          +
                                          if [ "$greeting" = "hello world" ]
                                           then
                                             echo "Programmer, I presume?"
                                           fi
                                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.json
                                          index b2bd779..24020b0 100644
                                          --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.json
                                          +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2281.json
                                          @@ -10,5 +10,5 @@
                                               "shell",
                                               "convention"
                                             ],
                                          -  "defaultSeverity": "Major"
                                          +  "defaultSeverity": "Blocker"
                                           }
                                          \ No newline at end of file
                                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2282.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2282.html
                                          index 9118ae5..d101d0c 100644
                                          --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2282.html
                                          +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2282.html
                                          @@ -1,9 +1,9 @@
                                           

                                          Variable names can't start with numbers, so this is interpreted as a command.

                                          Problematic code

                                          -
                                          411toppm=true
                                          +
                                          411toppm=true
                                           

                                          Correct code

                                          -
                                          _411toppm=true
                                          +
                                          _411toppm=true
                                           

                                          Rationale

                                          You appear to be assigning to a variable name that starts with a digit. This is not allowed: variables must start with A-Z, a-z or _.

                                          diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2283.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2283.html index c56840d..ade7210 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2283.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2283.html @@ -1,6 +1,6 @@ -

                                          Use [ ] to compare values, or remove spaces around = to assign (or quote '=' if literal).

                                          +

                                          Use [ ] to compare values, or remove spaces around = to assign (or quote '=' if literal).

                                          Problematic code

                                          -
                                          # Assignment
                                          +
                                          # Assignment
                                           var = value
                                           
                                           # Comparison
                                          @@ -10,7 +10,7 @@ 

                                          Problematic code

                                          fi

                                          Correct code

                                          -
                                          # Assignment
                                          +
                                          # Assignment
                                           var=value
                                           
                                           # Comparison
                                          @@ -21,12 +21,11 @@ 

                                          Correct code

                                          Rationale

                                          ShellCheck found an unquoted = after a word.

                                          -

                                          If this was supposed to be a comparison, use square brackets: [ "$var" = value ]

                                          +

                                          If this was supposed to be a comparison, use square brackets: [ "$var" = value ]

                                          If this was supposed to be an assignment, remove spaces around =: var=value

                                          Exceptions

                                          If the = was meant literally, quote it:

                                          -
                                          grep '=true' file.cfg
                                          -
                                          +
                                          grep '=true' file.cfg

                                          Related resources

                                          • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                          • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2284.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2284.html index a474b1d..b1dd1ba 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2284.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2284.html @@ -1,12 +1,12 @@ -

                                            Use [ x = y ] to compare values (or quote '==' if literal).

                                            +

                                            Use [ x = y ] to compare values (or quote '==' if literal).

                                            Problematic code

                                            -
                                            if $var == value
                                            +
                                            if $var == value
                                             then
                                               echo "Match"
                                             fi
                                             

                                            Correct code

                                            -
                                            if [ "$var" = value ]
                                            +
                                            if [ "$var" = value ]
                                             then
                                               echo "Match"
                                             fi
                                            @@ -16,8 +16,7 @@ 

                                            Rationale

                                            This was most likely supposed to be a comparison, so use square brackets as in the correct code.

                                            Exceptions

                                            If the == was supposed to be literal, you can quote it to make ShellCheck ignore it:

                                            -
                                            grep '===' file.js
                                            -
                                            +
                                            grep '===' file.js

                                            Related resources

                                            • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                            • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2285.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2285.html index 0d050f9..0307803 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2285.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2285.html @@ -1,16 +1,15 @@ -

                                              Remove spaces around += to assign (or quote '+=' if literal).

                                              +

                                              Remove spaces around += to assign (or quote '+=' if literal).

                                              Problematic code

                                              -
                                              var += "my text"
                                              +
                                              var += "my text"
                                               

                                              Correct code

                                              -
                                              var+="my text"
                                              +
                                              var+="my text"
                                               

                                              Rationale

                                              ShellCheck found an unquoted += after a word. To append text to a variable, remove spaces around += as in the example.

                                              Exceptions

                                              If the += was supposed to be literal, you can quote it to make ShellCheck ignore it:

                                              -
                                              grep '+=' files..
                                              -
                                              +
                                              grep '+=' files..

                                              Related resources

                                              • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                              • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2286.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2286.html index 43b1473..fb1856d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2286.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2286.html @@ -1,12 +1,12 @@

                                                This empty string is interpreted as a command name. Double check syntax (or use 'true' as a no-op).

                                                Problematic code

                                                -
                                                jq 
                                                -   ''
                                                +
                                                jq 
                                                +   ''
                                                    file.json
                                                 

                                                Correct code

                                                -
                                                jq \
                                                -  '' \
                                                +
                                                jq \
                                                +  '' \
                                                   file.json
                                                 

                                                Rationale

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2287.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2287.html index 62e6d92..bcabf3b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2287.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2287.html @@ -1,9 +1,9 @@

                                                This is interpreted as a command name ending with '/'. Double check syntax.

                                                Problematic code

                                                -
                                                df/
                                                +
                                                df/
                                                 

                                                Correct code

                                                -
                                                df /
                                                +
                                                df /
                                                 

                                                Rationale

                                                ShellCheck found a command name that ends with /. Since directories are not valid commands, this is always wrong.

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2288.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2288.html index 9b903e6..7b10c26 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2288.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2288.html @@ -1,12 +1,13 @@

                                                This is interpreted as a command name ending with apostrophe. Double check syntax.

                                                -

                                                (also applies to multiple other characters like .,([{<>}])#"')

                                                +

                                                (also applies to multiple other characters like .,([{<>}])#"')

                                                Problematic code

                                                -
                                                var=$("wget 'http://www.shellcheck.net/'")
                                                +
                                                var=$("wget 'http://www.shellcheck.net/'")
                                                 echo Usage: $0 {start|stop|restart}
                                                 array=val1, val2, val3
                                                +
                                                 

                                                Correct code

                                                -
                                                var="$(wget 'http://www.shellcheck.net/')"
                                                +
                                                var="$(wget 'http://www.shellcheck.net/')"
                                                 echo "Usage: $0 {start|stop|restart}"
                                                 array=(val1 val2 val3)
                                                 
                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2289.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2289.html index 9b10fea..267841e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2289.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2289.html @@ -1,13 +1,13 @@

                                                This is interpreted as a command name containing a linefeed. Double check syntax.

                                                (or tab)

                                                Problematic code

                                                -
                                                '''
                                                +
                                                '''
                                                 This script greets the planet
                                                -'''
                                                +'''
                                                 echo "Hello World"
                                                 

                                                Correct code

                                                -
                                                # This script greets the planet
                                                +
                                                # This script greets the planet
                                                 echo "Hello World"
                                                 

                                                Rationale

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2290.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2290.html index fd88211..521d2ca 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2290.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2290.html @@ -1,17 +1,16 @@

                                                Remove spaces around = to assign.

                                                Problematic code

                                                -
                                                export LC_ALL = "POSIX"
                                                +
                                                export LC_ALL = "POSIX"
                                                 

                                                Correct code

                                                -
                                                export LC_ALL="POSIX"
                                                +
                                                export LC_ALL="POSIX"
                                                 

                                                Rationale

                                                Parameters to export, declare, local, typeset and readonly may not have spaces around the = or += operator. This is the same as for regular variable assignments:

                                                export var = value   # Invalid: spaces around =
                                                 export var =value    # Invalid: space before =
                                                 export var= value    # Invalid: space after =
                                                -export var=value     # Valid
                                                -
                                                +export var=value # Valid

                                                This is because each individual argument to these commands is interpreted as a string in the format name=value. By adding spaces, you are instead passing the three strings var, =, value, none of which follow this format.

                                                Exceptions

                                                None

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2291.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2291.html index 7b3a566..c8a00fe 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2291.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2291.html @@ -1,9 +1,9 @@

                                                Quote repeated spaces to avoid them collapsing into one.

                                                Problematic code

                                                -
                                                echo Hello        World
                                                +
                                                echo Hello        World
                                                 

                                                Correct code

                                                -
                                                echo "Hello        World"
                                                +
                                                echo "Hello        World"
                                                 

                                                Rationale

                                                ShellCheck found multiple unquoted spaces between words passed to echo. Due to the way arguments are interpreted and passed in the shell, these will collapse into a single space:

                                                @@ -11,7 +11,7 @@

                                                Rationale

                                                Hello World

                                                If you want to output multiple spaces, such as when creating a notice or banner, make sure the spaces are quoted, e.g. by adding (or extending) double quotes to include them:

                                                -
                                                $ echo "Hello        World"
                                                +
                                                $ echo "Hello        World"
                                                 Hello        World
                                                 

                                                Exceptions

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.html index 778fc13..cd0beae 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.html @@ -1,10 +1,10 @@ -

                                                Prefer [[ ]] over [ ] for tests in Bash/Ksh.

                                                +

                                                Prefer [[ ]] over [ ] for tests in Bash/Ksh.

                                                This is an [[optional]] suggestion. It must be explicitly enabled with a [[directive]] enable=require-double-brackets in a # shellcheck comment or .shellcheckrc

                                                Problematic code

                                                -
                                                [ -e /etc/issue ]
                                                +
                                                [ -e /etc/issue ] 
                                                 

                                                Correct code

                                                -
                                                [[ -e /etc/issue ]]
                                                +
                                                [[ -e /etc/issue ]]
                                                 

                                                Rationale

                                                ShellCheck has been explicitly asked to warn about uses of [ .. ] in favor of the extended Bash/Ksh test [[ .. ]].

                                                @@ -14,5 +14,5 @@

                                                Exceptions

                                                This suggestion does not trigger for Sh or Dash scripts, even when explicitly enabled, as these shells don't support [[ .. ]].

                                                Related resources

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.json index e1fd82d..af05e21 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2292.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2293.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2293.html index 411271c..2f48f17 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2293.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2293.html @@ -1,9 +1,9 @@

                                                When eval'ing @Q-quoted words, use * rather than @ as the index.

                                                Problematic code

                                                -
                                                eval "$MYCOMMAND ${@@Q}"
                                                +
                                                eval "$MYCOMMAND ${@@Q}"
                                                 

                                                Correct code

                                                -
                                                eval "$MYCOMMAND ${*@Q}"
                                                +
                                                eval "$MYCOMMAND ${*@Q}"
                                                 

                                                Rationale

                                                ShellCheck noticed that you are calling eval and including an escaped array. However, the array is passed as multiple arguments and relies on being implicitly joined together to form a single shell string, which eval can then evaluate.

                                                diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2294.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2294.html index 8d9dafd..2b463d2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2294.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2294.html @@ -1,65 +1,65 @@

                                                eval negates the benefit of arrays. Drop eval to preserve whitespace/symbols (or eval as string).

                                                Problematic code

                                                -
                                                check() {
                                                +
                                                check() {
                                                   eval "$@" || exit
                                                 }
                                                 

                                                Correct code

                                                -
                                                check() {
                                                +
                                                check() {
                                                   "$@" || exit
                                                 }
                                                 

                                                Rationale

                                                -

                                                ShellCheck found eval used on an array (or equivalently, "$@"). This is problematic because it effectively throws away all boundary information and rebuilds it from shell words.

                                                -

                                                Let's say you invoke check sed -i '$d' "my file.txt":

                                                -

                                                eval "$@" will:

                                                +

                                                ShellCheck found eval used on an array (or equivalently, "$@"). This is problematic because it effectively throws away all boundary information and rebuilds it from shell words.

                                                +

                                                Let's say you invoke check sed -i '$d' "my file.txt":

                                                +

                                                eval "$@" will:

                                                1. Join the elements on spaces: sed -i $d my file.txt
                                                2. Split the string on shell word boundaries: sed, -i, $d, my file.txt
                                                3. Perform shell expansions (assuming $d is unset): sed, -i, my, file.txt
                                                4. Execute the first element as the command and the rest as its arguments, as if running sed -i 'my' 'file.txt'
                                                -

                                                "$@" will

                                                +

                                                "$@" will

                                                1. Execute the first element as the command and the rest as its arguments, as if running sed -i '$d' 'my file.txt'
                                                -

                                                Note that while "$@" is essentially always better than eval "$@", it's easy to unintentionally introduce a dependency on bad behavior through the shell debugging anti-strategy of "adding quotes until it works":

                                                +

                                                Note that while "$@" is essentially always better than eval "$@", it's easy to unintentionally introduce a dependency on bad behavior through the shell debugging anti-strategy of "adding quotes until it works":

                                                # Works with problematic example because of double-escaping, fails with correct example
                                                -check ls -l "'My File.txt'" 
                                                +check ls -l "'My File.txt'" 
                                                 
                                                 # Works with correct example the way it was always intended:
                                                -check ls -l "My File.txt"
                                                +check ls -l "My File.txt" 
                                                 

                                                The correct example is still better, but the function invocation has to be tweaked as well.

                                                Exceptions

                                                If each of the array elements is a carefully escaped shell command or word, use * instead of @ to explicitly join the elements on spaces which is what would happen anyways:

                                                on_exit=(
                                                   'rm /tmp/myfile; '
                                                -  'echo "Finished on $(date)" > log.txt; '
                                                +  'echo "Finished on $(date)" > log.txt; '
                                                 )
                                                 
                                                -# Equivalent to `eval "${on_exit[@]}"`, but more explicit
                                                -eval "${on_exit[*]}"
                                                +# Equivalent to `eval "${on_exit[@]}"`, but more explicit
                                                +eval "${on_exit[*]}"
                                                 
                                                 # Even better in this case, as it does not require
                                                 # semicolons and commands don't interfere:
                                                -for cmd in "${on_exit[@]}"
                                                +for cmd in "${on_exit[@]}"
                                                 do
                                                -  eval "$cmd"
                                                +  eval "$cmd"
                                                 done
                                                 

                                                If you require eval for another part of the command, explicitly transform the array into a series of escaped shell words. This ensures that the array elements will eval back to themselves:

                                                # Assumed to be outside of our control, 
                                                -# otherwise we would doput this in an array as well:
                                                -COMMAND='dialog --menu "Choose file:" 15 40 4'
                                                +# otherwise we would output this in an array as well:
                                                +COMMAND='dialog --menu "Choose file:" 15 40 4'
                                                 
                                                 # Our array:
                                                 array=(
                                                -  1 "My File.txt"
                                                -  2 "My Other File.txt"
                                                +  1 "My File.txt"
                                                +  2 "My Other File.txt"
                                                 )
                                                -eval "$COMMAND ${array[*]@Q}"                     # Bash 4+
                                                -eval "$COMMAND $(printf "%q " "${array[@]}")"     # Bash 1+
                                                +eval "$COMMAND ${array[*]@Q}"                     # Bash 4+
                                                +eval "$COMMAND $(printf "%q " "${array[@]}")"     # Bash 1+
                                                 

                                                Related resources

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.html index 24f7fcc..b79c2c2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.html @@ -1,7 +1,7 @@ -

                                                  Expansions inside ${..} need to be quoted separately, otherwise they will match as a pattern.

                                                  +

                                                  Expansions inside ${..} need to be quoted separately, otherwise they will match as a pattern.

                                                  Problematic code

                                                  -
                                                  relative_path() {
                                                  -  printf '%s\n' "${2#$1}"
                                                  +
                                                  relative_path() {
                                                  +  printf '%s\n' "${2#$1}"
                                                   }
                                                   
                                                   # Results in "/tmp/King_Kong_[1933]/extras/trailer.mkv" because the prefix fails to match
                                                  @@ -9,10 +9,11 @@ 

                                                  Problematic code

                                                  # Results in "cover.jpg" even though the prefix is different relative_path "/tmp/King_Kong_[1933]/" "/tmp/King_Kong_3/cover.jpg" +

                                                  Correct code

                                                  -
                                                  relative_path() {
                                                  -  printf '%s\n' "${2#"$1"}"
                                                  +
                                                  relative_path() {
                                                  +  printf '%s\n' "${2#"$1"}"
                                                   }
                                                   # Results in "extras/trailer.mkv" as expected
                                                   relative_path "/tmp/King_Kong_[1933]/" "/tmp/King_Kong_[1933]/extras/trailer.mkv"
                                                  @@ -24,7 +25,7 @@ 

                                                  Rationale

                                                  When using expansions in a parameter expansion prefix/suffix expression, the expansion needs to be quoted separately or it will match as a pattern. The quotes around the outer parameter expansion does not protect against this.

                                                  This means that any variable that contains e.g. brackets, asterisks or question marks may not match as expected. In the example, [1933] was interpreted as a pattern character range and would therefore match /tmp/King_Kong_3/ but not /tmp/King_Kong_[1933]/ as was the intention.

                                                  Exceptions

                                                  -

                                                  If you wanted to treat the string as a pattern, such as suffix=".*"; file="${var%$suffix}"; then you can [[ignore]] this suggestion.

                                                  +

                                                  If you wanted to treat the string as a pattern, such as suffix=".*"; file="${var%$suffix}"; then you can [[ignore]] this suggestion.

                                                  Related resources

                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.json index 2f7500a..cdecd48 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2295.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2296.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2296.html index db7d9f4..eb82095 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2296.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2296.html @@ -1,10 +1,10 @@ -

                                                    Parameter expansions can't start with {. Double check syntax.

                                                    +

                                                    Parameter expansions can't start with {. Double check syntax.

                                                    (or any other character)

                                                    Problematic code

                                                    -
                                                    echo "Hello ${{name}"
                                                    +
                                                    echo "Hello ${{name}"
                                                     

                                                    Correct code

                                                    -
                                                    echo "Hello ${name}"
                                                    +
                                                    echo "Hello ${name}"
                                                     

                                                    Rationale

                                                    ShellCheck found a parameter expansion ${something} that starts with an invalid character. In the example, this was caused by accidentally duplicating the { in ${{name}.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.html index a6185d1..5154b47 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.html @@ -1,14 +1,14 @@ -

                                                    Double quotes must be outside ${}: ${"invalid"} vs "${valid}".

                                                    +

                                                    Double quotes must be outside ${}: ${"invalid"} vs "${valid}".

                                                    Problematic code

                                                    -
                                                    echo ${"USER"}
                                                    +
                                                    echo ${"USER"}
                                                     

                                                    Correct code

                                                    -
                                                    echo "${USER}"
                                                    +
                                                    echo "${USER}"
                                                     

                                                    Rationale

                                                    ShellCheck found a parameter expansion containing what appears to be a quoted variable name.

                                                    -

                                                    While the parameter expansion itself must be quoted, as in "${valid}", the quotes may not appear inside the {} as in ${"invalid"}.

                                                    -

                                                    Also note that translated strings like $"Hello" may not use curly braces.

                                                    +

                                                    While the parameter expansion itself must be quoted, as in "${valid}", the quotes may not appear inside the {} as in ${"invalid"}.

                                                    +

                                                    Also note that translated strings like $"Hello" may not use curly braces.

                                                    Exceptions

                                                    None

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.json index 34e6a89..db62344 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2297.json @@ -1,5 +1,5 @@ { - "title": "Double quotes must be outside ${}: ${'invalid'} vs '${valid}'.", + "title": "Double quotes must be outside ${}: ${"invalid"} vs "${valid}".", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2298.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2298.html index 767fe52..30d5842 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2298.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2298.html @@ -1,22 +1,23 @@

                                                    ${$x} is invalid. For expansion, use ${x}. For indirection, use arrays, ${!x} or (for sh) eval.

                                                    (or ${${x}} is invalid)

                                                    Problematic code

                                                    -
                                                    # Expecting $RETRIES or 3 if unset
                                                    +
                                                    # Expecting $RETRIES or 3 if unset
                                                     retries=${$RETRIES:-3}
                                                     

                                                    or

                                                    -
                                                    mypath="/tmp/foo.txt"
                                                    +
                                                    mypath="/tmp/foo.txt"
                                                     var=mypath
                                                    -result=${$var##*/}  # Expecting ${mypath##*/}, i.e. 'foo.txt'
                                                    +result=${$var##*/}  # Expecting ${mypath##*/}, i.e. 'foo.txt'
                                                     

                                                    Correct code

                                                    -
                                                    retries=${RETRIES:-3}
                                                    +
                                                    retries=${RETRIES:-3}
                                                     

                                                    or

                                                    -
                                                    mypath="/tmp/foo.txt"
                                                    +
                                                    mypath="/tmp/foo.txt"
                                                     var=mypath
                                                     result=${!var}
                                                     result=${result##*/}
                                                    +
                                                     

                                                    Rationale

                                                    ShellCheck found a parameter expansion ${..} where the first element was a second parameter expansion, either ${$x..} or ${${x}..}. This is not valid.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2299.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2299.html index b9a0aba..f870f2a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2299.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2299.html @@ -1,10 +1,10 @@

                                                    Parameter expansions can't be nested. Use temporary variables.

                                                    Problematic code

                                                    -
                                                    path="/path/to/MyFile.mp3"
                                                    +
                                                    path="/path/to/MyFile.mp3"
                                                     echo "Playing ${${path##*/}%.*}"    # Expect: Playing MyFile
                                                     

                                                    Correct code

                                                    -
                                                    path="/path/to/MyFile.mp3"
                                                    +
                                                    path="/path/to/MyFile.mp3"
                                                     tmp=${path##*/}
                                                     echo "Playing ${tmp%.*}"
                                                     
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2300.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2300.html index 142b50d..92fa24f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2300.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2300.html @@ -1,9 +1,9 @@

                                                    Parameter expansion can't be applied to command substitutions. Use temporary variables.

                                                    Problematic code

                                                    -
                                                    echo "Building ${$(git rev-parse --show-toplevel)##*/}"
                                                    +
                                                    echo "Building ${$(git rev-parse --show-toplevel)##*/}"
                                                     

                                                    Correct code

                                                    -
                                                    tmp=$(git rev-parse --show-toplevel)
                                                    +
                                                    tmp=$(git rev-parse --show-toplevel)
                                                     echo "Building ${tmp##*/}"
                                                     

                                                    Rationale

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2301.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2301.html index 5367a8a..7ea40bd 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2301.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2301.html @@ -1,14 +1,14 @@

                                                    Parameter expansion starts with unexpected quotes. Double check syntax.

                                                    Problematic code

                                                    -
                                                    echo ${"Hello World"}
                                                    +
                                                    echo ${"Hello World"}
                                                     

                                                    Correct code

                                                    -
                                                    echo $"Hello World"
                                                    +
                                                    echo $"Hello World"
                                                     

                                                    Rationale

                                                    ShellCheck found a parameter expansion ${...} that contains an unexpected syntax element, such as single or double quotes.

                                                    -

                                                    In the example, this was due to wrapping a translated string $".." with curly braces, which is not valid.

                                                    -

                                                    It is unclear what the intention is with invalid expansions like ${'foo'}, ${$"foo"}, so please look up how to do what you were trying to do.

                                                    +

                                                    In the example, this was due to wrapping a translated string $".." with curly braces, which is not valid.

                                                    +

                                                    It is unclear what the intention is with invalid expansions like ${'foo'}, ${$"foo"}, so please look up how to do what you were trying to do.

                                                    Exceptions

                                                    If this warning triggers for working code, please submit a bug.

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.html index 42bf14b..73d8e95 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.html @@ -1,41 +1,41 @@ -

                                                    This loops over values. To loop over keys, use "${!array[@]}".

                                                    +

                                                    This loops over values. To loop over keys, use "${!array[@]}".

                                                    Plus companion warning [[SC2303]]: i is an array value, not a key. Use directly or loop over keys instead.

                                                    Problematic code

                                                    -
                                                    array=(foo bar)
                                                    -for i in "${array[@]}"
                                                    +
                                                    array=(foo bar)
                                                    +for v in "${array[@]}"
                                                     do
                                                    -  echo "Value is ${array[$i]}"
                                                    +  echo "Value is ${array[$v]}"
                                                     done
                                                     

                                                    Correct code

                                                    Either loop over values

                                                    -
                                                    for i in "${array[@]}"
                                                    +
                                                    for v in "${array[@]}"
                                                     do
                                                    -  echo "Value is $i"
                                                    +  echo "Value is $v"
                                                     done
                                                     

                                                    or loop over keys:

                                                    -
                                                    for i in "${!array[@]}"
                                                    +
                                                    for k in "${!array[@]}"  # Note `!`
                                                     do
                                                    -  echo "Key is $i"
                                                    -  echo "Value is ${array[$i]}"
                                                    +  echo "Key is $k"
                                                    +  echo "Value is ${array[$k]}"
                                                     done
                                                     

                                                    Rationale

                                                    ShellCheck found a for loop over array values, where the variable is used as an array key.

                                                    -

                                                    In the problematic example, the loop will print Value is foo twice. On the second iteration, i=bar, and bar is unset and considered zero, so ${array[$i]} becomes ${array[bar]} becomes ${array[0]} becomes foo.

                                                    -

                                                    If you don't care about the key, simply loop over array values and use $i to refer to the array value, like in the first correct example.

                                                    -

                                                    If you do want the key, loop over array keys with "${!array[@]}", use $i to refer to the array key, and ${array[$i]} to refer to the array value.

                                                    +

                                                    In the problematic example, the loop will print Value is foo twice. On the second iteration, v=bar, and bar is unset and considered zero, so ${array[$v]} becomes ${array[bar]} becomes ${array[0]} becomes foo.

                                                    +

                                                    If you don't care about the key, simply loop over array values and use $v to refer to the array value, like in the first correct example.

                                                    +

                                                    If you do want the key, loop over array keys with "${!array[@]}", use $k to refer to the array key, and ${array[$k]} to refer to the array value.

                                                    Exceptions

                                                    If you do want to use values from the arrays as keys in the same array, you can [[ignore]] these messages with a directive:

                                                    -
                                                    declare -A fatherOf=(
                                                    +
                                                    declare -A fatherOf=(
                                                       ["Eric Bloodaxe"]="Harald Fairhair"
                                                       ["Harald Fairhair"]="Halfdan the Black"
                                                       ["Halfdan the Black"]="Gudrød the Hunter"
                                                       ["Gudrød the Hunter"]="Halfdan the Mild"
                                                     )
                                                     
                                                    -# shellcheck disable=SC2302,SC23203
                                                    +# shellcheck disable=SC2302,SC2303
                                                     for i in "${fatherOf[@]}" 
                                                     do
                                                       echo "${fatherOf[$i]:-(missing)} begat $i"
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.json
                                                    index 3cff15e..d8fe6d8 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.json
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2302.json
                                                    @@ -1,5 +1,5 @@
                                                     {
                                                    -  "title": "This loops over values. To loop over keys, use '${!array[@]}'.",
                                                    +  "title": "This loops over values. To loop over keys, use "${!array[@]}".",
                                                       "type": "CODE_SMELL",
                                                       "status": "ready",
                                                       "remediation": {
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2304.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2304.html
                                                    index aabc2c4..c8fc587 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2304.html
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2304.html
                                                    @@ -1,9 +1,9 @@
                                                     

                                                    * must be escaped to multiply: \*. Modern $((x * y)) avoids this issue.

                                                    Problematic code

                                                    -
                                                    result=$(expr 2 * 3)
                                                    +
                                                    result=$(expr 2 * 3)
                                                     

                                                    Correct code

                                                    -
                                                    # Modern, efficient, POSIX standard approach
                                                    +
                                                    # Modern, efficient, POSIX standard approach
                                                     result=$(( 2 * 3 ))
                                                     
                                                     # Older, slower approach
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2305.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2305.html
                                                    index fc4d362..3d7f8eb 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2305.html
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2305.html
                                                    @@ -1,9 +1,9 @@
                                                     

                                                    Quote regex argument to expr to avoid it expanding as a glob.

                                                    Problematic code

                                                    -
                                                    expr "$input" : [0-9]*
                                                    +
                                                    expr "$input" : [0-9]*
                                                     

                                                    Correct code

                                                    -
                                                    expr "$input" : "[0-9]*"
                                                    +
                                                    expr "$input" : "[0-9]*"
                                                     

                                                    Rationale

                                                    ShellCheck found an expr command using : to match a regex, but the regex is not quoted and therefore being treated as a glob.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2306.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2306.html index 274f49e..39a9128 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2306.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2306.html @@ -1 +1,20 @@ -

                                                    Escape glob characters in arguments to expr to avoid pathname expansion.

                                                    \ No newline at end of file +

                                                    Escape glob characters in arguments to expr to avoid pathname expansion.

                                                    +

                                                    Problematic code

                                                    +
                                                    f=$(expr "$c" * 9 / 5 + 32)
                                                    +
                                                    +

                                                    Correct code

                                                    +

                                                    Prefer rewriting to a modern style (see [[SC2003]]):

                                                    +
                                                    f=$((c * 9 / 5 + 32))
                                                    +
                                                    +

                                                    If you do not wish to do so, at least escape the glob characters when passing them to expr:

                                                    +
                                                    f=$(expr "$c" \* 9 / 5 + 32)
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    expr is a command so expr 2 * 2 will consider * to mean "all files in the current directory". This causes the expression to fail to evaluate unless you are in an empty directory with the failglob and nullglob options turned off.

                                                    +

                                                    Prefer rewriting it using the modern, POSIX standard arithmetic expansion $((..)). If you do not wish to do so, you can escape any characters like * to avoid the shell performing pathname expansion on them.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2307.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2307.html index 4415e6e..aca6c1a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2307.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2307.html @@ -1,6 +1,6 @@

                                                    'expr' expects 3+ arguments but sees 1. Make sure each operator/operand is a separate argument, and escape <>&|.

                                                    Problematic code

                                                    -
                                                    # | not escaped
                                                    +
                                                    # | not escaped
                                                     expr 1 | 2
                                                     # > not escaped
                                                     expr "$foo" >= "$bar"
                                                    @@ -11,7 +11,7 @@ 

                                                    Problematic code

                                                    expr "1 + 2"

                                                    Correct code

                                                    -
                                                    expr 16 \| 7
                                                    +
                                                    expr 16 \| 7
                                                     expr "$foo" \>= "$bar"
                                                     expr 1 + 2
                                                     
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2308.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2308.html index 8916d9a..3e15ca5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2308.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2308.html @@ -1,9 +1,9 @@

                                                    expr length has unspecified results. Prefer ${#var}.

                                                    -

                                                    or 'expr match' has unspecified results. Prefer 'expr str : regex'.
                                                    -or 'expr substr' has unspecified results. Prefer 'cut' or ${var#???}.
                                                    +

                                                    or 'expr match' has unspecified results. Prefer 'expr str : regex'.
                                                    +or 'expr substr' has unspecified results. Prefer 'cut' or ${var#???}.
                                                    or 'expr index' has unspecified results. Prefer x=${var%%[chars]*}; $((${#x}+1)).

                                                    Problematic code

                                                    -
                                                    # Find length of string
                                                    +
                                                    # Find length of string
                                                     length=$(expr length "$var")
                                                     
                                                     # Match string against regex
                                                    @@ -16,7 +16,7 @@ 

                                                    Problematic code

                                                    col2=$(expr substr "foo bar baz" 8 5)

                                                    Correct code

                                                    -
                                                    # Find length of string
                                                    +
                                                    # Find length of string
                                                     length=${#var}
                                                     
                                                     # Match string against regex
                                                    @@ -30,7 +30,7 @@ 

                                                    Correct code

                                                    col2="${str:7:5}" # Get substring by index (POSIX) -col2="$(printf 'foo bar baz\n' | cut -c 8-12)" +col2="$(printf 'foo bar baz\n' | cut -c 8-12)"

                                                    Rationale

                                                    You are using a expr with length, match, index, or substr. These forms did not make it into POSIX, and fail on platforms like MacOS and FreeBSD. Consider replacing them with portable equivalents:

                                                    @@ -40,26 +40,26 @@

                                                    match

                                                    can be trivially replaced with the POSIX form expr str : regex

                                                    index

                                                    if you only need a numerical index as part of trying to extract a piece of the string, consider replacing it with parameter expansion:

                                                    -
                                                    str="mykey=myvalue"
                                                    -key="${str%%=*}"    # Remove everything after first =, no index required
                                                    -value="${str#*=}"   # Remove everything before first =, no index required
                                                    +
                                                    str="mykey=myvalue"
                                                    +key="${str%%=*}"    # Remove everything after first =, no index required
                                                    +value="${str#*=}"   # Remove everything before first =, no index required
                                                     

                                                    otherwise, you can find the index of the first = using parameter expansion and string length:

                                                    -
                                                    str="mykey=myvalue"
                                                    -x=${str%%=*}     # Assign x="mystr"
                                                    +
                                                    str="mykey=myvalue"
                                                    +x=${str%%=*}     # Assign x="mystr"
                                                     index=$((${#x}+1))   # Add 1 to length of x
                                                     

                                                    substr

                                                    Extract a substring via character index is generally fragile. For example, in this example, any minor changes to the format, including just the version increasing from 8.9 to 8.10, will cause the following snippet to fail:

                                                    -
                                                    str="VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 15 2021 12:29:39)"
                                                    -version=$(expr substr "$str" 19 3)
                                                    +
                                                    str="VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 15 2021 12:29:39)"
                                                    +version=$(expr substr "$str" 19 3)
                                                     

                                                    Instead, consider a different approach:

                                                    -
                                                    x="${str%% (*}"     # Delete ` (` and everything after, giving "VIM - Vi IMproved 8.2"
                                                    -version="${x##* }"  # Delete everything before last space, giving "8.2"
                                                    +
                                                    x="${str%% (*}"     # Delete ` (` and everything after, giving "VIM - Vi IMproved 8.2"
                                                    +version="${x##* }"  # Delete everything before last space, giving "8.2"
                                                     
                                                     # Get the fifth word separated by spaces
                                                    -IFS=" " read -r _ _ _ _ version _ << EOF
                                                    +IFS=" " read -r _ _ _ _ version _ << EOF
                                                     $str
                                                     EOF
                                                     
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2309.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2309.html index 721b779..757d55c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2309.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2309.html @@ -1,21 +1,20 @@

                                                    -eq treats this as a variable. Use = to compare as string (or expand explicitly with $var)

                                                    Problematic code

                                                    -
                                                    read -p "Continue? [y/n] " var
                                                    +
                                                    read -p "Continue? [y/n] " var
                                                     [ "$var" -eq "n" ​] && exit 1
                                                     

                                                    Correct code

                                                    -
                                                    #read -p "Continue? [y/n] " var
                                                    +
                                                    #read -p "Continue? [y/n] " var
                                                     [ "$var" = "n" ​] && exit 1
                                                     

                                                    Rationale

                                                    ShellCheck found a string used as an argument to a numerical operator like -eq, -ne, -lt, -ge. Such strings will be treated as arithmetic expressions, meaning n will refer to a variable $n, and 24/12 will be evaluated into 2.

                                                    -

                                                    In the problematic example, the intention was instead to compare "n" as a string, so it should use the equivalent string operator instead, in this case =.

                                                    +

                                                    In the problematic example, the intention was instead to compare "n" as a string, so it should use the equivalent string operator instead, in this case =.

                                                    Exceptions

                                                    It is perfectly valid to use variables as operands. ShellCheck will not flag any value that is an unquoted variable name assigned in the script:

                                                    -
                                                    a=42; [[ "a" -eq 0 ]]  # Flagged due to quotes
                                                    +
                                                    a=42; [[ "a" -eq 0 ]]  # Flagged due to quotes
                                                           [[ b -eq 0 ]]    # Flagged due to not being assigned
                                                    -c=42; [[ c -eq 0 ]]    # Not flagged
                                                    -
                                                    +c=42; [[ c -eq 0 ]] # Not flagged

                                                    However, ShellCheck does not know whether you intended foo/bar to be division or a file path.

                                                    If you intended to divide $foo and $bar, you can either make it explicit with [[ $((foo/bar)) -ge 0 ]], or simply [[ignore]] the warning.

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2310.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2310.html index e5de011..227bee2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2310.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2310.html @@ -1,7 +1,7 @@

                                                    This function is invoked in an 'if' condition so set -e will be disabled. Invoke separately if failures should cause the script to exit.

                                                    (This warning is optional and must be explicitly enabled)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     #shellcheck enable=check-set-e-suppressed
                                                     
                                                     set -e
                                                    @@ -17,7 +17,7 @@ 

                                                    Problematic code

                                                    fi

                                                    Correct code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     #shellcheck enable=check-set-e-suppressed
                                                     
                                                     set -e
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2311.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2311.html
                                                    index 67ca713..7c7e0d8 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2311.html
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2311.html
                                                    @@ -1,6 +1,6 @@
                                                     

                                                    Bash implicitly disabled set -e for this function invocation because it's inside a command substitution. Add set -e; before it or enable inherit_errexit.

                                                    Problematic code

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     
                                                     #shellcheck enable=check-set-e-suppressed
                                                     set -e
                                                    @@ -15,7 +15,7 @@ 

                                                    Problematic code

                                                    echo "Successfully deployed $version"

                                                    Correct code

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     
                                                     #shellcheck enable=check-set-e-suppressed
                                                     set -e
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2312.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2312.html
                                                    index 7b15f73..b9fa3c9 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2312.html
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2312.html
                                                    @@ -1,24 +1,31 @@
                                                     

                                                    Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).

                                                    This is an optional suggestion enabled with shellcheck -o check-extra-masked-returns or enable=check-extra-masked-returns in a # shellcheck directive or .shellcheckrc.

                                                    Problematic code

                                                    -
                                                    set -e
                                                    +
                                                    set -e
                                                     cd "$(get_chroot_dir)/etc"
                                                    -tar xf "$config"
                                                    +tar xf "${config}"
                                                     

                                                    Correct code

                                                    -
                                                    set -e
                                                    +
                                                    set -e
                                                     dir="$(get_chroot_dir)"
                                                    -cd "$dir/etc"
                                                    -tar xf "$config"
                                                    +cd "${dir}/etc"
                                                    +tar xf "${config}"
                                                    +
                                                    +

                                                    Correct code: (with correction)

                                                    +
                                                    set -e
                                                    +dir="$(get_chroot_dir)"
                                                    +[[ -d "${dir}" ]] || exit 1
                                                    +cd "${dir}/etc"
                                                    +tar xf "${config}"
                                                     

                                                    Rationale

                                                    In the problematic example, the exit code for get_chroot_dir is ignored because it is used in a command substitution in the argument of another command.

                                                    -

                                                    If the command shows error: Can't determine chroot and exits with failure without outputting a directory, then the command being run will be cd "/etc" and the script will proceed to overwrite the host system's configuration.

                                                    +

                                                    If the command shows error: Can't determine chroot and exits with failure without outputting a directory, then the command being run will be cd "/etc" and the script will proceed to overwrite the host system's configuration.

                                                    By assigning it to a variable first, the exit code of the command will propagate into the exit code of the assignment, so that it can be checked explicitly with if or implicitly with set -e.

                                                    Exceptions

                                                    If you don't care about the command's exit status, or already handle it through a side channel like <(cmd; echo $? > status), then you can either [[ignore]] the suggestion with a directive, or use || true (or || :) to suppress it.

                                                    -

                                                    Note that this suggestion is optional, and only shows up when explicitly enabled in the script, on the command line, or in a .shellcheckrc.

                                                    Related resources

                                                    +

                                                    https://mywiki.wooledge.org/BashPitfalls#cmd1_.26.26_cmd2_.7C.7C_cmd3

                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2313.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2313.html index 640b8c2..8eaf2b0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2313.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2313.html @@ -1,9 +1,9 @@

                                                    Quote array indices to avoid them expanding as globs.

                                                    Problematic code

                                                    -
                                                    read -r foo[index]
                                                    +
                                                    read -r foo[index]
                                                     

                                                    Correct code

                                                    -
                                                    read -r "foo[index]"
                                                    +
                                                    read -r "foo[index]"
                                                     

                                                    Rationale

                                                    ShellCheck found an array element passed to read, where the [] was not quoted. This means the array index [index] will be treated as a glob range, and the word may be replaced or trigger failglob.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.html index 3edf49d..53548b2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.html @@ -1,6 +1,6 @@ -

                                                    In bats, ! does not cause a test failure

                                                    +

                                                    In bats, ! does not cause a test failure.

                                                    Problematic code

                                                    -
                                                    #!/usr/bin/env bats
                                                    +
                                                    #!/usr/bin/env bats
                                                     
                                                     @test "test" {
                                                         # ... code
                                                    @@ -9,7 +9,7 @@ 

                                                    Problematic code

                                                    }

                                                    Correct code

                                                    -
                                                    #!/usr/bin/env bats
                                                    +
                                                    #!/usr/bin/env bats
                                                     
                                                     @test "test" {
                                                         # ... code
                                                    @@ -31,7 +31,8 @@ 

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.json index 48a22bb..0316b33 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2314.json @@ -1,5 +1,5 @@ { - "title": "In bats, ! does not cause a test failure", + "title": "In bats, ! does not cause a test failure.", "type": "CODE_SMELL", "status": "ready", "remediation": { @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.html index 07fdd23..3c5f427 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.html @@ -1,6 +1,6 @@ -

                                                    In bats, ! does not cause a test failure. Fold the ! into the conditional!

                                                    +

                                                    In bats, ! does not cause a test failure. Fold the ! into the conditional!

                                                    Problematic code

                                                    -
                                                    #!/usr/bin/env bats
                                                    +
                                                    #!/usr/bin/env bats
                                                     
                                                     @test "test" {
                                                         # ... code
                                                    @@ -9,7 +9,7 @@ 

                                                    Problematic code

                                                    }

                                                    Correct code

                                                    -
                                                    #!/usr/bin/env bats
                                                    +
                                                    #!/usr/bin/env bats
                                                     
                                                     @test "test" {
                                                         # ... code
                                                    @@ -30,7 +30,8 @@ 

                                                    Related resources

                                                  • SC2314: In bats, ! does not cause a test failure (for non-conditionals)
                                                  • SC2251: This ! is not on a condition and skips errexit
                                                  • Stackoverflow: Why do I need parenthesis In bash set -e and negated return code
                                                  • -
                                                  • bash manpage (look at trap [-lp] [[arg] sigspec ...]):

                                                    The ERR trap is not executed [...] if the command's return value is being inverted via !

                                                    +
                                                  • bash manpage (look at trap [-lp] [[arg] sigspec ...]):
                                                    +

                                                    The ERR trap is not executed [...] if the command's return value is being inverted via !

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.json index febb90a..2207b86 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2315.json @@ -10,5 +10,5 @@ "shell", "convention" ], - "defaultSeverity": "Major" + "defaultSeverity": "Info" } \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.html new file mode 100644 index 0000000..1502190 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.html @@ -0,0 +1,26 @@ +

                                                  This applies local to the variable named readonly, which is probably not what you want. Use a separate command or the appropriate declare options instead.

                                                  +

                                                  Problematic code

                                                  +
                                                  local readonly foo=3
                                                  +readonly export bar=4
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  local foo=3
                                                  +readonly foo
                                                  +
                                                  +readonly bar=4
                                                  +export bar
                                                  +
                                                  +

                                                  or

                                                  +
                                                  declare -r foo=3
                                                  +declare -rx bar=4
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  In most languages, declaration modifiers like public/static/const are keywords and you can apply multiple to any declaration.

                                                  +

                                                  In shell scripting they are instead command names, and anything after them is an argument. This means that readonly local foo will create two readonly variables: local, and foo. Neither will be local.

                                                  +

                                                  Instead, either use multiple commands, or use a single declare command with appropriate flags (declare will automatically make a variable local when invoked in a function, unless -g is passed to explicitly make it global).

                                                  +

                                                  Exceptions

                                                  +

                                                  If you want to name your variable local, you can quote it as in readonly "local" to make your intention clear to ShellCheck and other humans.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.json new file mode 100644 index 0000000..f407f59 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2316.json @@ -0,0 +1,14 @@ +{ + "title": "This applies local to the variable named readonly, which is probably not what you want. Use a separate command or the appropriate declare options instead.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Blocker" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.html new file mode 100644 index 0000000..2cc5c5e --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.html @@ -0,0 +1,57 @@ +

                                                  Command appears to be unreachable. Check usage (or ignore if invoked indirectly).

                                                  +

                                                  Problematic code

                                                  +
                                                  usage() {
                                                  +  echo >&2 "Usage: $0 -i input"
                                                  +  exit 1
                                                  +}     
                                                  +if [ "$1" = "--help" ]
                                                  +then
                                                  +  usage
                                                  +  exit 0   # Unreachable
                                                  +fi
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  usage() {
                                                  +  echo >&2 "Usage: $0 -i input"
                                                  +}     
                                                  +if [ "$1" = "--help" ]
                                                  +then
                                                  +  usage
                                                  +  exit 0
                                                  +fi
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  The problematic code wanted to exit with success if the user explicitly asked for --help. However, since the usage function already had an exit 1, this statement could never run.

                                                  +

                                                  One possible solution is to change usage() to only echo, and let callers be responsible for exiting.

                                                  +

                                                  Exceptions

                                                  +

                                                  ShellCheck may incorrectly believe that code is unreachable if it's invoked by variable name or in a trap. In such a case, please [[Ignore]] the message.

                                                  +

                                                  Note in particular that since unreachable commands may come in clusters, it's useful to use ShellCheck's filewide or functionwide ignore directives. A disable directive before a function ignores the entire function:

                                                  +
                                                  #!/bin/bash
                                                  +...
                                                  +# shellcheck disable=SC2317  # Don't warn about unreachable commands in this function
                                                  +start() {
                                                  +  echo Starting
                                                  +  /etc/init.d/foo start
                                                  +}
                                                  +"$1"
                                                  +exit 0
                                                  +
                                                  +

                                                  A disable directive after the shebang, before any commands, will ignore the entire file:

                                                  +
                                                  #!/bin/bash
                                                  +# Test script #1
                                                  +# shellcheck disable=SC2317  # Don't warn about unreachable commands in this file
                                                  +
                                                  +echo "Temporarily disabled"
                                                  +exit 0
                                                  +
                                                  +run-test1
                                                  +run-test2
                                                  +run-test3
                                                  +
                                                  +

                                                  Defined functions are assumed to be reachable when the script ends (not exits) since another file may source and invoke them.

                                                  +

                                                  More Problematic Code

                                                  +

                                                  You have defined two functions in the same file you are sourcing whose names are the same but defined differently within their bodies. Then shellcheck will state that every line of the body of the earlier seen function definition will be unreachable which is how bash would operate when sourcing the file. It unclear what shellcheck would output if the earlier definition appeared in a difference file that was seen first. Apparently doing a quick test. It does NOT notice.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.json new file mode 100644 index 0000000..722e53e --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2317.json @@ -0,0 +1,14 @@ +{ + "title": "Command appears to be unreachable. Check usage (or ignore if invoked indirectly).", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.html new file mode 100644 index 0000000..177618c --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.html @@ -0,0 +1,20 @@ +

                                                  This assignment is used again in this declare, but won't have taken effect. Use two declares.

                                                  +

                                                  (or local, typeset, readonly, export)

                                                  +

                                                  Problematic code

                                                  +
                                                  declare -i first=$1 current=$first
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  declare -i first=$1
                                                  +declare -i current=$first
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  When assigning variables via a command, such as declare, typeset, local etc, the expansion of all arguments happen before all assignments. This means that you can't have a variable assigned and then referenced in the same command.

                                                  +

                                                  In the example, if $1 is 42, the arguments will first be expanded in the current environment into -i first=42 current=. They will then be passed to declare which will perform the assignments.

                                                  +

                                                  To correctly set current=$first so that it uses the new value of first, use two separate commands as shown.

                                                  +

                                                  Note that this only applies when assigning via commands, because arguments are always expanded before commands are invoked. If assigning without a command, as in first=$1 current=$first, it will work as expected.

                                                  +

                                                  Exceptions

                                                  +

                                                  If you want to reference the value as it existed before the command, e.g. if swapping variables with declare x=$y y=$x, you can ignore this message. However, consider rewriting it anyways for the benefit of any humans reading the code.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.json new file mode 100644 index 0000000..d3b0995 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2318.json @@ -0,0 +1,14 @@ +{ + "title": "This assignment is used again in this declare, but won't have taken effect. Use two declares.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.html new file mode 100644 index 0000000..5776197 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.html @@ -0,0 +1,34 @@ +

                                                  This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.

                                                  +

                                                  Problematic code

                                                  +
                                                  mycommand
                                                  +if [ $? -ne 0 ] && [ $? -ne 14 ]
                                                  +then
                                                  +  echo "Command failed"
                                                  +fi
                                                  +
                                                  +

                                                  or

                                                  +
                                                  mycommand
                                                  +[ $? -gt 0 ] && exit $?
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  mycommand
                                                  +ret=$?
                                                  +if [ $ret -ne 0 ] && [ $ret -ne 14 ]
                                                  +then
                                                  +  echo "Command failed"
                                                  +fi
                                                  +
                                                  +

                                                  or

                                                  +
                                                  mycommand || exit $?
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  ShellCheck found a $? that always refers to a condition like [ .. ], [[ .. ]], or test.

                                                  +

                                                  This most commonly happens when trying to inspect $? before doing something with it, such as inspecting it again or exiting with it, without realizing that any such inspection will also overwrite $?.

                                                  +

                                                  In the first problematic example, [ $? -ne 14 ] will never be true because it only runs after [ $? -ne 0 ] has modified $? to be 0. The solution is to assign $? from mycommand to a variable so that the variable can be inspected repeatedly.

                                                  +

                                                  In the second problematic example, exit $? will always exit 0, because it only runs if [ $? -gt 0 ] returns success and sets $? to 0. The solution could again be to assign $? to a variable first, or (as shown) use mycommand || exit $? as there is no condition to overwrite $?.

                                                  +

                                                  Exceptions

                                                  +

                                                  None. Note that ShellCheck does not warn if the usage of $? after [ .. ] is unconditional, as in [ -d dir ]; return $?.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.json new file mode 100644 index 0000000..ddd4bfd --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2319.json @@ -0,0 +1,14 @@ +{ + "title": "This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.html new file mode 100644 index 0000000..ba00ccf --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.html @@ -0,0 +1,28 @@ +

                                                  This $? refers to echo/printf, not a previous command. Assign to variable to avoid it being overwritten.

                                                  +

                                                  Problematic code

                                                  +
                                                  mycommand
                                                  +echo "Command exited with $?"
                                                  +if [ $? -ne 0 ]
                                                  +then
                                                  +  echo "Failed"
                                                  +fi
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  mycommand
                                                  +ret=$?
                                                  +echo "Command exited with $ret"
                                                  +if [ $ret -ne 0 ]
                                                  +then
                                                  +  echo "Failed"
                                                  +fi
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  ShellCheck found a $? that always refers to echo or printf.

                                                  +

                                                  This most commonly happens when trying to show $? before doing something with it, without realizing that any such action will also overwrite $?.

                                                  +

                                                  In the problematic example, echo "Command exited with $?" was intended to show the exit code before acting on it, but the act of showing $? also overwrote it, so the condition is always false. The solution is to assign $? to a variable first, so that it can be used repeatedly.

                                                  +

                                                  Exceptions

                                                  +

                                                  If you intentionally refer to echo to get the result of a write, you can ignore this message. Alternatively, write it out as in if echo $$ > "$pidfile"; then status=0; else status=1; fi

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.json new file mode 100644 index 0000000..7fb6985 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2320.json @@ -0,0 +1,14 @@ +{ + "title": "This $? refers to echo\/printf, not a previous command. Assign to variable to avoid it being overwritten.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.html new file mode 100644 index 0000000..605d4bd --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.html @@ -0,0 +1,15 @@ +

                                                  Array indices are already arithmetic contexts. Prefer removing the $(( and )).

                                                  +

                                                  Problematic code

                                                  +
                                                  values[$((i+1))]=1
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  values[i+1]=1
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  In indexed arrays (but not associative ones), the array index is already an arithmetic context. There is no point or value in wrapping it in an additional, explicit $((..)).

                                                  +

                                                  Exceptions

                                                  +

                                                  If ShellCheck has failed to realize that your array is associative, or if you for stylistic reasons prefer the redundancy, you can ignore this message.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.json new file mode 100644 index 0000000..5ec69e5 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2321.json @@ -0,0 +1,14 @@ +{ + "title": "Array indices are already arithmetic contexts. Prefer removing the $(( and )).", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.html new file mode 100644 index 0000000..af759fa --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.html @@ -0,0 +1,15 @@ +

                                                  In arithmetic contexts, ((x)) is the same as (x). Prefer only one layer of parentheses.

                                                  +

                                                  Problematic code

                                                  +
                                                  value=$(( ((offset + index)) * 512 )) 
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  value=$(( (offset + index) * 512 ))
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  ShellCheck found doubly nested parentheses in an arithmetic expression. While the syntax for an arithmetic expansion is $((..)), this does not imply that parentheses in the expression should also be doubled. (x), ((x)), and (((x))) are all identical, so you might as well use only one layer of parentheses.

                                                  +

                                                  Exceptions

                                                  +

                                                  This is a stylistic suggestion. If you prefer keeping both parentheses, you can [[ignore]] this message.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.json new file mode 100644 index 0000000..fcfeac4 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2322.json @@ -0,0 +1,14 @@ +{ + "title": "In arithmetic contexts, ((x)) is the same as (x). Prefer only one layer of parentheses.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.html new file mode 100644 index 0000000..43212c9 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.html @@ -0,0 +1,19 @@ +

                                                  a[(x)] is the same as a[x]. Prefer not wrapping in additional parentheses.

                                                  +

                                                  Similarly $(( (x) )) is the same as $(( x )), (( (x) )) is the same as (( x )).

                                                  +

                                                  Problematic code

                                                  +
                                                  array[(x+1)]=val
                                                  +echo $(( (x+1) ))
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  array[x+1]=val
                                                  +echo $((x+1))
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  ShellCheck found an entire arithmetic expression wrapped in parentheses. This does not serve a purpose since the expression is already clearly delimited by the construct it's in, such as array[..]= or $((..)) in the example.

                                                  +

                                                  Note: ShellCheck does not warn about redundant parentheses in subexpressions, such as (a*b)+c. Feel free to use parentheses to clarify the order of operations any way you'd like. ShellCheck only emits this suggestion when the entire expression is wrapped twice.

                                                  +

                                                  Exceptions

                                                  +

                                                  If you prefer having an extra layer of parentheses for stylistic reasons, you can [[ignore]] this message.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.json new file mode 100644 index 0000000..c7c7e77 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2323.json @@ -0,0 +1,14 @@ +{ + "title": "a[(x)] is the same as a[x]. Prefer not wrapping in additional parentheses.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.html new file mode 100644 index 0000000..bf3f361 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.html @@ -0,0 +1,28 @@ +

                                                  var+=1 will append, not increment. Use (( var += 1 )), declare -i var, or quote number to silence.

                                                  +

                                                  Problematic code

                                                  +
                                                  var=2 n=3
                                                  +var+=$n
                                                  +
                                                  +

                                                  Correct code

                                                  +

                                                  In bash/ksh, use an (( arithmetic context ))

                                                  +
                                                  (( var += n ))
                                                  +
                                                  +

                                                  or declare the variable as an integer type:

                                                  +
                                                  declare -i var=2
                                                  +n=4
                                                  +var+=$n
                                                  +
                                                  +

                                                  For POSIX sh, use an $((arithmetic expansion)):

                                                  +
                                                  var=$((var+n))
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  The problematic code attempts to add 2 and 3 to get 5.

                                                  +

                                                  Instead, += on a string variable will concatenate, so the result is 23.

                                                  +

                                                  Exceptions

                                                  +

                                                  If you do want to concatenate a number, for example to append trailing zeroes, you can silence the warning by quoting the number:

                                                  +
                                                  var+="000"
                                                  +
                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.json new file mode 100644 index 0000000..da3fc77 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2324.json @@ -0,0 +1,14 @@ +{ + "title": "var+=1 will append, not increment. Use (( var += 1 )), declare -i var, or quote number to silence.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.html new file mode 100644 index 0000000..826beb7 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.html @@ -0,0 +1,21 @@ +

                                                  Multiple ! in front of pipelines are a bash/ksh extension. Use only 0 or 1.

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/sh
                                                  +! ! true
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  #!/bin/sh
                                                  +true
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  POSIX (and Dash) does not allow multiple ! pipeline negations in a row. It's also logically unnecessary.

                                                  +

                                                  Use either zero or one !.

                                                  +

                                                  Exceptions

                                                  +

                                                  Scripts whose shebang declares it will run with Ksh and Bash will not trigger this warning.

                                                  +

                                                  If you really want to negate multiple times on POSIX or Dash, e.g. to normalize exit codes to 0 or 1, use cmd || false or a command group:

                                                  +
                                                  ! { ! true; } 
                                                  +
                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.json new file mode 100644 index 0000000..89ab5fb --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2325.json @@ -0,0 +1,14 @@ +{ + "title": "Multiple ! in front of pipelines are a bash\/ksh extension. Use only 0 or 1.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Blocker" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.html new file mode 100644 index 0000000..45df720 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.html @@ -0,0 +1,21 @@ +

                                                  ! is not allowed in the middle of pipelines. Use command group as in cmd | { ! cmd; } if necessary.

                                                  +

                                                  Problematic code

                                                  +
                                                  cat | ! tee /dev/full
                                                  +
                                                  +

                                                  Correct code

                                                  +

                                                  Either negate the entire pipeline (this is equivalent unless pipefail is set):

                                                  +
                                                  ! cat | tee /dev/full
                                                  +
                                                  +

                                                  Or use a command group to negate a single stage:

                                                  +
                                                  cat | { ! tee /dev/full; }
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  POSIX specifies that a status negation operator ! is only used to negate the status of an entire pipeline, not individual stages.

                                                  +

                                                  By default the status of a pipeline is that of the last command, so use ! in front of the pipeline to negate as necessary.

                                                  +

                                                  If you have set the option pipefail to OR the status of each stage together, and want to negate the status of only a single stage, you can use negate inside a { ! command group; }.

                                                  +

                                                  Exceptions

                                                  +

                                                  Ksh supports ! in front of individual pipeline stages. ShellCheck does not warn when the shebang declares that the script will run with Ksh.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.json new file mode 100644 index 0000000..5a1ccbe --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2326.json @@ -0,0 +1,14 @@ +{ + "title": "! is not allowed in the middle of pipelines. Use command group as in cmd | { ! cmd; } if necessary.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Blocker" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.html new file mode 100644 index 0000000..2e50338 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.html @@ -0,0 +1,2 @@ +

                                                  This command substitution will be empty because the command's output gets redirected away.

                                                  +

                                                  See companion warning [[SC2328]].

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.json new file mode 100644 index 0000000..783b009 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2327.json @@ -0,0 +1,14 @@ +{ + "title": "This command substitution will be empty because the command's output gets redirected away.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.html new file mode 100644 index 0000000..b6832ed --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.html @@ -0,0 +1,24 @@ +

                                                  This redirection takes output away from the command substitution.

                                                  +

                                                  (and companion warning "This command substitution will be empty because the command's output gets redirected away" which points to the relevant command substitution)

                                                  +

                                                  Problematic code

                                                  +
                                                  var=$(tr -d ':' < input.txt > output.txt)
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  # If the output should be captured INSTEAD OF being written to file
                                                  +var=$(tr -d ':' < input.txt)
                                                  +
                                                  +# If the output should be captured IN ADDITION to being written to file
                                                  +var=$(tr -d ':' < input.txt | tee output.txt)
                                                  +
                                                  +# If the output should NOT BE captured at all
                                                  +tr -d ':' < input.txt > output.txt
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  ShellCheck has found a command substitution ($(..), `..`) that appears to never capture any output because the command's output is being redirected.

                                                  +

                                                  Decide whether you want the output to be captured (by removing the redirection), to go into wherever it's redirected (by not running the command in a command substitution), or both (by using tee to copy the output to both file and stdout).

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.json new file mode 100644 index 0000000..d3241a2 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2328.json @@ -0,0 +1,14 @@ +{ + "title": "This redirection takes output away from the command substitution.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Blocker" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.html new file mode 100644 index 0000000..9dd0880 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.html @@ -0,0 +1,25 @@ +

                                                  This function is never invoked. Check usage (or ignored if invoked indirectly).

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/sh
                                                  +f() {
                                                  +  echo "Hello World"
                                                  +}
                                                  +exit
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  #!/bin/sh
                                                  +f() {
                                                  +  echo "Hello World"
                                                  +}
                                                  +f
                                                  +exit
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  ShellCheck found a function that goes out of scope before it's ever invoked. Verify that the function is called. It could be misspelled, or its invocation could also be unreachable (as in f() { ..; }; exit; f).

                                                  +

                                                  Note that if the example script did not end in exit, this warning would not be emitted. This is because the function could be invoked by another script that sources it.

                                                  +

                                                  Exceptions

                                                  +

                                                  ShellCheck is currently bad at figuring out functions that are invoked via trap. In such cases, please [[ignore]] the message with a directive.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.json new file mode 100644 index 0000000..99b1492 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2329.json @@ -0,0 +1,14 @@ +{ + "title": "This function is never invoked. Check usage (or ignored if invoked indirectly).", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.html new file mode 100644 index 0000000..5bc96a5 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.html @@ -0,0 +1,25 @@ +

                                                  BusyBox [[ .. ]] does not support glob matching. Use a case statement.

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/busybox sh
                                                  +if [[ $1 == https:* ]]
                                                  +then
                                                  +  echo "Using URL $1"
                                                  +fi
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  #!/bin/busybox sh
                                                  +case "$1" in
                                                  +  https:*)
                                                  +    echo "Using URL $1" 
                                                  +    ;;
                                                  +esac
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  You are using [[ .. ]] in BusyBox sh to match against a glob pattern. This is supported in Bash and Ksh, but not in BusyBox.

                                                  +

                                                  Rewrite the match to use a case statement instead.

                                                  +

                                                  Exceptions

                                                  +

                                                  None.

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.json new file mode 100644 index 0000000..4a55993 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC2330.json @@ -0,0 +1,14 @@ +{ + "title": "BusyBox [[ .. ]] does not support glob matching. Use a case statement.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Blocker" +} \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3001.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3001.html index b637a42..6dc5c65 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3001.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3001.html @@ -1,7 +1,7 @@

                                                  In POSIX sh, process substitution is undefined.

                                                  -

                                                  (or "In dash, ... is not supported." when using dash)

                                                  +

                                                  (or "In dash, ... is not supported." when using dash)

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   while IFS= read -r n
                                                   do
                                                     sum=$((sum+n))
                                                  @@ -9,14 +9,14 @@ 

                                                  Problematic code

                                                  Correct code

                                                  The easiest fix is to switch to a shell that does support process substitution, by changing the shebang to #!/bin/bash or ksh.

                                                  -
                                                  #!/bin/bash
                                                  +
                                                  #!/bin/bash
                                                   while IFS= read -r n
                                                   do
                                                     sum=$((sum+n))
                                                   done < <(program)
                                                   

                                                  Alternatively, process substitution can often be replaced with temporary files:

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   tmp="$(mktemp)"
                                                   program > "$tmp"
                                                   while IFS= read -r n
                                                  @@ -27,7 +27,7 @@ 

                                                  Correct code

                                                  If streaming is important, the temporary file can be a named pipe, and the producer or consumer can be run as a background job.

                                                  If the reading command accepts input from standard input, the process substitution can be replaced with a regular pipe:

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   program | while IFS= read -r n
                                                   do
                                                     sum=$((sum+n))
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3002.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3002.html
                                                  index 8ceecaa..3c42b44 100644
                                                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3002.html
                                                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3002.html
                                                  @@ -1,12 +1,12 @@
                                                   

                                                  In POSIX sh, extglob is undefined.

                                                  -

                                                  (or "In dash, ... is not supported." when using dash)

                                                  +

                                                  (or "In dash, ... is not supported." when using dash)

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   rm !(*.hs)
                                                   

                                                  Correct code

                                                  Either switch the shebang to a shell that does support extglob, like bash or ksh, or rewrite in terms of a loop with a case or grep match:

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   for file in *
                                                   do
                                                     case "$file" in
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3003.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3003.html
                                                  index aa12ae1..cfcc9b6 100644
                                                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3003.html
                                                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3003.html
                                                  @@ -1,26 +1,26 @@
                                                  -

                                                  In POSIX sh, $'..' is undefined.

                                                  +

                                                  In POSIX sh, $'..' is undefined.

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  -IFS=$' \t\n'
                                                  +
                                                  #!/bin/sh
                                                  +IFS=$' \t\n'
                                                   

                                                  Correct code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   # Note: \n can not be last, or it will be stripped by $()
                                                  -IFS=$(printf ' \n\t')
                                                  +IFS=$(printf ' \n\t')
                                                   

                                                  or

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   # Trailing linefeed added literally
                                                  -IFS="$(printf ' \t')
                                                  +IFS="$(printf ' \t')
                                                   "
                                                   

                                                  or

                                                  -
                                                  #!/bin/bash
                                                  +
                                                  #!/bin/bash
                                                   # Bash supports this
                                                  -IFS=$' \t\n'
                                                  +IFS=$' \t\n'
                                                   

                                                  Rationale

                                                  -

                                                  You are using the interpolated string Bashism $'..' in a script that declares itself as POSIX sh (e.g. via #!/bin/sh).

                                                  +

                                                  ANSI-C quoting, $'..', is a bash extension, which is not supported by POSIX sh.

                                                  To ensure the script runs correctly on other systems, either switch to Bash, or rewrite it in a POSIX compatible way.

                                                  This can generally done via printf as in the example. Be careful about strings with trailing linefeeds, as a $(command substitution) will strip them.

                                                  Exceptions

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.html index 486cfa2..620704d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.html @@ -1,21 +1,21 @@ -

                                                  In POSIX sh, $".." is undefined

                                                  +

                                                  In POSIX sh, $".." is undefined

                                                  Problematic code

                                                  -
                                                  printf $"Hello, %s\n" "$USER"
                                                  +
                                                  printf $"Hello, %s\n" "$USER"
                                                   

                                                  Correct code

                                                  -
                                                  # Check if gettext exists
                                                  +
                                                  # Check if gettext exists
                                                   if ! type gettext > /dev/null 2>&1
                                                   then
                                                     # If not, create a dummy function that returns the input verbatim
                                                  -  gettext() { printf '%s' "$1"; }
                                                  +  gettext() { printf '%s' "$1"; }
                                                   fi
                                                   
                                                   # Use gettext to get a translated version of the string
                                                  -printf "$(gettext 'Hello, %s\n')" "$USER"
                                                  +printf "$(gettext 'Hello, %s\n')" "$USER"
                                                   

                                                  Rationale

                                                  -

                                                  Bash allows using $"..." to easily get a translated string according to the current locale. This is powered by the gettext infrastructure.

                                                  -

                                                  Neither $".." nor the gettext infrastructure or the gettext command is POSIX. A script targeting sh may choose to invoke gettext if the system has it (such as GNU or Solaris), or use the translated string verbatim if it doesn't (such as macOS or FreeBSD).

                                                  +

                                                  Bash allows using $"..." to easily get a translated string according to the current locale. This is powered by the gettext infrastructure.

                                                  +

                                                  Neither $".." nor the gettext infrastructure or the gettext command is POSIX. A script targeting sh may choose to invoke gettext if the system has it (such as GNU or Solaris), or use the translated string verbatim if it doesn't (such as macOS or FreeBSD).

                                                  Exceptions

                                                  None.

                                                  Related resources

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.json index 0935dd1..f8a08f9 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3004.json @@ -1,5 +1,5 @@ { - "title": "In POSIX sh, $'..' is undefined", + "title": "In POSIX sh, $".." is undefined", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.html index 097af2c..18a0a0c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.html @@ -1 +1,23 @@ -

                                                  arithmetic for loops are not supported in dash and undefined in POSIX sh.

                                                  \ No newline at end of file +

                                                  In POSIX sh, arithmetic for loops are undefined.

                                                  +

                                                  Problematic code

                                                  +
                                                  for ((i=0; i<10; i++))
                                                  +do 
                                                  +  echo "$i"
                                                  +done
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  i=0
                                                  +while [ "$i" -lt 10 ]
                                                  +do 
                                                  +  echo "$i"
                                                  +  i=$((i+1))
                                                  +done
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  C-style arithmetic for loops are a Ksh/Bash feature that's not supported by POSIX sh or dash. Use a while loop with separate initialization and incrementing instead.

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.json index 0548ac9..b19ff6f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3005.json @@ -1,5 +1,5 @@ { - "title": "arithmetic for loops are not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, arithmetic for loops are undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3006.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3006.html index b6640f3..b722d68 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3006.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3006.html @@ -1,6 +1,6 @@ -

                                                  In POSIX sh, standalone ((..)) is undefined.

                                                  +

                                                  In POSIX sh, standalone ((..)) is undefined.

                                                  Problematic code

                                                  -
                                                  variable=1
                                                  +
                                                  variable=1
                                                   if ((variable)); then
                                                     echo variable is not zero
                                                   fi
                                                  @@ -8,7 +8,7 @@ 

                                                  Problematic code

                                                  Correct code

                                                  bash supports standalone ((..)) natively.

                                                  For POSIX compliance, use

                                                  -
                                                  variable=1
                                                  +
                                                  variable=1
                                                   if [ "${variable}" -ne 0 ]; then
                                                     echo variable is not zero
                                                   fi
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.html
                                                  index 809eb03..292538b 100644
                                                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.html
                                                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.html
                                                  @@ -1 +1,15 @@
                                                  -

                                                  $[..] in place of $((..)) is not supported in dash and undefined in POSIX sh.

                                                  \ No newline at end of file +

                                                  In POSIX sh, $[..] in place of $((..)) is undefined.

                                                  +

                                                  Problematic code

                                                  +
                                                  var=$[1 + 2]
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  var=$((1 + 2))
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash. In this case, $[ .. ] can easily be replaced by the POSIX standard arithmetic expansion $(( .. )).

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.json index fffcf98..5bdd0dc 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3007.json @@ -1,5 +1,5 @@ { - "title": "$[..] in place of $((..)) is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, $[..] in place of $((..)) is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.html index 8e85bd0..af93a7a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.html @@ -1 +1,27 @@ -

                                                  select loops are not supported in dash and undefined in POSIX sh.

                                                  \ No newline at end of file +

                                                  In POSIX sh, select loops are undefined.

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/sh
                                                  +echo "Select file to show"
                                                  +select input in *
                                                  +do
                                                  +  ls -l "$input"
                                                  +done
                                                  +
                                                  +

                                                  Correct code

                                                  +

                                                  Either switch to bash:

                                                  +
                                                  #!/usr/bin/env bash
                                                  +echo "Select file to show"
                                                  +select input in *
                                                  +do
                                                  +  ls -l "$input"
                                                  +done
                                                  +
                                                  +

                                                  Or write your own menu.

                                                  +

                                                  Rationale

                                                  +

                                                  Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash. There is no simple substitution for a select loop, so either write your own using compatible constructs like for loops, or switch to a shell that supports them.

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.json index 0315b1f..33e67f1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3008.json @@ -1,5 +1,5 @@ { - "title": "select loops are not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, select loops are undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.html index 531f479..22ed7d1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.html @@ -1 +1,22 @@ -

                                                  brace expansion is not supported in dash and undefined in POSIX sh.

                                                  \ No newline at end of file +

                                                  In POSIX sh, brace expansion is undefined.

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/sh
                                                  +
                                                  +for i in {1..5}; do ...
                                                  +
                                                  +

                                                  Here, $i expands to {1..5}. It does not expand to the sequence 1 2 3 4 5

                                                  +

                                                  Correct code

                                                  +
                                                  #!/bin/sh
                                                  +i=1
                                                  +# while $i ≤ 5 ...
                                                  +while [ "${i}" -le 5 ]; do ...
                                                  +
                                                  +  # something that will occur 5 times
                                                  +
                                                  +  i=$((i + 1))
                                                  +done
                                                  +
                                                  +

                                                  You can also use the seq command, e.g. seq 1 5, but seq is not a POSIX utility.

                                                  +

                                                  Rationale

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.json index 1981a44..e7b366d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3009.json @@ -1,5 +1,5 @@ { - "title": "brace expansion is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, brace expansion is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3010.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3010.html index 06dd6fa..ea80e93 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3010.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3010.html @@ -1,3 +1,2 @@ -
                                                  In POSIX sh, [[ ]] is undefined.
                                                  -
                                                  -

                                                  POSIX sh uses [ ] instead. To see the limitations/differences, view http://mywiki.wooledge.org/BashFAQ/031

                                                  +

                                                  In POSIX sh, [[ ]] is undefined.

                                                  +

                                                  POSIX sh uses [ ] and test instead. To see the limitations/differences, view http://mywiki.wooledge.org/BashFAQ/031

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.html index 2de71aa..9621fa2 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.html @@ -1 +1,19 @@ -

                                                  here-strings are not supported in dash and undefined in POSIX sh.

                                                  \ No newline at end of file +

                                                  In POSIX sh, here-strings are undefined.

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/sh
                                                  +wc <<< "$1"
                                                  +
                                                  +

                                                  Correct code

                                                  +
                                                  #!/bin/sh
                                                  +wc << EOF
                                                  +$1
                                                  +EOF
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash. In this specific case, the here-string can be replaced by a single-line here document as shown.

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.json index 74f617a..280ffe5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3011.json @@ -1,5 +1,5 @@ { - "title": "here-strings are not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, here-strings are undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3012.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3012.html index 3670dad..3a23377 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3012.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3012.html @@ -1,6 +1,6 @@

                                                  In POSIX sh, lexicographical \< is undefined.

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   x="aardvark"
                                                   y="zebra"
                                                   if [ $x \< $y ]
                                                  @@ -12,11 +12,11 @@ 

                                                  Correct code

                                                  First, make sure you wanted a lexicographical comparison (aka dictionary order), and not a numerical comparison.

                                                  Then to compare as string, you can use expr and make sure that the strings are not interpreted numerically by adding some non-numerical data to them. Here, an apostrophe is prepended:

                                                  #!/bin/sh
                                                  -x="aardvark"
                                                  -y="zebra"
                                                  -if expr "'$x" \< "'$y"   > /dev/null 
                                                  +x="aardvark"
                                                  +y="zebra"
                                                  +if expr "'$x" \< "'$y"   > /dev/null 
                                                   then
                                                  -  echo "$x comes before $y in the dictionary"
                                                  +  echo "$x comes before $y in the dictionary"
                                                   fi
                                                   

                                                  Rationale

                                                  @@ -26,5 +26,6 @@

                                                  Exceptions

                                                  If you know your sh will be e.g. dash, consider explicitly using #!/bin/dash.

                                                  Related resources

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3013.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3013.html index f1717cf..bfb323f 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3013.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3013.html @@ -1,6 +1,6 @@

                                                  In POSIX sh, -nt is undefined.

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   if [ Foo.java -nt Foo.class ]
                                                   then
                                                     javac Foo.java
                                                  @@ -8,21 +8,21 @@ 

                                                  Problematic code

                                                  Correct code

                                                  The easiest fix is to switch to a shell that does support -nt, like bash:

                                                  -
                                                  #!/bin/bash
                                                  +
                                                  #!/bin/bash
                                                   if [ Foo.java -nt Foo.class ]
                                                   then
                                                     javac Foo.java
                                                   fi
                                                   

                                                  Otherwise, find can be used:

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   if [ -n "$(find Foo.java -newer Foo.class)" ]
                                                   then
                                                     javac Foo.java
                                                   fi
                                                   

                                                  Rationale

                                                  -

                                                  test -nt is an extension in ksh, bash and dash, but it is not covered by POSIX.

                                                  +

                                                  test -nt is an extension in ksh, bash and dash, but it is not specified by POSIX.

                                                  Exceptions

                                                  If you only intend to target shells that supports this feature, you can change the shebang to a shell that guarantees support, or [[ignore]] this warning.

                                                  @@ -30,5 +30,9 @@

                                                  Exceptions

                                                  warnings.

                                                  Related resources

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3014.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3014.html index 8a49acd..5c793fe 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3014.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3014.html @@ -1,14 +1,14 @@

                                                  In POSIX sh, == in place of = is undefined.

                                                  -

                                                  (or "In dash, ... is not supported." when using dash)

                                                  +

                                                  (or "In dash, ... is not supported." when using dash)

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   if [ $1 == "-n" ]
                                                   then
                                                     dry_run=1
                                                   fi
                                                   

                                                  Correct code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   if [ $1 = "-n" ]
                                                   then
                                                     dry_run=1
                                                  @@ -24,5 +24,9 @@ 

                                                  Exceptions

                                                  warnings.

                                                  Related resources

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3015.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3015.html index 2017def..ee56915 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3015.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3015.html @@ -1,17 +1,32 @@ -

                                                  In POSIX sh, =~ regex matching is undefined.

                                                  +

                                                  In POSIX sh, =~ regex matching is undefined.

                                                  Problematic code

                                                  -
                                                  [ "$var" =~ .*foo[0-9]* ]
                                                  +
                                                  #!/bin/sh
                                                  +if [ "$var" =~ foo[0-9]+ ]; then
                                                  +  echo matched
                                                  +fi
                                                   

                                                  Correct code

                                                  -
                                                  expr "$var" : ".*foo[0-9]*" > /dev/null
                                                  +
                                                  #!/bin/sh
                                                  +# Use the x-hack to handle strings like '('.
                                                  +if expr "X$var" : 'X.*foo[0-9]\{1,\}' >/dev/null; then
                                                  +  echo matched
                                                  +fi
                                                  +
                                                  +

                                                  or

                                                  +
                                                  #!/bin/sh
                                                  +case $var in
                                                  +  *foo[0-9]*)
                                                  +    echo matched
                                                  +    ;;
                                                  +esac
                                                   

                                                  Rationale

                                                  -

                                                  You are using =~ in a script declared to be compatible with POSIX sh or Dash.

                                                  -

                                                  =~ is not a POSIX operator and is unlikely to outside [[ ]] in Bash and Ksh.

                                                  -

                                                  Use expr's : operator instead.

                                                  +

                                                  You are using =~ in a script declared to be compatible with POSIX sh or Dash, but =~ is not specified by POSIX and is unlikely to work outside [[ ]] in Bash and Ksh.

                                                  +

                                                  Use expr's : operator instead. It may be necessary to revise the regular expression because POSIX expr uses basic regular expressions anchored to the beginning of the string, as opposed to the unanchored extended regular expressions used by [[ str =~ re ]] in Bash and Ksh.

                                                  +

                                                  Alternately, use case if the matching can be done with shell patterns instead of regular expressions. This avoids the need for an external utility.

                                                  Exceptions

                                                  None

                                                  Related resources

                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.html index 571b713..e711b04 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.html @@ -1 +1,25 @@ -

                                                  unary -v (in place of [ -n "${var+x}" ]) is not supported in dash and undefined in POSIX sh.

                                                  \ No newline at end of file +

                                                  In POSIX sh, unary -v (in place of [ -n "${var+x}" ]) is undefined.

                                                  +

                                                  Problematic code

                                                  +
                                                  #!/bin/sh
                                                  +if [ -v STY ]
                                                  +then
                                                  +  echo "STY is set, you are using screen"
                                                  +fi
                                                  +
                                                  +

                                                  Correct code

                                                  +

                                                  Either switch to bash or ksh, or use:

                                                  +
                                                  #!/bin/sh
                                                  +if [ -n "${STY+x}" ]
                                                  +then
                                                  +  echo "STY is set, you are using screen"
                                                  +fi
                                                  +
                                                  +

                                                  Rationale

                                                  +

                                                  Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                  +

                                                  In this case, [ -v variable ], which checks if a variable is set, is not specified by POSIX. It can be replaced with the portable [ -n "${variable+x}" ], which uses the "alternative value if set" parameter expansion syntax to accomplish the same thing.

                                                  +

                                                  Exceptions

                                                  +

                                                  None

                                                  +

                                                  Related resources

                                                  +
                                                    +
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                  • +
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.json index 09f783f..134950d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3016.json @@ -1,5 +1,5 @@ { - "title": "unary -v (in place of [ -n \"${var+x}\" ]) is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, unary -v (in place of [ -n "${var+x}" ]) is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3017.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3017.html index e5fd361..9e65f73 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3017.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3017.html @@ -1,14 +1,14 @@

                                                  In POSIX sh, unary -a in place of -e is undefined.

                                                  -

                                                  (or "In dash, ... is not supported." when using dash)

                                                  +

                                                  (or "In dash, ... is not supported." when using dash)

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   if [ -a file.txt ]
                                                   then
                                                     echo "file.txt exists"
                                                   fi
                                                   

                                                  Correct code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   if [ -e file.txt ]
                                                   then
                                                     echo "file.txt exists"
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3018.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3018.html
                                                  index bf1ea5d..38a5f4b 100644
                                                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3018.html
                                                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3018.html
                                                  @@ -1,13 +1,13 @@
                                                  -

                                                  In POSIX sh, ++ is undefined.

                                                  -

                                                  (or "In dash, ... is not supported." when using dash)

                                                  +

                                                  In POSIX sh, ++ is undefined.

                                                  +

                                                  (or "In dash, ... is not supported." when using dash)

                                                  Problematic code

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   i=1
                                                   echo "$((i++))"
                                                   

                                                  Correct code

                                                  Write out the increment/decrement operation explicitly:

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   i=1
                                                   echo "$i"
                                                   i=$((i+1))
                                                  diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3019.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3019.html
                                                  index e2b7cd8..5e64300 100644
                                                  --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3019.html
                                                  +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3019.html
                                                  @@ -1,15 +1,15 @@
                                                   

                                                  In POSIX sh, exponentials are undefined.

                                                  Problematic code

                                                  ShellCheck noticed you're using **-notation with two asterisks to obtain an exponent's value. Some examples:

                                                  -
                                                  #!/bin/sh
                                                  +
                                                  #!/bin/sh
                                                   echo $((2 ** 3)) # Using `**` for an exponent is undefined in POSIX sh.
                                                   

                                                  Correct code

                                                  Other possibilities exist:

                                                  -
                                                  #!/bin/sh
                                                  -echo $((2 * 2 * 2))   # equivalent to `bash`'s `echo $((2 ** 3))`
                                                  -echo '2 ^ 3' | bc     # piping the formula to `bc` to parse the output https://stackoverflow.com/a/13111995
                                                  -printf '2 ^ 3\n' | bc # piping using `printf` (newline `\n` is mandatory)
                                                  +
                                                  #!/bin/sh
                                                  +echo $((2 * 2 * 2))   # equivalent to `bash`'s `echo $((2 ** 3))`
                                                  +echo '2 ^ 3' | bc     # piping the formula to `bc` to parse the output https://stackoverflow.com/a/13111995
                                                  +printf '2 ^ 3\n' | bc # piping using `printf` (newline `\n` is mandatory)
                                                   
                                                  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX!
                                                  • diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3020.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3020.html index 1ea9424..a36f4ce 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3020.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3020.html @@ -1,12 +1,12 @@

                                                    In POSIX sh, &> is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     ssh host cmd &> log
                                                     

                                                    Correct code

                                                    Use the long form instead of the shorthand:

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     ssh host cmd > log 2>&1
                                                     

                                                    Rationale

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.html index 10f687d..eb730fa 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.html @@ -1,2 +1,27 @@ -

                                                    In POSIX sh, >& is undefined. -See similar warning [[SC3020]]

                                                    +

                                                    In POSIX sh, >& filename (as opposed to >& fd) is undefined.

                                                    +

                                                    Note: ShellCheck 0.8.0 and below would trigger this warning on the perfectly POSIX compatible construct >& fd. If you are using >& to copy an integer file descriptor, please ignore this warning.

                                                    +

                                                    Problematic code

                                                    +
                                                    # Writing to a filename
                                                    +mycommand >& log.txt
                                                    +
                                                    +

                                                    Correct code

                                                    +
                                                    mycommand > log.txt 2>&1
                                                    +
                                                    +

                                                    The following construct is also fine, as it uses an integer file descriptor instead of a filename:

                                                    +
                                                    fd=3
                                                    +mycommand >& $fd
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    There are two forms of the >& file descriptor operator:

                                                    +
                                                      +
                                                    • >& integer such as >& 3 is a POSIX compatible synonym for 1>&3
                                                    • +
                                                    • >& filename such as >& log.txt is a Bash specific synonym for > log.txt 2>&1
                                                    • +
                                                    +

                                                    If (and only if) you are using the latter form, write it out as shown in the correct example to ensure portability.

                                                    +

                                                    Exceptions

                                                    +

                                                    ShellCheck 0.8.0 and below incorrectly emits this warning for constructs like >& $var, even though this would be POSIX compatible when $var is an integer. In such cases, please ignore this warning.

                                                    +

                                                    Related resources

                                                    + diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.json index 4b0138e..7bec58d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3021.json @@ -1,5 +1,5 @@ { - "title": "In POSIX sh, >& is undefined.", + "title": "In POSIX sh, >& filename (as opposed to >& fd) is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3022.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3022.html index 04dee1b..841ce33 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3022.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3022.html @@ -1,7 +1,7 @@

                                                    In POSIX sh, named file descriptors is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    exec {n}> "$output"
                                                    +
                                                    exec {n}> "$output"
                                                     echo "Your fd is $n"
                                                     

                                                    Correct code

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.html index df87c0d..3d0d59d 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.html @@ -1,7 +1,7 @@ -

                                                    In POSIX sh, FDs outside of 0-9 are undefined.

                                                    -

                                                    (or "In dash, ... are not supported." when using dash)

                                                    +

                                                    In POSIX sh, FDs outside 0-9 are undefined.

                                                    +

                                                    (or "In dash, ... are not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    # Open FD #10 for writing
                                                    +
                                                    # Open FD #10 for writing
                                                     exec 10> file
                                                     

                                                    Correct code

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.json index 23b66a0..66fe458 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3023.json @@ -1,5 +1,5 @@ { - "title": "In POSIX sh, FDs outside of 0-9 are undefined.", + "title": "In POSIX sh, FDs outside 0-9 are undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3024.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3024.html index 2780dfe..81019d6 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3024.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3024.html @@ -1,11 +1,11 @@ -

                                                    In POSIX sh, += is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    In POSIX sh, += is undefined.

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    var="Hello "
                                                    +
                                                    var="Hello "
                                                     var+="World"
                                                     

                                                    Correct code

                                                    -
                                                    var="Hello "
                                                    +
                                                    var="Hello "
                                                     var="${var}World"
                                                     

                                                    Rationale

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3025.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3025.html index aa8d5fc..3e3a412 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3025.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3025.html @@ -1,11 +1,11 @@ -

                                                    In POSIX sh, /dev/{tcp,udp} is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    In POSIX sh, /dev/{tcp,udp} is undefined.

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    echo foo > /dev/tcp/myhost/1234
                                                    +
                                                    echo foo > /dev/tcp/myhost/1234
                                                     

                                                    Correct code

                                                    Rewrite using a tool like netcat (nc):

                                                    -
                                                    echo foo | nc myhost 1234
                                                    +
                                                    echo foo | nc myhost 1234
                                                     

                                                    Rationale

                                                    /dev/tcp/$host/$port and /dev/udp/$host/$port are recognized in redirections by bash and ksh, and a socket connection is made instead of opening a file. They do not physically exist in /dev.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3026.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3026.html index 6b80b6f..c680e9b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3026.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3026.html @@ -1,18 +1,25 @@ -

                                                    In POSIX sh, ^ in place of ! in glob bracket expressions is undefined.

                                                    +

                                                    In POSIX sh, ^ in place of ! in glob bracket expressions is undefined.

                                                    Problematic code

                                                    -
                                                    echo foo-[^0]*.jpg
                                                    +
                                                    echo foo-[^0]*.jpg
                                                     

                                                    Correct code

                                                    -
                                                    echo foo-[!0]*.jpg
                                                    +
                                                    echo foo-[!0]*.jpg
                                                    +
                                                    +

                                                    or if the intention is to match ^ literally then either do not put it immediately after [ or quote it by backslash \

                                                    +
                                                    echo foo-[0^]*.jpg foo-[\^0]*.jpg
                                                     

                                                    Rationale

                                                    -

                                                    [^c] is frequently used in most regular expression variants to mean "any character except c". Ksh and Bash adopted it for globs as well.

                                                    -

                                                    However, strictly speaking, the only range complement syntax guaranteed to be supported across shells is [!c]. Dash only supports it when using fnmatch and glob from glibc.

                                                    +

                                                    [^c] is frequently used in most regular expression variants to mean "any character except c". Ksh and Bash adopted it for globs as well.

                                                    +

                                                    However, strictly speaking, the only range complement syntax guaranteed to be supported across shells is [!c]. POSIX explicitly states that behavior of [^c] is unspecified. Dash used to support [^c] when compiled with fnmatch and glob from glibc, but it was considered as a bug and fixed in version 0.5.12. Busybox has a similar bug 14516.

                                                    +

                                                    In zsh interactive sessions echo foo-[!0]*.jpg may cause zsh: event not found: 0 error due to attempt of history expansion. BASH however suppresses history expansion when ! is first character in square brackets.

                                                    +

                                                    Discrepancy of negation syntax in regular expressions and shell pattern matching appeared in early UNIX versions. Pipeline syntax in shells had not settled yet, some terminals had no | key, so ^ was used there. That is why in some shells exclamation mark ! was chosen instead of circumflex (caret) ^ symbol for negation in globs. For more details related to shell history see "The ^ = | ?" thread in the Unix Heritage Society mailing list or Sven Mascheck. The Traditional Bourne Shell Family. History and Development page.

                                                    Exceptions

                                                    If you only intend to target shells that supports this feature, you can change the shebang to a shell that guarantees support, or [[ignore]] this warning. Or just rewrite it to be on the technically correct side.

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3028.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3028.html index d918bc2..c8cda2b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3028.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3028.html @@ -1,21 +1,21 @@

                                                    In POSIX sh, VARIABLE is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     echo "$HOSTNAME $UID $RANDOM"
                                                     

                                                    Correct code

                                                    Either switch to a shell like bash that supports the special variable you're trying to use, or use an external command to get the information you want:

                                                    -
                                                    #!/bin/sh
                                                    -echo "$(hostname) $(id -u) $(awk 'BEGIN { srand(); print int(rand()*32768) }' /dev/null)"
                                                    +
                                                    #!/bin/sh
                                                    +echo "$(hostname) $(id -u) $(awk 'BEGIN { srand(); print int(rand()*32768) }' /dev/null)"
                                                     

                                                    Rationale

                                                    The variable you are attempting to use is a special variable in bash or ksh. To get the same information from dash or POSIX sh, use an external command instead.

                                                    For PIPESTATUS, the pipeline can be instrumented to record the exit value of each command:

                                                    -
                                                    { cmd0; echo $? > status0; } | { cmd1; echo $? > status1; } | cmd2
                                                    +
                                                    { cmd0; echo $? > status0; } | { cmd1; echo $? > status1; } | cmd2
                                                     

                                                    Exceptions

                                                    -

                                                    If you only intend to target shells that supports this feature, you can change +

                                                    If you only intend to target shells that support this feature, you can change the shebang to a shell that guarantees support, or [[ignore]] this warning.

                                                    You can use # shellcheck disable=SC3000-SC4000 to ignore all such compatibility warnings.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.html index fc2ed94..d975cb4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.html @@ -1 +1,19 @@ -

                                                    |& in place of 2>&1 | is not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, |& in place of 2>&1 | is undefined.

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +make |& tee ~/log
                                                    +
                                                    +

                                                    Correct code

                                                    +

                                                    Either switch to bash:

                                                    +
                                                    #!/bin/sh
                                                    +make 2>&1 | tee ~/log
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    In this case, the non-portable shorthand |& to pipe both stdout and stderr can be easily replaced with its written out form 2>&1 | as shown.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.json index 7c590da..e036ad4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3029.json @@ -1,5 +1,5 @@ { - "title": "|& in place of 2>&1 | is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, |& in place of 2>&1 | is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.html index 95b92a3..be455b0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.html @@ -1 +1,39 @@ -

                                                    arrays are not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, arrays are undefined.

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +x=( foo bar "Hello World" )
                                                    +for word in "${x[@]}"
                                                    +do
                                                    +  touch "$word.dat"
                                                    +done
                                                    +
                                                    +

                                                    Correct code

                                                    +

                                                    Consider switching to a shell that does support arrays, such as bash or ksh, by changing the shebang.

                                                    +

                                                    Alternatively, if you only need a single array at a time, you may be able to rewrite to use the positional parameters:

                                                    +
                                                    set -- foo bar "Hello World"
                                                    +for word in "$@"
                                                    +do
                                                    +  touch "$word.dat"
                                                    +done
                                                    +
                                                    +

                                                    or strings with carefully chosen delimiters:

                                                    +
                                                    x='foo
                                                    +bar
                                                    +Hello World
                                                    +'
                                                    +
                                                    +printf '%s' "$x" |
                                                    +  while IFS='' read -r word
                                                    +  do
                                                    +    touch "$word.dat"
                                                    +  done
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Arrays are specific to Bash and Ksh, but you are using them in a script declared to run with sh or dash. These shells do not support arrays at all. You should either switch to Bash/Ksh, or rewrite the script to not use arrays.

                                                    +

                                                    There are no great replacements, especially not that can be mechanically applied, but using the positional parameters or a delimited string works in many cases, as shown in the examples.

                                                    +

                                                    Exceptions

                                                    +

                                                    None.

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.json index 5ec21a2..926843c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3030.json @@ -1,5 +1,5 @@ { - "title": "arrays are not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, arrays are undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3031.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3031.html index 8a40315..c82dbe4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3031.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3031.html @@ -1,12 +1,12 @@

                                                    In POSIX sh, redirecting from/to globs is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     date > /tmp/log*.txt
                                                     

                                                    Correct code

                                                    To redirect from/to the only match of a glob, expand the glob first:

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     set -- /tmp/log*.txt
                                                     # PS: This is a good place to verify that there was exactly one match
                                                     date > "$1"
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.html
                                                    index f7ef6cf..dfd24fc 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.html
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.html
                                                    @@ -1 +1,20 @@
                                                    -

                                                    coproc is not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, coproc is undefined.

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +coproc nc shellcheck.net 80
                                                    +
                                                    +

                                                    Correct code

                                                    +

                                                    Either switch to Bash:

                                                    +
                                                    #!/usr/bin/env bash
                                                    +coproc nc shellcheck.net 80
                                                    +
                                                    +

                                                    or try to write your script in a POSIX compatible way using named pipes.

                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    There is no simple substitution for a coproc, though many use cases can be rewritten via redirection to named pipes (mkfifo).

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.json index d2f091e..c486ec4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3032.json @@ -1,5 +1,5 @@ { - "title": "coproc is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, coproc is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.html index 079ef0b..5b6a9eb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.html @@ -1 +1,22 @@ -

                                                    naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is undefined.

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +run-tests() {
                                                    +  make test
                                                    +}
                                                    +
                                                    +

                                                    Correct code

                                                    +
                                                    #!/bin/sh
                                                    +run_tests() {
                                                    +  make test
                                                    +}
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    In this case, you can rename your functions to follow the same naming requirements as variables, as these are the only names guaranteed to be valid function names in POSIX.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.json index 346f9b6..af9cb75 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3033.json @@ -1,5 +1,5 @@ { - "title": "naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3034.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3034.html index 38af0b2..777434a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3034.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3034.html @@ -1,10 +1,10 @@ -

                                                    In POSIX sh, $(<file) is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    In POSIX sh, $(<file) is undefined.

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    content=$(<file)
                                                    +
                                                    content=$(<file)
                                                     

                                                    Correct code

                                                    -
                                                    content=$(cat file)
                                                    +
                                                    content=$(cat file)
                                                     

                                                    Rationale

                                                    In bash and ksh, $(< file) will expand to the full contents of the file.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.html index 0ca6026..15555ac 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.html @@ -1 +1,18 @@ -

                                                    echo flags besides -n are not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In Dash, echo flags besides -n are not supported.

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/dash
                                                    +echo -e "Name:\t$USER"
                                                    +
                                                    +

                                                    Correct code

                                                    +
                                                    #!/bin/dash
                                                    +printf 'Name:\t%s\n' "$USER"
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    In this case, you can rewrite your use of echo using printf, which behaves the same across shells.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.json index 79c9b75..2c3ec44 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3036.json @@ -1,5 +1,5 @@ { - "title": "echo flags besides -n are not supported in dash and undefined in POSIX sh.", + "title": "In Dash, echo flags besides -n are not supported.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3037.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3037.html index 1a6780e..3a807e1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3037.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3037.html @@ -1,12 +1,12 @@

                                                    In POSIX sh, echo flags are undefined.

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    -echo -ne 'Foo:\tBar'
                                                    +
                                                    #!/bin/sh
                                                    +echo -ne 'Foo:\tBar'
                                                     

                                                    Correct code

                                                    Rewrite in terms of the more robust and standardized printf:

                                                    -
                                                    #!/bin/sh
                                                    -printf 'Foo:\tBar'
                                                    +
                                                    #!/bin/sh
                                                    +printf 'Foo:\tBar'
                                                     

                                                    Rationale

                                                    echo has historically behaved differently on different systems. Use printf instead to ensure compatibility between shells.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3038.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3038.html index 0c75d51..11eabc5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3038.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3038.html @@ -1,12 +1,12 @@

                                                    In POSIX sh, exec flags are undefined.

                                                    -

                                                    (or "In dash, ... are not supported." when using dash)

                                                    +

                                                    (or "In dash, ... are not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     exec -ca ls busybox
                                                     

                                                    Correct code

                                                    Switch to a shell that does support the exec flags you are using, like bash:

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     exec -ca ls busybox
                                                     

                                                    In POSIX, you can use env to start a command with a clean environment, but there is no way to generally specifying the zeroth argument.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.html index 66ead4e..ae91953 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.html @@ -1,12 +1,12 @@ -

                                                    In POSIX sh, 'let' is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    In POSIX sh, let is undefined.

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     let x=2+2
                                                     

                                                    Correct code

                                                    Use standard $((arithmetic expansion)) instead of legacy let:

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     x=$((2+2))
                                                     

                                                    Rationale

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.json index 1d75c38..3ee7480 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3039.json @@ -1,5 +1,5 @@ { - "title": "In POSIX sh, 'let' is undefined.", + "title": "In POSIX sh, let is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.html index b911f40..de6f1a1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.html @@ -1,14 +1,22 @@ -

                                                    In POSIX sh, set option pipefail is undefined.

                                                    -

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +

                                                    In POSIX sh, set option [name] is undefined.

                                                    +

                                                    The value [name] is something other than allexport, errexit, ignoreeof, monitor, noclobber, noexec, noglob, nolog, notify , nounset, verbose, vi, or xtrace.

                                                    + + +

                                                    Rationale

                                                    +

                                                    In a script declared to run with sh or dash, ShellCheck found set -o used with an option that is not defined by POSIX. The option may not be supported by these shells.

                                                    +

                                                    Exceptions

                                                    +

                                                    If the script has already inspected the current shell and determined the option is available, you can [[ignore]] this message.

                                                    +

                                                    pipefail

                                                    +

                                                    Problematic code:

                                                    +
                                                    #!/bin/sh
                                                     set -o pipefail
                                                     if cmd1 | cmd2 | cmd3
                                                     then
                                                       echo "Success"
                                                     fi
                                                     
                                                    -

                                                    Correct code

                                                    -
                                                    fail="$(mktemp)"
                                                    +

                                                    Correct code:

                                                    +
                                                    fail="$(mktemp)"
                                                     if { cmd1 || echo > "$fail"; } |
                                                        { cmd2 || echo > "$fail"; } |
                                                          cmd3 && [ ! -s "$fail" ]
                                                    @@ -17,15 +25,16 @@ 

                                                    Correct code

                                                    fi rm "$fail"
                                                    -

                                                    or switch to a shell that supports pipefail, such as Bash.

                                                    -

                                                    Rationale

                                                    -

                                                    ShellCheck found set -o pipefail in a script declare to run with sh or dash. This feature may not be supported on these shells.

                                                    -

                                                    As an alternative, each stage in the pipeline such as cmd1 can be rewritten as { cmd1 || echo > file; } which will behave as before but now also write the exit code into file if the command fails.

                                                    +

                                                    Each stage in the pipeline such as cmd1 can be rewritten as { cmd1 || echo > file; } which will behave as before but now also write the exit code into file if the command fails.

                                                    If the file has been written to when the pipeline is done, it means one of the commands failed. This is demonstrated in the correct example.

                                                    -

                                                    This can obviously be extended with e.g. echo "cmd1=$?" >> file if the particular exit codes for particular commands are desired.

                                                    -

                                                    Exceptions

                                                    -

                                                    If the script has already inspected the current shell and determined the option is available, you can [[ignore]] this message.

                                                    +

                                                    This can obviously be extended with e.g. echo "cmd1=$?" >> file if the particular exit codes for particular commands are desired.

                                                    +

                                                    If pipefail or other options are used only as a sanity check, another option is to try the setting in a subshell and only apply it in the main shell if successful. This will set the flag on platforms where sh is linked to ash, busybox, or bash, but do nothing if sh is dash.

                                                    +
                                                    # Set pipefail if it works in a subshell, disregard if unsupported
                                                    +# shellcheck disable=SC3040
                                                    +(set -o pipefail 2> /dev/null) && set -o pipefail
                                                    +
                                                    +

                                                    Alternatively, switch to a shell that supports pipefail, such as Bash.

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.json index 434b795..cabad29 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3040.json @@ -1,5 +1,5 @@ { - "title": "In POSIX sh, set option pipefail is undefined.", + "title": "In POSIX sh, set option [name] is undefined.", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.html index 9e3e5f6..c4a758c 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.html @@ -1 +1,20 @@ -

                                                    only set flags -a, -b, -C, -e, -f, -h, -m, -n, -u, -v, -x, -o are supported in dash or defined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, set flag -E is undefined

                                                    +

                                                    (or any other option)

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +set -EB
                                                    +
                                                    +

                                                    Correct code

                                                    +

                                                    Switch to a shell that supports these options, such as Bash:

                                                    +
                                                    #!/bin/bash
                                                    +set -EB
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    Most shell specific set flags are hard to emulate in POSIX, so use your best judgement as to whether you can make do without them, or if you should require a shell that supports them.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.json index 0dd2f33..0c56b42 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3041.json @@ -1,5 +1,5 @@ { - "title": "only set flags -a, -b, -C, -e, -f, -h, -m, -n, -u, -v, -x, -o are supported in dash or defined in POSIX sh.", + "title": "In POSIX sh, set flag -E is undefined", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.html index 492d7c5..0011c16 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.html @@ -1 +1,20 @@ -

                                                    double-dashed set flags are not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, set flag --default is undefined

                                                    +

                                                    (or any other option)

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/ksh
                                                    +set --default
                                                    +
                                                    +

                                                    Correct code

                                                    +

                                                    Switch to a shell that supports these options, such as Ksh:

                                                    +
                                                    #!/bin/ksh
                                                    +set --default
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Ksh.

                                                    +

                                                    Most shell specific set flags are hard to emulate in POSIX, so use your best judgement as to whether you can make do without them, or if you should require a shell that supports them.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.json index d3b83af..998acc4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3042.json @@ -1,5 +1,5 @@ { - "title": "double-dashed set flags are not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, set flag --default is undefined", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3043.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3043.html index edca726..cc57ac0 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3043.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3043.html @@ -1,19 +1,19 @@

                                                    In POSIX sh, local is undefined.

                                                    Problematic code

                                                    -
                                                    myfunc() {
                                                    +
                                                    myfunc() {
                                                       local i=0
                                                       ..
                                                     }
                                                     

                                                    Correct code

                                                    In POSIX sh, you can adopt some convention to avoid accidentally overwriting variables names, e.g. prefixing with the function name:

                                                    -
                                                    myfunc() {
                                                    +
                                                    myfunc() {
                                                       _myfunc_i=0
                                                       ..
                                                     }
                                                     

                                                    Rationale

                                                    -

                                                    local is supported in many shells, including bash, ksh, dash, and BusyBox ash. However, strictly speaking, it's not POSIX.

                                                    +

                                                    local is supported in many shells, including bash, ksh, dash, and BusyBox ash. However, it is not specified by POSIX.

                                                    Exceptions

                                                    Since quite a lot of real world shells support this feature, you may decide to [[ignore]] the warning.

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3044.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3044.html index a7b782a..9e4e786 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3044.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3044.html @@ -1,26 +1,26 @@

                                                    In POSIX sh, declare is undefined.

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     declare var="value"
                                                     

                                                    or

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     declare -r readonly
                                                     

                                                    or

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     declare ...
                                                     

                                                    Correct code

                                                    If assigning a simple variable outside of a function, skip declare all together:

                                                    -
                                                    var="value"
                                                    +
                                                    var="value"
                                                     

                                                    If declaring a variable read-only:

                                                    var=value
                                                     readonly var
                                                     

                                                    If you are unable to find a suitable replacement, consider switching to a shell that supports declare:

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     declare ...
                                                     

                                                    Indexed arrays, associative arrays, local variables, namerefs, and integer variables are not supported in POSIX sh. Either write around them, or switch to a shell that supports them.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3045.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3045.html index bce6e16..8c4fa7e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3045.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3045.html @@ -1,9 +1,9 @@

                                                    In POSIX sh, some-command-with-flag is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    ShellCheck has noticed you're using flags for commands that don't necessarily support them. Some examples:

                                                    -
                                                    #!/bin/sh
                                                    -read -e     # Using libreadline
                                                    +
                                                    #!/bin/sh
                                                    +read   -e   # Using libreadline
                                                     export -f   # Exporting functions
                                                     ulimit -v   # Setting vspace limits
                                                     wait   -n   # Waiting for a single process
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3046.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3046.html
                                                    index 2f97dd8..63d2957 100644
                                                    --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3046.html
                                                    +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3046.html
                                                    @@ -1,15 +1,15 @@
                                                     

                                                    In POSIX sh, source in place of . is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     source mylib.sh
                                                     

                                                    Correct code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     . mylib.sh
                                                     

                                                    Rationale

                                                    -

                                                    source is a bash and ksh specific alias for the harder-to-pronounce/search command . ("dot")

                                                    +

                                                    source is a bash and ksh specific alias for the harder-to-pronounce/search command . ("dot")

                                                    When writing for dash or sh, use . instead.

                                                    Exceptions

                                                    If you only intend to target shells that supports this feature, you can change diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3047.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3047.html index a98d5aa..d929a8b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3047.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3047.html @@ -1,14 +1,14 @@

                                                    In POSIX sh, trapping ERR is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Also applies to RETURN and DEBUG.

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    -trap 'Command failed with $?' ERR
                                                    +
                                                    #!/bin/sh
                                                    +trap 'Command failed with $?' ERR
                                                     

                                                    Correct code

                                                    Switch to a shell like ksh or bash that does support the kind of trap you want:

                                                    -
                                                    #!/bin/bash
                                                    -trap 'Command failed with $?' ERR
                                                    +
                                                    #!/bin/bash
                                                    +trap 'Command failed with $?' ERR
                                                     

                                                    Otherwise, rewrite the script to not rely on the trap.

                                                    Rationale

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3048.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3048.html index d8daf71..1238ec4 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3048.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3048.html @@ -1,14 +1,14 @@

                                                    In POSIX sh, prefixing signal names with 'SIG' is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    trap 'handle_interrupt' SIGINT
                                                    +
                                                    trap 'handle_interrupt' SIGINT
                                                     

                                                    Correct code

                                                    -
                                                    trap 'handle_interrupt' INT
                                                    +
                                                    trap 'handle_interrupt' INT
                                                     

                                                    Rationale

                                                    -

                                                    POSIX does not allow specifying the "SIG" prefix of a signal name like "INT". Bash and Ksh supports this as an extension.

                                                    -

                                                    Simply remove the "SIG" from the signal name.

                                                    +

                                                    POSIX does not allow specifying the "SIG" prefix of a signal name like "INT". Bash and Ksh supports this as an extension.

                                                    +

                                                    Simply remove the "SIG" from the signal name.

                                                    Exceptions

                                                    None

                                                    Related resources

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3049.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3049.html index 255bc45..a07570b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3049.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3049.html @@ -1,13 +1,14 @@

                                                    In POSIX sh, using lower/mixed case for signal names is undefined.

                                                    Problematic code

                                                    -
                                                    trap 'handle_sigint' int
                                                    +
                                                    trap 'handle_sigint' int
                                                     

                                                    Correct code

                                                    -
                                                    trap 'handle_sigint' INT
                                                    +
                                                    trap 'handle_sigint' INT
                                                     

                                                    Rationale

                                                    POSIX requires that signal names be given in uppercase:

                                                    -

                                                    The condition can be EXIT, 0 (equivalent to EXIT), or a signal specified using a symbolic name, without the SIG prefix, as listed in the tables of signal names in the <signal.h> header defined in XBD Headers; for example, HUP, INT, QUIT, TERM. Implementations may permit names with the SIG prefix or ignore case in signal names as an extension.

                                                    +
                                                    +

                                                    The condition can be EXIT, 0 (equivalent to EXIT), or a signal specified using a symbolic name, without the SIG prefix, as listed in the tables of signal names in the <signal.h> header defined in XBD Headers; for example, HUP, INT, QUIT, TERM. Implementations may permit names with the SIG prefix or ignore case in signal names as an extension.

                                                    Exceptions

                                                    None

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3050.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3050.html index e447522..5784645 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3050.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3050.html @@ -1,11 +1,11 @@

                                                    In POSIX sh, printf %q is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    ssh host "cat $(printf "%q" "$remotefile")"
                                                    +
                                                    ssh host "cat $(printf "%q" "$remotefile")"
                                                     

                                                    Correct code

                                                    There is not really a good, built-in way to escape a string for a remote shell in POSIX sh. However, you can replace each ' in the input with '\'' and then wrap the whole results in single quotes:

                                                    -
                                                    escape() { printf "'%s'\\n" "$(printf '%s' "$1" | sed -e "s/'/'\\\\''/g")"; }
                                                    +
                                                    escape() { printf "'%s'\\n" "$(printf '%s' "$1" | sed -e "s/'/'\\\\''/g")"; }
                                                     ssh host "cat $(escape "$remotefile")"
                                                     

                                                    Alternatively, switch to a shell that does support printf %q like ksh or bash.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.html index ba05500..374dc3a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.html @@ -1 +1,19 @@ -

                                                    'source' in place of '.' is not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, source in place of . is undefined

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +source /usr/lib/myfile.sh
                                                    +
                                                    +

                                                    Correct code

                                                    +
                                                    #!/bin/sh
                                                    +. /usr/lib/myfile.sh
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    source is a Bash/Ksh specific, readable synonym for .. In this case, you can for the most part easily replace source with .

                                                    +

                                                    Do note that source myfile in POSIX sh only searches $PATH, while Bash/Ksh will additionally search the current directory.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.json index 29e1178..6b64f0e 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3051.json @@ -1,5 +1,5 @@ { - "title": "'source' in place of '.' is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, source in place of . is undefined", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.html index 7866aae..cf3892a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.html @@ -1 +1,18 @@ -

                                                    arithmetic base conversion is not supported in dash and undefined in POSIX sh.

                                                    \ No newline at end of file +

                                                    In POSIX sh, arithmetic base conversion is undefined

                                                    +

                                                    Problematic code

                                                    +
                                                    #!/bin/sh
                                                    +echo "Hex $1 is decimal $((16#$1))"
                                                    +
                                                    +

                                                    Correct code

                                                    +
                                                    #!/bin/sh
                                                    +echo "Hex $1 is decimal $(echo "ibase=16; $1" | bc)"
                                                    +
                                                    +

                                                    Rationale

                                                    +

                                                    Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

                                                    +

                                                    In this case, using radix specifiers in arithmetic contexts, such as $((16#DEADBEEF)) is not portable. bc ibase/obase can be used instead.

                                                    +

                                                    Exceptions

                                                    +

                                                    None

                                                    +

                                                    Related resources

                                                    +
                                                      +
                                                    • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
                                                    • +
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.json index 0304a90..71778eb 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.json +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3052.json @@ -1,5 +1,5 @@ { - "title": "arithmetic base conversion is not supported in dash and undefined in POSIX sh.", + "title": "In POSIX sh, arithmetic base conversion is undefined", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3053.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3053.html index b687c3b..dd9898b 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3053.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3053.html @@ -1,18 +1,18 @@

                                                    In POSIX sh, indirect expansion is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     name="PATH"
                                                     echo "${!name}"
                                                     

                                                    Correct code

                                                    The easiest solution is to switch to a shell that does support indirect expansion, like bash:

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     name="PATH"
                                                     echo "${!name}"
                                                     

                                                    Alternatively, carefully rewrite using eval:

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     name=PATH
                                                     eval "echo \"\$$name\""
                                                     
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3054.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3054.html index 9be5cf0..508527a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3054.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3054.html @@ -1,18 +1,18 @@

                                                    In POSIX sh, array references are undefined.

                                                    -

                                                    (or "In dash, ... are not supported." when using dash)

                                                    +

                                                    (or "In dash, ... are not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     var=(foo bar)
                                                     echo "${var[1]}"
                                                     

                                                    Correct code

                                                    The easiest solution is to switch to a shell that does support arrays, like bash:

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     var=(foo bar)
                                                     echo "${var[1]}"
                                                     

                                                    Alternatively, rewrite the logic to use e.g. indirect variable references or set:

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     set -- foo bar
                                                     echo "$2"
                                                     
                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3056.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3056.html index 0714c7f..8829b6a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3056.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3056.html @@ -1,12 +1,12 @@

                                                    In POSIX sh, name matching prefixes are undefined.

                                                    -

                                                    (or "In dash, ... are not supported." when using dash)

                                                    +

                                                    (or "In dash, ... are not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     echo "${!ANDROID_*}"
                                                     

                                                    Correct code

                                                    The easiest solution is to switch to a shell that does support name matching prefixes, like bash:

                                                    -
                                                    #!/bin/bash
                                                    +
                                                    #!/bin/bash
                                                     echo "${!ANDROID_*}"
                                                     

                                                    Alternatively, extract the data you want from env or set

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3057.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3057.html index 32dffa8..516b0f1 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3057.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3057.html @@ -1,13 +1,20 @@

                                                    In POSIX sh, string indexing is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     echo "Your initial is ${USER:0:1}"
                                                     

                                                    Correct code

                                                    Either switch to a shell that does support string indexing via parameter expansion, like bash or ksh, or rewrite with cut:

                                                    -
                                                    #!/bin/sh
                                                    -echo "Your initial is $(printf '%s' "$USER" | cut -c 1)"
                                                    +
                                                    #!/bin/sh
                                                    +echo "Your initial is $(printf '%s' "$USER" | cut -c 1)"
                                                    +
                                                    +

                                                    To find the last argument passed to a shell script without using bash’s ${@:$#}- or ${@: -1}-style string indexing, use the following, which even “works in the unix v7 bourne shell from 1979”:

                                                    +
                                                    #!/bin/sh
                                                    +for argument in "$@"; do
                                                    +  : # `:`, also called as `true`, is a no-op here
                                                    +done
                                                    +printf '%s\n' "${argument-}"
                                                     

                                                    Rationale

                                                    String indexing is a bash and ksh extension, and does not work in dash or POSIX sh.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3059.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3059.html index eb61caf..8a7960a 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3059.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3059.html @@ -1,2 +1,2 @@

                                                    Case modification is not supported in dash and undefined in POSIX sh.

                                                    -

                                                    https://github.com/sbaudoin/sonar-shellcheck/blob/master/src/main/scripts/build_checks.yml

                                                    +

                                                    https://github.com/sbaudoin/sonar-shellcheck/blob/master/src/main/scripts/build_checks.yml

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3060.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3060.html index 19468af..62412d5 100644 --- a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3060.html +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3060.html @@ -1,13 +1,13 @@

                                                    In POSIX sh, string replacement is undefined.

                                                    -

                                                    (or "In dash, ... is not supported." when using dash)

                                                    +

                                                    (or "In dash, ... is not supported." when using dash)

                                                    Problematic code

                                                    -
                                                    #!/bin/sh
                                                    +
                                                    #!/bin/sh
                                                     echo "${var//foo/bar}"
                                                     

                                                    Correct code

                                                    Either switch the shebang to bash or ksh, or rewrite the expansion using a tool like sed:

                                                    -
                                                    #!/bin/sh
                                                    -echo "$var" | sed 's/foo/bar/g'
                                                    +
                                                    #!/bin/sh
                                                    +echo "$var" | sed 's/foo/bar/g'
                                                     

                                                    Rationale

                                                    String replacement in parameter expansion is a bash and ksh extension. dash and POSIX sh does not support it.

                                                    diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.html b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.html new file mode 100644 index 0000000..980b131 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.html @@ -0,0 +1 @@ +

                                                    read without a variable is

                                                    \ No newline at end of file diff --git a/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.json b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.json new file mode 100644 index 0000000..b3c1fc9 --- /dev/null +++ b/src/main/resources/org/sonar/l10n/shellcheck/rules/shellcheck/SC3061.json @@ -0,0 +1,14 @@ +{ + "title": "read without a variable is", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "shell", + "convention" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/scripts/build_checks.py b/src/main/scripts/build_checks.py index c4a6819..d00baad 100644 --- a/src/main/scripts/build_checks.py +++ b/src/main/scripts/build_checks.py @@ -13,26 +13,30 @@ # FUNCTIONS ############################ def get_config(rule_name, name): - if 'rules' in config and rule_name in config['rules'] and name in config['rules'][rule_name]: - return config['rules'][rule_name][name] - return config['default'].get(name, None) + if "rules" in config and rule_name in config["rules"] and name in config["rules"][rule_name]: + return config["rules"][rule_name][name] + return config["default"].get(name, None) def get_tags(rule_name): - tags_always = get_config(rule_name, 'tags_always') + tags_always = get_config(rule_name, "tags_always") if not tags_always: tags_always = [] - return tags_always + get_config(rule_name, 'tags') + return tags_always + get_config(rule_name, "tags") def get_default_severity(severity): - return config['default']['severity'][severity] + return config["default"]["severity"][severity] def get_severity(rule_name): - if 'rules' in config and rule_name in config['rules'] and 'severity' in config['rules'][rule_name]: - return get_default_severity(config['rules'][rule_name]['severity']) - return get_default_severity('default') + if ( + "rules" in config + and rule_name in config["rules"] + and "severity" in config["rules"][rule_name] + ): + return get_default_severity(config["rules"][rule_name]["severity"]) + return get_default_severity("default") ############################ @@ -41,118 +45,129 @@ def get_severity(rule_name): # Check command line arguments if len(sys.argv) != 3: - print('Usage: build_checks.py ', file=sys.stderr) + print( + "Usage: build_checks.py ", + file=sys.stderr, + ) sys.exit(1) sc_path = sys.argv[1] sev_file = sys.argv[2] if not os.path.isdir(sc_path): - print('Error: `' + sc_path + '\' does not exist or is not a directory', file=sys.stderr) + print("Error: `" + sc_path + "' does not exist or is not a directory", file=sys.stderr) sys.exit(1) if not os.path.isfile(sev_file): - print('Error: `' + sev_file + '\' does not exist or is not a file', file=sys.stderr) + print("Error: `" + sev_file + "' does not exist or is not a file", file=sys.stderr) sys.exit(1) # Load YAML configuration try: - config = yaml.load(open(os.path.dirname(os.path.abspath(__file__)) + '/build_checks.yml').read(), Loader=yaml.FullLoader) + config = yaml.load( + open(os.path.dirname(os.path.abspath(__file__)) + "/build_checks.yml").read(), + Loader=yaml.FullLoader, + ) except yaml.YAMLError as exc: print("Error in configuration file:", exc, file=sys.stderr) sys.exit(1) # Complete with severities try: severities = yaml.load(open(sev_file).read(), Loader=yaml.FullLoader) - if not 'rules' in config: - config['rules'] = {} + if "rules" not in config: + config["rules"] = {} for rule in severities: - if not rule in config['rules']: - config['rules'][rule] = {} - config['rules'][rule]['severity'] = severities[rule] + if rule not in config["rules"]: + config["rules"][rule] = {} + config["rules"][rule]["severity"] = severities[rule] except yaml.YAMLError as exc: print("Error in severities file:", exc, file=sys.stderr) sys.exit(1) -markdown = mistune.Markdown() # Process SCxxxx files found in Wiki for filename in os.listdir(sc_path): - if not (re.match('SC[0-9]{4}\.md', filename)): + if not (re.match("SC[0-9]{4}\.md", filename)): continue - rule = filename.replace('.md', '') + rule = filename.replace(".md", "") - if get_config(rule, 'ignored'): - print(' Ignoring ', filename, '...') + if get_config(rule, "ignored"): + print(" Ignoring ", filename, "...") continue - print(' Processing ', filename, '...') + print(" Processing ", filename, "...") - if get_config(rule, 'description'): - print(' INFO: configured description ignored') + if get_config(rule, "description"): + print(" INFO: configured description ignored") # Read MD file - md_data = open(sc_path + '/' + filename, 'r', encoding='utf-8').read() + md_data = open(sc_path + "/" + filename, "r", encoding="utf-8").read() # Check utf-8 code points > 3 bytes for c in md_data: - if c > '\uffff': - print(' WARN: this file may contain UTF-8 code points not supported by MySQL') + if c > "\uffff": + print(" WARN: this file may contain UTF-8 code points not supported by MySQL") break - md_data = re.sub('^(### .*):$', '\\1', md_data, flags=re.MULTILINE) - md_data = re.sub('^## (.*)$', '\\1', md_data, flags=re.MULTILINE) - md_data = re.sub('###', '##', md_data, flags=re.MULTILINE) + md_data = re.sub("^(### .*):$", "\\1", md_data, flags=re.MULTILINE) + md_data = re.sub("^## (.*)$", "\\1", md_data, flags=re.MULTILINE) + md_data = re.sub("###", "##", md_data, flags=re.MULTILINE) # Write HTML file - html = markdown(md_data) - file = open(rule + '.html', 'w') + html = mistune.html(md_data) + file = open(rule + ".html", "w") file.write(html) file.close() # Write JSON file - title = re.sub('<[^>]*>', '', html.splitlines()[0]) - title = re.sub('"', '\'', title) + title = re.sub("<[^>]*>", "", html.splitlines()[0]) + title = re.sub('"', "'", title) json_data = { - 'title': title, - 'type': get_config(rule, 'type'), - 'status': 'ready', - 'remediation': {'func': 'Constant/Issue', 'constantCost': get_config(rule, 'constantCost')}, - 'tags': get_tags(rule), - 'defaultSeverity': get_severity(rule) + "title": title, + "type": get_config(rule, "type"), + "status": "ready", + "remediation": {"func": "Constant/Issue", "constantCost": get_config(rule, "constantCost")}, + "tags": get_tags(rule), + "defaultSeverity": get_severity(rule), } - file = open(rule + '.json', 'w') - file.write(json.dumps(json_data, indent=2).replace('/', '\\/')) + file = open(rule + ".json", "w") + file.write(json.dumps(json_data, indent=2).replace("/", "\\/")) file.close() # Process rules defined in build_checks.yml if needed -for rule in config['rules']: - if get_config(rule, 'ignored'): +for rule in config["rules"]: + if get_config(rule, "ignored"): continue - if not os.path.isfile(rule + '.html') or not os.path.isfile(rule + '.json')\ - and 'description' in config['rules'][rule]: - print(' Generating additional files for', rule, '...') + if ( + not os.path.isfile(rule + ".html") + or not os.path.isfile(rule + ".json") + and "description" in config["rules"][rule] + ): + print(" Generating additional files for", rule, "...") # Write HTML file - file = open(rule + '.html', 'w') - if 'html' in config['rules'][rule]: - file.write(config['rules'][rule]['html']) + file = open(rule + ".html", "w") + if "html" in config["rules"][rule]: + file.write(config["rules"][rule]["html"]) else: # We cannot continue if there is no description - if 'description' not in config['rules'][rule]: + if "description" not in config["rules"][rule]: file.close() continue else: - file.write('

                                                    ') - file.write(html_parser.escape(config['rules'][rule]['description'])) - file.write('

                                                    ') + file.write("

                                                    ") + file.write(html_parser.escape(config["rules"][rule]["description"])) + file.write("

                                                    ") file.close() # Write JSON file json_data = {} - json_data['title'] = config['rules'][rule]['description'] - json_data['type'] = get_config(rule, 'type') - json_data['status'] = 'ready' - json_data['remediation'] = { 'func': 'Constant/Issue', 'constantCost': get_config(rule, 'constantCost') } - json_data['tags'] = get_tags(rule) - json_data['defaultSeverity'] = get_severity(rule) - file = open(rule + '.json', 'w') - file.write(json.dumps(json_data, indent=2).replace('/', '\\/')) + json_data["title"] = config["rules"][rule]["description"] + json_data["type"] = get_config(rule, "type") + json_data["status"] = "ready" + json_data["remediation"] = { + "func": "Constant/Issue", + "constantCost": get_config(rule, "constantCost"), + } + json_data["tags"] = get_tags(rule) + json_data["defaultSeverity"] = get_severity(rule) + file = open(rule + ".json", "w") + file.write(json.dumps(json_data, indent=2).replace("/", "\\/")) file.close() diff --git a/src/main/scripts/build_checks.sh b/src/main/scripts/build_checks.sh index d62eb98..ba4e303 100755 --- a/src/main/scripts/build_checks.sh +++ b/src/main/scripts/build_checks.sh @@ -1,6 +1,7 @@ #!/bin/bash -e apk add -q git +pip install pip --upgrade pip install mistune PyYAML cd /tmp @@ -17,10 +18,12 @@ do echo " Processing $FILE..." sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' style [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": style" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' StyleC [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": style" }' >> $TMP_CODE + sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' styleWithFix [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": style" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' verbose [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": verbose" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' verboseWithFix [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": verbose" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' info [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": info" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' InfoC [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": info" }' >> $TMP_CODE + sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' infoWithFix [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": info" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' warn [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": warning" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' warning [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": warning" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' WarningC [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": warning" }' >> $TMP_CODE @@ -28,6 +31,7 @@ do sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' warnWithFix [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": warning" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' err [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": error" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' ErrorC [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": error" }' >> $TMP_CODE + sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' errWithFix [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": error" }' >> $TMP_CODE sed 'H;1h;$!d;g;s/\n */ /g' $FILE | grep -o ' ppt [^0-9]*[0-9]\{4\}' | sed 's/.*\([0-9]\{4\}\)$/\1/' | awk '{ print "SC"$1": error" }' >> $TMP_CODE done cat $TMP_CODE | sed 's/C / /' | tr 'EWI' 'ewi' | sed 's/ppt/error/' | sort -u > $CODE_YAML diff --git a/src/main/scripts/build_checks.yml b/src/main/scripts/build_checks.yml index ceb5c2e..b3a7eb1 100644 --- a/src/main/scripts/build_checks.yml +++ b/src/main/scripts/build_checks.yml @@ -15,127 +15,74 @@ default: warning: Major rules: + SC1000: + ignored: False # See Wiki: rule retired v0.3.3 SC1004: - severity: info + severity: info # See Wiki: rule retired v0.7.2 SC1013: - description: "Bash requires ; or \\n here, after redirecting nested compound commands." + ignored: True # Not in the Wiki SC1015: - ignored: True # See Wiki: rule retired + ignored: False # See Wiki: rule retired v0.4.5 SC1016: - ignored: True # See Wiki: rule retired + ignored: False # See Wiki: rule retired v0.4.5 SC1021: - description: "You need a space before the \\)" - SC1027: - description: "Expected another argument for this operator." - SC1043: - description: "Found end token `EOF' further down, but with wrong casing." - SC1055: - description: "You need at least one command here. Use 'true;' as a no-op." - SC1057: - description: "Did you forget the 'do' for this loop?" - SC1059: - description: "No semicolons directly after 'do'." - SC1060: - description: "Can't have empty do clauses (use 'true' as a no-op)." - SC1063: - description: "You need a line feed or semicolon before the 'do'." + description: "You need a space before the \\)" # Not in the Wiki SC1067: - severity: error + severity: error # See Wiki: rule retired v0.7.2 SC1068: - severity: error - SC1076: - description: "Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ] or [[ $((i/2+7)) -ge 18 ]]." - SC1080: - description: "You need \\ before line feeds to break lines in [ ]." + severity: error # See Wiki: rule retired v0.7.2 SC1083: type: BUG SC1085: - description: "Did you forget to move the ;; after extending this case item?" - SC1092: - description: "Stopping at 100 'source' frames :O" + description: "Did you forget to move the ;; after extending this case item?" # Not in the Wiki SC1093: - description: "This file appears to be recursively sourced. Ignoring." - SC1103: - description: "This shell type is unknown. Use e.g. sh or bash." + description: "This file appears to be recursively sourced. Ignoring." # Not in the Wiki SC1117: - ignored: True # See Wiki: rule retired - SC1134: - description: "Parse error: failed to process file." - SC1137: - description: "Missing second '(' or ')' to start arithmetic or terminate the condition of the 'for ((;;))' loop" - SC1138: - description: "Remove spaces in arithmetic for loop." - SC2023: - description: "The shell may override 'time' as seen in man time(1). Use 'command time ..' for that one." + ignored: False # See Wiki: rule retired v0.6.0 + SC1136: + ignored: False # See Wiki: rule retired v0.7.2 + SC1146: + description: "Unknown extended-analysis value. Expected true/false." # Not in the Wiki + SC2008: + ignored: False # See Wiki: rule retired v0.4.7 SC2034: type: BUG - SC2042: - description: "Use spaces, not commas, to separate loop elements." + SC2039: + ignored: False # See Wiki: rule retired v0.7.2 + SC2040: + ignored: False # See Wiki: rule retired v0.4.2 SC2043: type: BUG - SC2075: - description: "Escaping \\< and \\> is required in [..], but invalid in [[..]]" - SC2083: - description: "Don't add spaces after the slash in './file'." - SC2111: - description: "ksh does not allow 'function' keyword and '()' at the same time." - SC2118: - description: "Ksh does not support |&. Use 2>&1 |." SC2124: type: BUG - SC2127: - description: "To use ${ ..; } command expansion or cases with ;;& or ;;&, specify #!/usr/bin/env bash or ksh" - html: | -

                                                    To use cases with ;&, specify #!/usr/bin/env bash or #!/usr/bin/env ksh.

                                                    -

                                                    To use cases with ;;&, specify #!/usr/bin/env bash.

                                                    -

                                                    To use ${ ..; } command expansion, specify #!/usr/bin/env ksh.

                                                    + SC2130: + ignored: False # See Wiki: rule retired v0.4.2 + SC2149: + ignored: False # See Wiki: rule retired v0.5.0 + SC2169: + ignored: False # See Wiki: rule retired v0.7.2 SC2188: type: BUG SC2193: type: BUG SC2215: type: BUG - SC2258: - description: "The trailing comma is part of the value, not a separator. Delete or quote it." - SC2265: - description: "Use && for logical AND. Single & will background and return true." - SC2266: - description: "Use || for logical OR. Single | will pipe." - SC3005: - description: "arithmetic for loops are not supported in dash and undefined in POSIX sh." - SC3007: - description: "$[..] in place of $((..)) is not supported in dash and undefined in POSIX sh." - SC3008: - description: "select loops are not supported in dash and undefined in POSIX sh." - SC3009: - description: "brace expansion is not supported in dash and undefined in POSIX sh." - SC3011: - description: "here-strings are not supported in dash and undefined in POSIX sh." - SC3016: - description: "unary -v (in place of [ -n \"${var+x}\" ]) is not supported in dash and undefined in POSIX sh." - SC3029: - description: "|& in place of 2>&1 | is not supported in dash and undefined in POSIX sh." - SC3030: - description: "arrays are not supported in dash and undefined in POSIX sh." - SC3032: - description: "coproc is not supported in dash and undefined in POSIX sh." - SC3033: - description: "naming functions outside [a-zA-Z_][a-zA-Z0-9_]* is not supported in dash and undefined in POSIX sh." - SC3036: - description: "echo flags besides -n are not supported in dash and undefined in POSIX sh." - SC3041: - description: "only set flags -a, -b, -C, -e, -f, -h, -m, -n, -u, -v, -x, -o are supported in dash or defined in POSIX sh." - SC3042: - description: "double-dashed set flags are not supported in dash and undefined in POSIX sh." - SC3051: - description: "'source' in place of '.' is not supported in dash and undefined in POSIX sh." - SC3052: - description: "arithmetic base conversion is not supported in dash and undefined in POSIX sh." + SC2240: + severity: warning # Not detected by grep + SC3053: + severity: warning # Not detected by grep + SC3054: + severity: warning # Not detected by grep + SC3055: + severity: warning # Not detected by grep + SC3056: + severity: warning # Not detected by grep + SC3057: + severity: warning # Not detected by grep SC3058: - description: "string operations on $@/$* are not supported in dash and undefined in POSIX sh." - SC1144: - description: "external-sources can only be enabled in .shellcheckrc, not in individual files." - SC1145: - description: "Unknown external-sources value. Expected true/false." - SC2306: - description: "Escape glob characters in arguments to expr to avoid pathname expansion." \ No newline at end of file + description: "In POSIX sh, string operations on $@/$* are not supported" # Not in the Wiki + SC3059: + severity: warning # Not detected by grep + SC3061: + description: "read without a variable is" # Not in the Wiki + diff --git a/src/main/scripts/get_checks.sh b/src/main/scripts/get_checks.sh index 2fffd4d..5912f3e 100755 --- a/src/main/scripts/get_checks.sh +++ b/src/main/scripts/get_checks.sh @@ -2,4 +2,4 @@ TARGET_DIR=org/sonar/l10n/shellcheck/rules/shellcheck mkdir -p `dirname $0`/../resources/$TARGET_DIR -docker run --rm -v `pwd`/..:/mnt python:3.7-alpine3.12 sh /mnt/scripts/build_checks.sh /mnt/resources/$TARGET_DIR +docker run --rm -v /tmp:/tmp -v `pwd`/..:/mnt python:3.7-alpine3.12 sh /mnt/scripts/build_checks.sh /mnt/resources/$TARGET_DIR