I've been struggling with a rather complex shell script, and it's becoming apparent that Bash might not be the best choice for this particular task. While I usually gravitate towards statically typed languages like Go or Rust, I've noticed that many people recommend alternative languages such as Lua or Python for scripting tasks.

I'm curious to know your opinions and experiences with scripting languages for larger or more intricate shell scripts. Have you ever encountered a situation where Bash just didn't cut it, and if so, which scripting languages did you turn to for a more effective solution? Are there any specific languages you found particularly suitable for debugging, testing, or handling complex logic in your shell scripts?

  • swordsmanluke@programming.dev
    ·
    1 year ago

    Aw man, you can't write all that and then not give an example!

    Ruby makes scripting drop-dead simple. You can run any shell command by surrounding it with back ticks.

    # simple example, just grab files: 
    files = `ls`.split("\n")
    
    # pipes work inside back ticks
    files.map {|f| `cat #{f} | grep "can I use grep w/out cat"`}
      .compact
      .each { |match| puts match }
    # easy to build a pipeline on the data in ruby, too! 
    

    That's it! No messing around with popen3, or figuring out pipes or signals. Those are there too if you really need them, but if you just wanna write a quick script with a less arcane syntax - try Ruby!

    • snowe@programming.devM
      ·
      1 year ago

      Haha sorry, I wrote it all on my phone while traveling. and yeah, if you're running just shell commands it looks almost the exact same as a bash script, and then when you need actual scripting capabilities you get them.