25 votes

ShellCheck: a static analysis tool for shell scripts

18 comments

  1. [2]
    hungariantoast
    Link
    As for the incident itself, at first we thought it was a standard core barrier failure. A hole opens in the Firma, and exposes the upper layers to the core. Anything within the path of the extreme...
    6 votes
    1. valar
      Link Parent
      We may need the tildes equivalent of r/outoftheloop :D

      We may need the tildes equivalent of r/outoftheloop :D

      4 votes
  2. [11]
    chundissimo
    Link
    Okay finally a BASH post that doesn’t give me mild nausea! Shellcheck has really changed my experience of BASH. I’ve always used it since beginning my programming journey, but quickly I was made...

    Okay finally a BASH post that doesn’t give me mild nausea! Shellcheck has really changed my experience of BASH. I’ve always used it since beginning my programming journey, but quickly I was made aware of how brutal its footguns can be and how arcane its syntax can be. Shellcheck gives me so much peace of mind that the scripts I write likely don’t have land mines in them.

    I used to avoid using BASH for anything beyond small, personal scripts, but now I’ve used it in much broader contexts including in glueing together some of my production systems at my job. BASH has its limits (although @hungariantoast seems to be on a crusade to disprove that), but it really is an indispensable tool in a programmer’s tool belt and Shellcheck feels like a nearly mandatory auxiliary to me.

    5 votes
    1. [10]
      first-must-burn
      Link Parent
      After years of shell checking scripts, I almost always correctly quote variables on my own now. But it's still a very useful tool. 10/10 would install again

      After years of shell checking scripts, I almost always correctly quote variables on my own now. But it's still a very useful tool. 10/10 would install again

      3 votes
      1. [9]
        davek804
        Link Parent
        'i almost always don't forget how to do the basics of my job' - rephrasing what you've said into my own words. I, however, regularly forget. It hurts. Love me some bash.

        'i almost always don't forget how to do the basics of my job' - rephrasing what you've said into my own words. I, however, regularly forget. It hurts. Love me some bash.

        3 votes
        1. [6]
          first-must-burn
          Link Parent
          The thing about bash is: it's nobody's job. It's more like if all the jobs had elevators that you had to program in order to get to your office. It's not on anybody's resume. Nobody takes a class...

          The thing about bash is: it's nobody's job.

          It's more like if all the jobs had elevators that you had to program in order to get to your office. It's not on anybody's resume. Nobody takes a class on it. Nobody even mentions it. But everybody has to do it.

          Oh and by the way all the elevators (shells) are slightly different. So most of the time you get to your floor no matter which kind of elevator it is, but sometimes it's an unexpectedly old elevator that plummets to the basement (sh).

          3 votes
          1. [5]
            davek804
            Link Parent
            Nothing like when you get stuck on the sub basement sh container with just a flashlight and a can of bug spray.

            Nothing like when you get stuck on the sub basement sh container with just a flashlight and a can of bug spray.

            3 votes
            1. [4]
              first-must-burn
              Link Parent
              Scary stories to tell in the dark: "You have to fix something on this ancient and critical solaris system. There is no outside internet access. Vi is the only editor available. You don't remember...

              Scary stories to tell in the dark: "You have to fix something on this ancient and critical solaris system. There is no outside internet access. Vi is the only editor available. You don't remember the escape sequence to exit it."

              4 votes
              1. [3]
                Weldawadyathink
                Link Parent
                Enter a valid tar command on your first try.
                4 votes
                1. [2]
                  first-must-burn
                  Link Parent
                  I know tar xzvf filename to untar tar czvf filename path/to/add to tar and zip Replace the z with a j for bzip instead of grip (obviously) I also know that if you leave the filename out of the...

                  I know

                  • tar xzvf filename to untar
                  • tar czvf filename path/to/add to tar and zip
                  • Replace the z with a j for bzip instead of grip (obviously)

                  I also know that if you leave the filename out of the second command and use a wildcard in the path, you'll overwrite the first file you want to add with a tarball of the rest of them.

                  1. bme
                    Link Parent
                    Minor fun tar fact: when extracting you can leave off the compression flags, it will figure them out itself.

                    Minor fun tar fact: when extracting you can leave off the compression flags, it will figure them out itself.

                    2 votes
        2. [2]
          first-must-burn
          Link Parent
          I feel like my other reply was laden with snark, so I also wanted to say I like your rephrasing. I work in a pretty low trust environment, or I'd happily have that on a t-shirt.

          I feel like my other reply was laden with snark, so I also wanted to say I like your rephrasing. I work in a pretty low trust environment, or I'd happily have that on a t-shirt.

          1 vote
          1. davek804
            Link Parent
            I completely picked up and enjoyed your snark!

            I completely picked up and enjoyed your snark!

            1 vote
  3. [4]
    goose
    Link
    I love shellcheck! It's a staple in my ability to bash script. Although it did fail me, once. I was working on a multi-hundred line bash script, with some big changes that required significant...

    I love shellcheck! It's a staple in my ability to bash script. Although it did fail me, once.

    I was working on a multi-hundred line bash script, with some big changes that required significant edits that couldn't be tested until I had re-written many many lines. Upon running the script, I got a syntax error. But shellcheck reported no such errors. I was stumped. 15 minutes of Google and Stack Overflow later, I was led to believe that I had dropped a character relating to defining something, somewhere.

    So, I wrote another bash script, find_mismatch.bash:

    #!/bin/bash
    
    # Provide an input file, get a breakdown of lines that do not have matching single ticks, square brackets, or curly brackets
    
    n="1"
    
    while read -r line; do
        unset footer
        count="0"
        for (( i=0; i<${#line}; i++ )); do
            if [[ "${line:${i}:1}" == "[" ]]; then
                (( count++ ))
                footer="${footer}${line:${i}:1}"
            elif [[ "${line:${i}:1}" == "]" ]]; then
                (( count++ ))
                footer="${footer}${line:${i}:1}"
            elif [[ "${line:${i}:1}" == "{" ]]; then
                (( count++ ))
                footer="${footer}${line:${i}:1}"
            elif [[ "${line:${i}:1}" == "}" ]]; then
                (( count++ ))
                footer="${footer}${line:${i}:1}"
            elif [[ "${line:${i}:1}" == "'" ]]; then
                (( count++ ))
                footer="${footer}${line:${i}:1}"
            else
                footer="${footer} "
            fi
        done
        if [[ "${count}" -ne "0" ]]; then
            if ! [[ "$((count%2))" -eq "0" ]]; then
                echo "Line ${n}"
                echo "${line}"
                echo "${footer}"
                echo ""
                read -n 1 -s -r -p "Press any key to continue" </dev/tty
            fi
        fi
        (( n++ ))
    done < "${1}"
    echo ""
    exit 0
    

    And set it loose on my original multi-hundred line script. Sure enough, I found an associate array variable incorrectly defined:

    fileRatingKey["${fileId}"="${ratingKey}"
                             ^
    

    With a missing closing ] as indicated above. Surprisingly, shellcheck couldn't/wouldn't pick this up and alert me to it. But other than that one time it failed me, it's been an absolute staple for me. Extremely helpful tool.

    3 votes
    1. [3]
      first-must-burn
      Link Parent
      I like it! Though it seems silly to me that you wrote the second script when you could easily have just sacrificed a goat to the gods of sed and done it in a one-liner ;) I am curious whether...

      I like it! Though it seems silly to me that you wrote the second script when you could easily have just sacrificed a goat to the gods of sed and done it in a one-liner ;)

      I am curious whether Claude Sonnet or ChatGPT would be able to find it. They aren't good at everything, but they seem pretty good at that kind of thing.

      1 vote
      1. [2]
        goose
        Link Parent
        Trouble is, I was fresh out of goats, I just sold them all to Lumon

        when you could easily have just sacrificed a goat to the gods of sed

        Trouble is, I was fresh out of goats, I just sold them all to Lumon

        4 votes
        1. first-must-burn
          Link Parent
          I think this is a Severance reference? I really need to get my alternative media sources sorted out so I can watch that.

          I think this is a Severance reference? I really need to get my alternative media sources sorted out so I can watch that.

          3 votes
  4. davek804
    Link
    I find myself on Shell check more frequently than I care to admit. I love it. And I love disagreeing with its suggestions out of sheer spite. /S I had no idea I could install it too. Thanks for...

    I find myself on Shell check more frequently than I care to admit. I love it.

    And I love disagreeing with its suggestions out of sheer spite. /S

    I had no idea I could install it too. Thanks for sharing.

    2 votes