• synae[he/him]@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 month ago

    Incredibly true for me these days. But don’t fret, shellcheck and tldp.org is all you need. And maybe that one stackoverflow answer about how to get the running script’s directory

  • Cold_Brew_Enema@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 month 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.

    • Kissaki@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 month 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
        ·
        1 month 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
          0
          ·
          1 month 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.

          • ewenak@jlai.lu
            link
            fedilink
            arrow-up
            0
            ·
            1 month ago

            There is the “very magic” mode for vim regexes. It’s not the exact PCRE syntax, but it’s pretty close. You only need to add \v before the expression to use it. There is no permanent mode / option though. (I think you can remap the commands, like / to /\v)

    • kameecoding@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

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

    • 9point6@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

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

        • activ8r@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          0
          ·
          1 month ago

          I know that LLMs are probably very helpful for people who are just getting started, but you will never understand it if you can’t grasp the fundamentals. Don’t let “AI” make you lazy. If you do use LLMs make sure you understand the output it’s giving you enough to replicate it yourself.

          This may not be applicable to you specifically, but I think this is nice info to have here for others.

  • perishthethought@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 month ago

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

  • Rose@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 month 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.

  • KazuchijouNo@lemy.lol
    link
    fedilink
    arrow-up
    0
    ·
    1 month ago

    Today I tried to write bash (I think)

    I grabbed a bunch of commands, slapped a bunch of “&&” to string them together and saved them to a .sh file.

    It didn’t work as expected and I did not, at all, look at any documentation during the process. (This is obviously on me, I’ll try harder next time)

    • DeRp_DaWg@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

      I try to remember to use man when learning a new command/program. And I almost always half-ass it and press the search button immediately to find whatever flag i need.

    • CrazyLikeGollum@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 month 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
        0
        ·
        1 month 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
        0
        ·
        1 month 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
          0
          ·
          1 month 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.

      • traches@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        0
        ·
        edit-2
        1 month ago

        I’d been considering it for awhile, but thought it wasn’t worth the trouble of switching until I realized just how often I do things the tedious manual way because writing a bash script to do it is so arcane

  • Victor@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 month 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!

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

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

    • raldone01@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

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

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

    • UndercoverUlrikHD@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      1 month 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
        0
        ·
        edit-2
        1 month 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
          0
          ·
          1 month 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.

    • LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 month 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.

  • coldsideofyourpillow@lemmy.cafe
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 month ago

    That’s why I use nushell. Very convinient 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
      0
      ·
      1 month 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
          0
          ·
          1 month 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
          0
          ·
          edit-2
          1 month 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).

    • expr@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

      We have someone at work who uses it and he’s constantly having tooling issues due to compatibility problems, so… yeah.

      I’m sure it’s fine for sticking in the shebang and writing your own one-off personal scripts, but I would never actually main it. Too much ecosystem relies on bash/posix stuff.

    • AnUnusualRelic@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 month 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
        0
        ·
        1 month 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
          0
          ·
          1 month 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

          • shortrounddev@lemmy.world
            link
            fedilink
            English
            arrow-up
            0
            ·
            edit-2
            1 month ago

            Yeah but then you either need to compile and redistribute binaries for several platforms, or make sure that each target user has rust/cargo installed. Plus some devs don’t trust compiled binaries in something like an npm package

        • AnUnusualRelic@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          1 month 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.