Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I write a LOT of bash/shell scripts. And I don't like it, it's just part of what I have to do.

Learning a handful of bash idioms and best-practices has made a massive impact for me, and life much easier. The shell is something you cannot avoid if you're a programmer or other sort of code-wrangler.

You can interact with it + be (mostly) clueless and still get things done, but it's a huge return-on-investment to set up "shellcheck" and lookup "bash'isms", etc.

----

(Off-topic: I am convinced Ruby cannot be beaten for shell-scripting purposes. If I had a wish, it would be that every machine had a tiny Ruby interpreter on it so I could just use Ruby. I'm not even "a Ruby guy", it's just unreasonably good/easy for this sort of thing. And I keep my mind open for better alternatives constantly.)

Example of near-identical script in bash vs Ruby:

https://github.com/GavinRay97/hasura-ci-cd-action/blob/maste...

https://github.com/GavinRay97/hasura-ci-cd-action/blob/maste...

I'm not sure how much closer to describing your exact intent in English a language can get than:

  successfully_made_executable = system 'chmod +x /usr/local/bin/hasura'
  abort 'Failed making CLI executable' unless successfully_made_executable
Though I have NOT written Perl (either old Perl, or Raku/Perl 6) but I do believe it may be roughly this semantic too.

EDIT: Looks like Perl/Raku is essentially the same as Ruby in this regard. So besides it being a whacky language, take that for what you will:

  $successfully_made_executable = shell "chmod +x /usr/local/bin/hasura"
  die 'Failed making CLI executable' unless $successfully_made_executable.exitcode is 1


> I'm not sure how much closer to describing your exact intent in English a language can get than:

> successfully_made_executable = system 'chmod +x /usr/local/bin/hasura'

> abort 'Failed making CLI executable' unless successfully_made_executable

I guess, but if I was writing scripts like this, I'd really want to write a helper method something like:

    def system_or(cmd, fail_msg)
      system cmd || abort fail_msg
    end

    # and then

    system_or 'chmod +x /usr/local/bin/hasura', 'Failed making CLI executable'
Instead of having to write every single `system` call as boilerplate two liners with an immediate-throwaway local var.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: