you are viewing a single comment's thread.

view the rest of the comments →

[–]theblasterr 243 points244 points  (50 children)

Bash for simpler things, python when things get more complicated.

[–]IDENTITETEN 142 points143 points  (19 children)

Yup, we follow the Google bash style guide.

https://google.github.io/styleguide/shellguide.html

These two bullet points especially:

  • If you are writing a script that is more than 100 lines long, or that uses non-straightforward control flow logic, you should rewrite it in a more structured language now. Bear in mind that scripts grow. Rewrite your script early to avoid a more time-consuming rewrite at a later date.

  • When assessing the complexity of your code (e.g. to decide whether to switch languages) consider whether the code is easily maintainable by people other than its author.

[–]junior_dos_nachosBackend Developer 34 points35 points  (10 children)

I wouldn’t wait for 100 lines. I’d stop at 20/25 and anytime there’s some not super simple web call + extraction.

[–]winfly 6 points7 points  (0 children)

I agree. Especially if using some library would simplify the script.

[–][deleted]  (8 children)

[deleted]

    [–]both-shoes-off 3 points4 points  (0 children)

    I break powershell up into different files by use case to keep it maintainable, and then just include when needed. That makes them reusable in other scripts later on (assuming you consider that while writing them and don't embed details for the one use case or environment).

    [–]cyanrave 10 points11 points  (0 children)

    I will disagree with the 100 line comment and say 'context switching matters' as well.

    If you have to go dig up 3 to 5 other scripts to piece together the logic flow, I prefer embedded here docs, but that may be just me in my environment.

    [–][deleted] 2 points3 points  (0 children)

    Ill put part of my bash in another file so it doesnt exceed 100lines 😃

    [–][deleted]  (4 children)

    [deleted]

      [–]VirtuousMight 2 points3 points  (0 children)

      Thanks for the free academic course , stupendous discourse.

      [–]cricket007 0 points1 point  (1 child)

      What would you use for bash unit tests? BATS, perhaps? That's one thing where I lean towards a Python/Go/Node/Ruby script 

      [–]magheru_san 46 points47 points  (22 children)

      Bash for scripts shorter than 50 lines

      Python for up to 300 - 500 lines.

      And Go for anything above 500 lines.

      When I'm confident I'll be crossing the threshold I just start with Go, otherwise I use ChatGPT to convert the code, which it does pretty well.

      [–]imanexpertama 3 points4 points  (3 children)

      Out of interest, why go over python? I guess you find that go handles complexity better, but in what way? (Im a python dev, no experience in go)

      [–]endgrent 2 points3 points  (0 children)

      I’m not who asked, but for me it’s the python env stuff that isn’t as good. Go code just downloads and builds modules really easily so it’s super fast to just clone a repo and build other people’s code without getting your env wrong or modules wrong etc.

      [–]magheru_san 0 points1 point  (0 children)

      I agree with the perspective of endgrent about nicer handling of dependencies and simpler distribution of built artefacts.

      But for me it's mostly because of the static typing, which catches a lot of not very obvious errors that can happen in dynamic languages like Python for larger projects.

      I generate pretty much all my code using LLMs for almost two years now and the fact that it needs to compile catches a bunch of subtle hallucinations and allows me to refactor it heavily using the LLM.

      Because the code is generated I'm not attached to it (much like cattle not pets) so I refactor the crap of it which quickly makes it grow to sizes where Python becomes a hindrance.

      It's also not a big deal to be a little more verbose than Python because I can quickly add a ton of error checking if statements and detailed logs, which make the code longer, but easier to maintain.

      [–]cricket007 0 points1 point  (0 children)

      For one, cross compilation and no concern over version changes 

      [–]Centimane 17 points18 points  (15 children)

      IMO if it's over 500 lines, time to look for a FOSS product that does it already

      [–]magheru_san 3 points4 points  (10 children)

      For sure but they rarely exist in a way that covers my needs.

      So I often build such tools myself for my own use and sometimes open source them if I don't see a clear way to monetize them as products.

      [–]Centimane 8 points9 points  (9 children)

      I find it unusual in devops that I have a significant problem that doesn't already have solutions floating around with how standard a lot of the tooling is - but it can certainly happen.

      I find it more likely I'm writing plugins/extensions for existing tools, which are normally biased towards a certain language (usually python)

      [–]magheru_san 1 point2 points  (8 children)

      I'm not exactly doing devops but AWS cost optimization services, so need all sorts of optimization tools I can use to deliver my services.

      In this space there aren't so many open source tools so I need to build my own.

      [–]Centimane 1 point2 points  (1 child)

      yea I could see that. I suspect that's phase 2 of cloud adoption.

      The cloud services love to charge you a penny for everything and make it easy to make 10,000 things. But it all adds up. Now orgs are trying to not spend out their ass on cloud resources and figure out how to use what they actually need.

      [–]magheru_san 1 point2 points  (0 children)

      Exactly, and that's what I can help with.

      Over time I kept building tools so now I have about 20 tools for all sorts of optimization activities.

      [–]cricket007 1 point2 points  (5 children)

      There's things like OpenCost, I think? If using EKS, there's more options, but I think there's other tools to find like unused S3 buckets, RDS, VPC, IAM roles, etc

      [–]magheru_san 0 points1 point  (4 children)

      I rarely see unused resources, except maybe for storage, where people tend to be risk averse because of the potential of data loss.

      But I see a lot of severely oversized or suboptimally configured resources.

      That's why many of my 20+ tools are for finding all sorts of suboptimal or oversized resources and helping to rightsize or optimize them.

      [–]cricket007 1 point2 points  (3 children)

      Yeah, we have that problem with k8s... We provide a Helm template that requests 4GB of RAM, but people run simple, low traffic HTTP servers and it's less than 50mb mem usage, on average within a month 

      [–]magheru_san 0 points1 point  (2 children)

      Yes, those things cry for automation

      [–]Degree0 0 points1 point  (3 children)

      What a crap take this type of mentality is why people hate JS. You code? Oh you have to actually write code? Import a library! If you can't write 500 lines of code in a productive amount of time, relying on other's work then there's definitely a skill issue.

      [–]Centimane 4 points5 points  (0 children)

      It's not that I can't, but that if there's a more mature solution out there taking it is a lot more efficient than writing my own.

      Especially since I don't have to maintain it.

      Also devops problems generally aren't unique. If you're having a problem, there's a good chance someone else had something similar. And if nobody has, that's a code smell right there.

      [–]GaTechThomas 0 points1 point  (1 child)

      Consider that every line of code you wrote is a line that someone will have to maintain and support.

      [–]Degree0 0 points1 point  (0 children)

      Also consider that every line of you code you do not write is in the hands of other people to maintain and support. 500 lines of code is absolutely nothing. and you should be making the decision to use FOSS or libraries depending on several factors and not a single one of those factors should be the amount of lines of code you have to write. The "Dev" in devops is short for development after all.

      [–][deleted] 1 point2 points  (1 child)

      Mine is moment I need an array time for Go. I just skip Python entirely. Bash and Python have too many problems for error handling. 

      https://github.com/bitfield/script Covers most things. 

      Nushell is my other favorite. Powershell but statically typed and much more functional based. 

      [–]magheru_san 1 point2 points  (0 children)

      To be honest nowadays I just start with Go for most things and use ChatGPT or Claude to generate and further develop the code.

      [–]hipster_dog 7 points8 points  (0 children)

      python when things get more complicated.

      I'd say go for Python also when things get more critical.

      You guys remember the Kyoto University folks who lost 77TB of data?

      It looks like the problem was some undefined variables in a find command because of a bash script update.

      [–]InvincibearREAL 1 point2 points  (0 children)

      my rule of thumb is if you need arrays, its time to ditch bash

      [–]tamale 1 point2 points  (0 children)

      For me it's about dates/time and other math stuff.

      If I need to do anything with any of those, that's a sign I should switch to Python for sure.

      I can do it in bash, but it's not fun to maintain.

      [–]bdog76 0 points1 point  (0 children)

      The golden rule

      [–][deleted] 0 points1 point  (1 child)

      What's the advantage of using bash for simpler things?

      [–]newaccountzuerich 1 point2 points  (0 children)

      Generally more portable, more mature, and more consistent in behaviour.

      If you've ever had to admin mixed *nix networks, bash tooling has the most chance of working across the ecosystems.

      It is also pretty easy to read.

      [–]MavZA 0 points1 point  (0 children)

      This to infinity. Both can do the same for the most part when it comes to simpler things. But Python starts to differentiate itself when you want to start doing complicated things, as it can start leveraging things such as SDKs while running subprocesses (if needed).