• conditional_soup@lemm.ee
    link
    fedilink
    arrow-up
    91
    arrow-down
    7
    ·
    edit-2
    1 year ago

    Regex

    Edit: to everyone who responded, I use regex infrequently enough that the knowledge never really crystalizes. By the time I need it for this one thing again, I haven’t touched it in like a year.

    • HyperMegaNet@lemm.ee
      link
      fedilink
      arrow-up
      7
      ·
      1 year ago

      Thank you for this. About a year ago I came across ShellCheck thanks to a comment just like this on Reddit. I also happened to be getting towards the end of a project which included hundreds of lines of shell scripts across dozens of files.

      It turns out that despite my workplace having done quite a bit of shell scripting for previous projects, no one had heard about Shell Check. We had been using similar analysis tools for other languages but nothing for shell scripts. As you say, it turned up a huge number of errors, including some pretty spicy ones when we first started using it. It was genuinely surprising to see how many unique and terrible ways the scripts could have failed.

    • ethancedwards8@programming.dev
      link
      fedilink
      English
      arrow-up
      5
      ·
      1 year ago

      I wish it had a more comprehensive auto correct feature. I maintain a huge bash repository and have tried to use it, and it common makes mistakes. None of us maintainers have time to rewrite the scripts to match standards.

      • I honestly think autocorrecting your scripts would do more harm than good. ShellCheck tells you about potential issues, but It’s up to you to determine the correct behavior.

        For example, how could it know whether cat $foo should be cat "$foo", or whether the script actually relies on word splitting? It’s possible that $foo intentionally contains multiple paths.

        Maybe there are autofixable errors I’m not thinking of.

        FYI, it’s possible to gradually adopt ShellCheck by setting --severity=error and working your way down to warnings and so on. Alternatively, you can add one-off #shellcheck ignore SC1234 comments before offending lines to silence warnings.

        • UndercoverUlrikHD@programming.dev
          link
          fedilink
          arrow-up
          4
          ·
          1 year ago

          For example, how could it know whether cat $foo should be cat "$foo", or whether the script actually relies on word splitting? It’s possible that $foo intentionally contains multiple paths.

          Last time I used ShellCheck (yesterday funnily enough) I had written ports+=($(get_elixir_ports)) to split the input since get_elixir_ports returns a string of space separated ports. It worked exactly as intended, but ShellCheck still recommended to make the splitting explicit rather than implicit.

          The ShellCheck docs recommended

          IFS=" " read -r -a elixir_ports <<< "(get_elixir_ports)"
          ports+=("${elixir_ports[@]}")
          
      • stetech@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Then you’ll have to find the time later when this leads to bugs. If you write against bash while declaring it POSIX shell, but then a random system’s sh doesn’t implement a certain thing, you’ll be SOL. Or what exactly do you mean by “match standards”?

    • CrazyLikeGollum@lemmy.world
      link
      fedilink
      English
      arrow-up
      12
      ·
      1 year ago

      Or scripts for basically any other variant of the Bourne shell. They are, for the most part, very cross compatible.

      • Tinidril@midwest.social
        link
        fedilink
        English
        arrow-up
        9
        ·
        1 year ago

        That’s the only reason I’ve ever done much of anything in shell script. As a network administrator I’ve worked many network appliances running on some flavor of Unix and the one language I can count on to be always available is bash. It has been well worth knowing for just that reason.

      • BeigeAgenda@lemmy.ca
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        I wrote a script to do backups on a ESXi it uses Busybox’s ASH, one thing I learned after spending hours debugging my scripts was that ASH does not support arrays so you have to do everything with temporary files.

        • YouAreLiterallyAnNPC@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          1 year ago

          There actually is an array in any POSIX shell. You get one array per file/function. It just feels bad to use it. You can abuse ‘set – 1 2 3 4’ to act as a proper array. You can then use ‘for’ without ‘in’ to iterate over it.

          for i; do echo $i; done.

          Use shift <number> to pop items off.

          If I really have to use something more complex, I’ll reach for mkfifo instead so I can guarantee the data can only be consumed once without manipulating entries.

  • perishthethought@lemm.ee
    link
    fedilink
    English
    arrow-up
    34
    arrow-down
    5
    ·
    1 year ago

    I don’t normally say this, but the AI tools I’ve used to help me write bash were pretty much spot on.

    • SpaceNoodle@lemmy.world
      link
      fedilink
      arrow-up
      12
      ·
      1 year ago

      Yeah, an LLM can quickly parrot some basic boilerplate that’s showed up in its training data a hundred times.

    • henfredemars@infosec.pub
      link
      fedilink
      English
      arrow-up
      4
      ·
      1 year ago

      For building a quick template that I can tweak to my needs, it works really well. I just don’t find it to be an intuitive scripting language.

  • Rose@lemmy.world
    link
    fedilink
    arrow-up
    25
    arrow-down
    1
    ·
    1 year ago

    There’s always the old piece of wisdom from the Unix jungle: “If you write a complex shellscript, sooner or later you’ll wish you wrote it in a real programming language.”

    I wrote a huge PowerShell script over the past few years. I was like “Ooh, guess this is a resume item if anyone asks me if I know PowerShell.” …around the beginning of the year I rewrote the bloody thing in Python and I have zero regrets. It’s no longer a Big Mush of Stuff That Does a Thing. It’s got object orientation now. Design patterns. Things in independent units. Shit like that.

  • Victor@lemmy.world
    link
    fedilink
    arrow-up
    24
    ·
    1 year ago

    Ever since I switched to Fish Shell, I’ve had no issues remembering anything. Ported my entire catalogue of custom scripts over to fish and everything became much cleaner. More legible, and less code to accomplish the same things. Easier argument parsing, control structures, everything. Much less error prone IMO.

    Highly recommend it. It’s obviously not POSIX or anything, but I find that the cost of installing fish on every machine I own is lower than maintaining POSIX-compliant scripts.

    Enjoy your scripting!

    • UndercoverUlrikHD@programming.dev
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      If you’re going to write scripts that requires installing software, might as well use something like python though? Most Linux distros ship also ship with python installed

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        A shell script can be much more agile, potent, and concise, depending on the use case.

        E.g. if you want to make a facade (wrapper) around a program, that’s much cleaner in $SHELL. All you’re doing is checking which keyword/command the user wanted, and then executing the commands associated with what you want to achieve, like maybe displaying a notification and updating a global environment variable or something.

        Executing a bunch of commands and chaining their output together in python is surely much more cumbersome than just typing them out next to each other separated by a pipe character. It’s higher-level. 👍

        If it’s just text in text out though, sure, mostly equivalent, but for me this is rarely the use case for a script.

        • UndercoverUlrikHD@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          I’m not anti bash or fish, I’ve written in both just this week, but if we’re talking about readability/syntax as this post is about, and you want an alternative to bash, I’d say python is a more natural alternative. Fish syntax is still fairly ugly compared to most programming languages in my opinion.

          Different strokes for different folks I suppose.

          • Victor@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            Fish syntax is still fairly ugly compared to most programming languages in my opinion.

            subprocess.run(["fd", "-t", "d", "some_query"])
            

            vs

            fd -t d some_query
            

            Which is cleaner? Not to mention if you want to take the output from the command and pipe it into another one.

            It’s not about folks with weird opinions or otherwise, it’s about use cases. 🙂 I don’t think python is any more “natural” than most other imperative languages.

            Fish is probably even more natural, actually, due to it being more high level and the legibility of the script is basically dependent on the naming of the commands and options and variables used within it, rather than something else, just like python. They probably have similarly legible keywords. Fish I imagine has fewer, which is a good thing for legibility. A script does a lot more with a lot less, due to the commands themselves doing so much behind the scenes. There’s a lot more boilerplate to a “proper” programming language than a scripting language.

            But if you want to do something that python is better suited for, like advanced data processing or number crunching, or writing a whole application, then I would say that would be the better choice. It’s not about preference for me when it comes to python vs fish, it’s about the right tool for the job. But if we’re talking about bash vs fish, then I’m picking fish purely by preference. 👍

    • raldone01@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      1 year ago

      I love fish but sadly it has no proper equivalent of set -e as far as I know.

      ; or return; in every line is not a solution.

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Yeah I also went bash -> zsh -> fish. Zsh was just too complicated to configure for my taste. Couldn’t do it, apart from copy pasting stuff I didn’t understand myself, and that just didn’t sit right.

    • alt_xa_23@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      I switched to fish a while back, but haven’t learned how to script in it yet. Sounds like I should learn

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Give it a shot after reading through the manual! (Extremely short compared to bash’s!) It’s a joy in my opinion. ☺️👌

    • LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      I wish I could but since I use bash at work (often on embedded systems so no custom scripts or anything that isn’t source code) I just don’t want to go back and forth between the two.

  • 74 183.84@lemm.ee
    link
    fedilink
    English
    arrow-up
    11
    ·
    1 year ago

    And I thought I was the only one… for smaller bash scripts chatGPT/Deepseek does a good enough job at it. Though I still haven’t tried VScode’s copilot on bash scripts. I have only tried it wirh C code and it kiiiinda did an ass job at helping…

    • cm0002@lemmy.worldOP
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      AI does decently enough on scripting languages if you spell it out enough for it lol, but IMO it tends to not do so well when it comes to compiled languages

      I’ve tried Python with VScode Copilot (Claude) and it did pretty good

        • cm0002@lemmy.worldOP
          link
          fedilink
          arrow-up
          5
          ·
          1 year ago

          I was chalking it up to some scripting languages just tending to be more popular (like python) and thus having more training data for them to draw from

          But that’s a good point too lol

      • 74 183.84@lemm.ee
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        Yeah I tried that, Claude with some C code. Unfortunately the Ai only took me from point A to point A. And it only took a few hours :D

  • JTskulk@lemmy.world
    link
    fedilink
    English
    arrow-up
    10
    ·
    1 year ago

    Bash was the first language I learned, got pretty decent at it. Now what happens is I think of a tiny script I need to write, I start writing it in Bash, I have to do string manipulation, I say fuck this shit and rewrite in Python lol

    • cm0002@lemmy.worldOP
      link
      fedilink
      arrow-up
      5
      ·
      1 year ago

      For a defacto windows admin my Powershell skills are…embarrassing lol but I’m getting there!

  • Gobbel2000@programming.dev
    link
    fedilink
    arrow-up
    8
    ·
    1 year ago

    So true. Every time I have to look up how to write a bash for loop. Where does the semicolon go? Where is the newline? Is it terminated with done? Or with end? The worst part with bash is that when you do it wrong, most of the time there is no error but something completely wrong happens.

    • ClemaX@lemm.ee
      link
      fedilink
      arrow-up
      10
      ·
      edit-2
      1 year ago

      It all makes sense when you think about the way it will be parsed. I prefer to use newlines instead of semicolons to show the blocks more clearly.

      for file in *.txt
      do
          cat "$file"
      done
      

      The do and done serve as the loop block delimiters. Such as { and } in many other languages. The shell parser couldn’t know where stuff starts/ends.

      Edit: I agree that the then/fi, do/done case/esac are very inconsistent.

      Also to fail early and raise errors on uninitialized variables, I recommend to add this to the beginning of your bash scripts:

      set -euo pipefail
      

      Or only this for regular sh scripts:

      set -eu
      

      -e: Exit on error

      -u: Error on access to undefined variable

      -o pipefail: Abort pipeline early if any part of it fails.

      There is also -x that can be very useful for debugging as it shows a trace of every command and result as it is executed.

    • qjkxbmwvz@startrek.website
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      I can only remember this because I initially didn’t learn about xargs — so any time I need to loop over something I tend to use for var in $(cmd) instead of cmd | xargs. It’s more verbose but somewhat more flexible IMHO.

      So I run loops a lot on the command line, not just in shell scripts.

    • _stranger_@lemmy.world
      link
      fedilink
      arrow-up
      21
      ·
      1 year ago

      It’s more like bash did it one way and everyone who came after decided that was terrible and should be done a different way (for good reason).

      Looking right at you -eq and your weird ass syntax

      if [[ $x -eq $y ]]

  • Cold_Brew_Enema@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    1 year ago

    Me with powershell. I’ll write a pretty complex script, not write powershell for 3 months, come back and have to completely relearn it.

  • 6mementomorib@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    7
    ·
    1 year ago

    i used powershell, and even after trying every other shell and as a die hard Linux user I’ve considered going back to powershell cause damn man

    • ronflex@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      1 year ago

      I am a huge fan of using PowerShell for scripting on Linux. I use it a ton on Windows already and it allows me to write damn near cross-platform scripts with no extra effort. I still usually use a Bash or Fish shell but for scripting I love being able to utilize powershell.