Drasil icon indicating copy to clipboard operation
Drasil copied to clipboard

`shellcheck` our sh scripts

Open balacij opened this issue 3 years ago • 2 comments

shellcheck is a nice tool we can use to audit our scripts/*.sh scripts. It is very similar to HLint, in that it audits the scripts and suggests improvements.

balacij avatar Feb 07 '22 19:02 balacij

For example, this is the current results of running shellcheck on the existing .sh scripts.
~/Programming/Drasil/code/scripts$ shellcheck *.sh

In check_stack.sh line 6:
if [ -z $MIN_STACK_VER ]; then
        ^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
if [ -z "$MIN_STACK_VER" ]; then


In check_stack.sh line 19:
  if [ $CACHED = $MIN_STACK_VER ]; then
       ^-----^ SC2086: Double quote to prevent globbing and word splitting.
                 ^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
  if [ "$CACHED" = "$MIN_STACK_VER" ]; then


In check_stack.sh line 32:
MSV_SPACE=$(echo $MIN_STACK_VER | tr "." " ")
                 ^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
MSV_SPACE=$(echo "$MIN_STACK_VER" | tr "." " ")


In check_stack.sh line 33:
MSV_LEN=$(echo $MSV_SPACE | wc -w)
               ^--------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
MSV_LEN=$(echo "$MSV_SPACE" | wc -w)


In check_stack.sh line 34:
CV_SPACE=$(echo $CURRENT_VER | tr "." " ")
                ^----------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
CV_SPACE=$(echo "$CURRENT_VER" | tr "." " ")


In check_stack.sh line 35:
CV_LEN=$(echo $CV_SPACE | wc -w)
              ^-------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
CV_LEN=$(echo "$CV_SPACE" | wc -w)


In check_stack.sh line 38:
for i in $(seq 1 $MSV_LEN); do
                 ^------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
for i in $(seq 1 "$MSV_LEN"); do


In check_stack.sh line 39:
  if [ $i -gt $CV_LEN ]; then
       ^-- SC2086: Double quote to prevent globbing and word splitting.
              ^-----^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
  if [ "$i" -gt "$CV_LEN" ]; then


In check_stack.sh line 44:
  CVV=$(echo $CV_SPACE | cut -d" " -f$i)
             ^-------^ SC2086: Double quote to prevent globbing and word splitting.
                                     ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
  CVV=$(echo "$CV_SPACE" | cut -d" " -f"$i")


In check_stack.sh line 45:
  MSVV=$(echo $MSV_SPACE | cut -d" " -f$i)
              ^--------^ SC2086: Double quote to prevent globbing and word splitting.
                                       ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
  MSVV=$(echo "$MSV_SPACE" | cut -d" " -f"$i")


In check_stack.sh line 46:
  if [ $CVV -ne $MSVV ]; then
       ^--^ SC2086: Double quote to prevent globbing and word splitting.
                ^---^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
  if [ "$CVV" -ne "$MSVV" ]; then


In check_stack.sh line 47:
    if [ $CVV -lt $MSVV ]; then
         ^--^ SC2086: Double quote to prevent globbing and word splitting.
                  ^---^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
    if [ "$CVV" -lt "$MSVV" ]; then


In check_stack.sh line 55:
  echo $MIN_STACK_VER > "$CACHED_MSV_FILE"
       ^------------^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
  echo "$MIN_STACK_VER" > "$CACHED_MSV_FILE"


In code_build.sh line 7:
if [ -z "$EDIR" ]; then
         ^---^ SC2153: Possible misspelling: EDIR may not be assigned, but E_DIR is.


In code_build.sh line 18:
    cd "$EXAMPLE_CODE_SUBFOLDER"
    ^--------------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
    cd "$EXAMPLE_CODE_SUBFOLDER" || exit


In code_build.sh line 21:
      cd "$dr"
      ^------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
      cd "$dr" || exit


In code_build.sh line 25:
      "$MAKE" $TARGET
              ^-----^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
      "$MAKE" "$TARGET"


In code_build.sh line 26:
      RET=$(( $RET || $? ))
              ^--^ SC2004: $/${} is unnecessary on arithmetic variables.


In code_build.sh line 27:
      cd "$OLD_DIR"
      ^-----------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
      cd "$OLD_DIR" || exit


In code_build.sh line 34:
cd "$BUILD_FOLDER$EDIR"
^---------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
cd "$BUILD_FOLDER$EDIR" || exit


In code_build.sh line 38:
    cd "$d"
    ^-----^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
    cd "$d" || exit


In code_build.sh line 40:
    cd "$E_DIR"
    ^---------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
    cd "$E_DIR" || exit


In deploy_stage.sh line 129:
        ls -d "$(cd "$CUR_DIR" && git rev-parse --show-toplevel)/$p"*/ | rev | cut -d/ -f2 | rev | tr '\n' '\0' | xargs -0 printf "$p%s\n" >> "$EXAMPLE_DEST$example_name/src"
        ^-- SC2012: Use find instead of ls to better handle non-alphanumeric filenames.


