[HN Gopher] How to write idempotent Bash scripts
___________________________________________________________________
How to write idempotent Bash scripts
Author : gautamsomani
Score : 47 points
Date : 2021-12-08 10:22 UTC (12 hours ago)
(HTM) web link (arslan.io)
(TXT) w3m dump (arslan.io)
| xonix wrote:
| Idempotence is achievable elegantly with makesure tool [1] using
| @reached_if directive [2]. Full disclosure - I'm the author of
| the tool.
|
| [1] https://github.com/xonixx/makesure
|
| [2] https://github.com/xonixx/makesure#reached_if
| itamarst wrote:
| A better approach is to avoid writing bash scripts. They're just
| too dangerous, even with "strict mode"
| (http://redsymbol.net/articles/unofficial-bash-strict-mode/).
|
| Better to use Python, or Ruby, or a Rust or Go executable.
| giobox wrote:
| Bash has the distinction of effectively being one of the only
| "zero-dependency" cross-platform scripting languages for most
| software engineers: "just works" on Linux/Macs. On Windows, Git
| for Windows includes gitbash.exe (for cross platform hook-
| scripts etc), allowing bash scripts to work on Windows boxes as
| well.
|
| Given the prevalence of git in the software development world,
| this has meant in my experience bash has very often been the
| most effective "zero dependency" cross-platform scripting
| language around. Python/JS/<other runtimes> will rarely work
| out of the box 100% of the time especially for Windows
| developers. This makes bash pretty valuable for things like
| build scripts if you have developers using different OSes.
|
| If your source lives in git, you know there is a good chance
| user must have gitbash.exe too if they managed to clone a git
| repo on Windows, thus granting as near as one gets to "zero-
| dependency" cross-platform scripting in my experience.
| EuAndreh wrote:
| The sh shell, or POSIX sh, is closer to a zero dependency
| language. There are many systems that have shells which are
| not bash.
| giobox wrote:
| Excellent point, and will still work on recent MacOS
| versions with zsh/gitbash.exe too.
| fpoling wrote:
| Bash on Mac is deprecated and zsh is sufficiently different.
| Plus on minimal installs of containers or in VM one may not
| even have it with /bin/sh provided by dash or similar minimal
| shell.
|
| So my rule of thumb if one cannot use plain /bin/sh and must
| depend on bash-specific things, one better use a proper
| scripting language.
| seiferteric wrote:
| How does that help with idempotency? Your better off with
| Ansible if idempotency is the goal.
| znpy wrote:
| > How does that help with idempotency?
|
| It just doesn't.
|
| Edit: oh and you can write non-idempotent ansible playbooks
| too... it's not about the tool, it's about the craft, and
| experience.
| qbasic_forever wrote:
| That still doesn't solve the issue that idempotentcy is getting
| at though. You can very easily and happily write bad
| python/go/ruby/etc. code that does things like fail to handle a
| directory that already exists, a file that was previously
| created, etc. I'd even argue it's more difficult in those
| language since it's likely more error checking code to write
| vs. passing a flag to a command in a shell script.
| fpoling wrote:
| I have a PHP script that does idempotentcy. It was strictly
| easier to write than a similar code in bash the moment one
| needs non-trivial and robust patching of config files.
|
| And passing force flags can lead to subtle issues as -f and
| friends may have wrong behavior in corner cases. So for this
| reason I do not use it in shell scripts and rather do
| explicit tests before the command, like test -f file && rm
| file
| Spivak wrote:
| Doing so will solve none of the problems outlined in the
| article. You will still have to write all these patterns in
| Python to make them idempotent.
|
| Also for systems stuff you're not gaining much lot when your
| interface is calling binaries. A script of subprocess.run calls
| is way more cumbersome and the moment you want to pip install
| something it's no longer portable and a huge PITA to
| distribute.
|
| Bash has lots of footguns because it's been around a while; run
| your code through shellcheck if you're not confident. It's the
| lingua franca of Linux userspace.
___________________________________________________________________
(page generated 2021-12-08 23:00 UTC)