• wwb4itcgas@lemm.ee
    link
    fedilink
    English
    arrow-up
    1
    ·
    14 hours ago

    I have a confession to make: Unless shell script is absolutely required, I just use Python for all my automation needs.

  • JTskulk@lemmy.world
    link
    fedilink
    English
    arrow-up
    4
    ·
    4 days 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

  • conditional_soup@lemm.ee
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    4 days 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.

    • 9point6@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      4 days ago

      You get used to it, I don’t even see the code—I just see: group… pattern… read-ahead…

    • Kissaki@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      arrow-down
      1
      ·
      4 days ago

      You always forget regex syntax?

      I’ve always found it simple to understand and remember. Even over many years and decades, I’ve never had issues reading or writing simple regex syntax (excluding the flags and shorthands) even after long regex breaks.

      • Akito@lemmy.zip
        link
        fedilink
        English
        arrow-up
        0
        ·
        4 days ago

        It’s not about the syntax itself, it’s about which syntax to use. There are different ones and remembering which one is for which language is tough.

        • Lehmanator@programming.dev
          link
          fedilink
          English
          arrow-up
          1
          ·
          3 days ago

          This is exactly it. Regex is super simple. The difficulty is maintaining a mental mapping between language/util <-> regex engine <-> engine syntax & character class names. It gets worse when utils also conditionally enable extended syntaxes with flags or options.

          The hardest part is remembering whether you need to use \w or [:alnum:].

          Way too few utils actually mention which syntax they use too. Most just say something accepts a “regular expression”, which is totally ambiguous.

    • kameecoding@lemmy.world
      link
      fedilink
      arrow-up
      0
      arrow-down
      1
      ·
      4 days ago

      Most of regex is pretty basic and easy to learn, it’s the look ahead and look behind that are the killers imo

    • HyperMegaNet@lemm.ee
      link
      fedilink
      arrow-up
      1
      ·
      4 days 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.

  • Victor@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    4 days 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!

  • LeninOnAPrayer@lemm.ee
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    4 days ago

    The sad thing is that even chatgpt can’t program in bash. I just want a simple script and every single time it just doesn’t work. I always just end up saying “write this in python instead”.

  • Pixelbeard@lemmy.ca
    link
    fedilink
    Français
    arrow-up
    1
    ·
    4 days ago

    Je comprend tellement! Je répond en français pour ma première réponse sur Lemmy juste pour voir comment ça va être géré!

      • Pixelbeard@lemmy.ca
        link
        fedilink
        arrow-up
        2
        ·
        2 days ago

        En un mundo ideal. Todo se traduciría automáticamente del idioma original al idioma del lector y viceversa

        • admin@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          2 days ago

          ¿No nos volvería lentos y flojonazos? (not a real word if you translate, more like slang meaning to be really lazy)

  • umbraroze@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    4 days 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.

  • coldsideofyourpillow@lemmy.cafe
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    4 days ago

    That’s why I use nushell. Very convenient for writing scripts that you can understand. Obviously, it cannot beat Python in terms of prototyping, but at least I don’t have to relearn it everytime.

    • Akito@lemmy.zip
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 days ago

      Nu is great. Using it since many years. Clearly superior shell. Only problem is, that it constantly faces breaking changes and you therefore need to frequently update your modules.

        • Akito@lemmy.zip
          link
          fedilink
          English
          arrow-up
          1
          ·
          4 days ago

          Yesterday, I upgraded from 0.101.0 to 0.102.0 and date to-table was replaced equally (actually better) with into record, however it was not documented well in the error. Had to research for 5 to 10 minutes, which does not sound much, but if you get this like every second version, the amount of time adds up quickly.

        • barsoap@lemm.ee
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          4 days ago

          Not really. They’ve been on the stabilising path for about two years now, removing stuff like dataframes from the default feature set to be able to focus on stabilising the whole core language, but 1.0 isn’t out yet and the minor version just went three digits.

          And it’s good that way. The POSIX CLI is a clusterfuck because it got standardised before it got stabilised. dd’s syntax is just the peak of the iceberg, there, you gotta take out the nail scissors and manicure the whole lawn before promising that things won’t change.

          Even in its current state it’s probably less work for many scripts, though. That is, updating things, especially if you version-lock (hello, nixos) will be less of a headache than writing sh could ever be. nushell is a really nice language, occasionally a bit verbose but never in the boilerplate for boilerplate’s sake way, but in the “In two weeks I’ll be glad it’s not perl” way. Things like command line parsing are ludicrously convenient (though please nushell people land collecting repeated arguments into lists).

    • AnUnusualRelic@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      4 days ago

      So the alternative is:

      • either an obtuse script that works everywhere, or
      • a legible script that only works on your machine…
      • shortrounddev@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        4 days ago

        I am of the opinion that production software shouldn’t be written in shell languages. If it’s something which needs to be redistributed, I would write it in python or something

        • Hexarei@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          4 days ago

          I tend to write anything for distribution in Rust or something that compiles to a standalone binary. Python does not an easily redistributable application make lol

        • AnUnusualRelic@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          4 days ago

          For a bit of glue, a shell script is fine. A start script, some small utility gadget…

          With python, you’re not even sure that the right version is installed unless you ship it with the script.

  • Gobbel2000@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    4 days 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
      1
      ·
      edit-2
      4 days 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.

  • brokenlcd@feddit.it
    link
    fedilink
    arrow-up
    1
    ·
    4 days ago

    Knowing that there is still a bash script i wrote around 5 years ago still running the entirety of my high scool lab makes me sorry for the poor bastard that will need to fix those hieroglyphs as soon as some package breaks the script. I hate that i used bash, but it was the easiest option at the time on that desolate server.

    • formulaBonk@lemm.ee
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 days ago

      Bash scripts survive because often times they are the easiest option on an abandoned server