In deploy_stage.sh line 155:
  cd "$CUR_DIR$DEPLOY_FOLDER"
  ^-------------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
  cd "$CUR_DIR$DEPLOY_FOLDER" || exit


In deploy_stage.sh line 163:
cd "$DEPLOY_FOLDER"
^-----------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
cd "$DEPLOY_FOLDER" || exit


In deploy_stage.sh line 172:
cd "$CUR_DIR"
^-----------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
cd "$CUR_DIR" || exit


In duplicate_string_get_information.sh line 5:
    echo $p >>duplicate_string_summary.txt
         ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
    echo "$p" >>duplicate_string_summary.txt


In duplicate_string_get_information.sh line 27:
    printf '%s\n'>>duplicate_string_summary.txt
           ^----^ SC2183: This format string has 1 variables, but is passed 0 arguments.


In gooltest_build.sh line 1:
if [ -z "$EDIR" ]; then
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
         ^---^ SC2153: Possible misspelling: EDIR may not be assigned, but E_DIR is.


In gooltest_build.sh line 12:
cd "$BUILD_FOLDER$EDIR"
^---------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
cd "$BUILD_FOLDER$EDIR" || exit


In gooltest_build.sh line 15:
  cd "$lang"
  ^--------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
  cd "$lang" || exit


In gooltest_build.sh line 18:
    cd "$test"
    ^--------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
    cd "$test" || exit


In gooltest_build.sh line 19:
    "$MAKE" $TARGET
            ^-----^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean: 
    "$MAKE" "$TARGET"


In gooltest_build.sh line 20:
    RET=$(( $RET || $? ))
            ^--^ SC2004: $/${} is unnecessary on arithmetic variables.


In gooltest_build.sh line 21:
    cd "$LANG_DIR"
    ^------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
    cd "$LANG_DIR" || exit


In gooltest_build.sh line 23:
  cd "$E_DIR"
  ^---------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
  cd "$E_DIR" || exit


In make_docs.sh line 26:
if [ "$?" -eq "0" ]; then
     ^--^ SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.


In make_docs.sh line 51:
if [ "$?" -eq "0" ]; then
     ^--^ SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.


In mock_deploy.sh line 1:
# For recreating the deployment on a local machine.
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.


In tex_build.sh line 27:
cd "$BUILD_FOLDER$EDIR"/SRS/PDF
^-----------------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
cd "$BUILD_FOLDER$EDIR"/SRS/PDF || exit


In tex_build.sh line 32:
  echo "\n\n\033[0;33m$EDIR TeX Summary\033[0m:"
       ^-- SC2028: echo may not expand escape sequences. Use printf.


In tex_build.sh line 35:
    cat "$EDIR$GEN_NAME_SUFFIX".log | grep -E "erfull|Warning"
        ^-------------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.


In tex_build.sh line 36:
    cat "$EDIR$GEN_NAME_SUFFIX".blg | grep -B3 -E "Error"
        ^-------------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.


In tex_build.sh line 37:
    BIBERRS=$(cat "$EDIR$GEN_NAME_SUFFIX".blg | grep -E "Error" | wc -l)
                  ^-------------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
                                                ^-------------^ SC2126: Consider using grep -c instead of grep|wc -l.

For more information:
  https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y...
  https://www.shellcheck.net/wiki/SC2153 -- Possible misspelling: EDIR may no...
  https://www.shellcheck.net/wiki/SC2164 -- Use 'cd ... || exit' or 'cd ... |...

balacij avatar Feb 07 '22 19:02 balacij

Great idea @balacij.

smiths avatar Feb 07 '22 20:02 smiths