Bashing the Bash – Replacing Shell Scripts with Python (2017)
medium.com
medium.com
This really just reads like someone who doesn't know shell and does know python, but thinks the problem is the tool. They continually describe shell with phrases like "obscure and difficult-to-predict" while talking through things that are either obvious with experience or from context, and then write something different in python (i.e. something you could write in BASH that does something different) that's not less obscure but is only using python obscurity.
> An example of a shell obscurity is the way the current working directory is set. The cd command is clear enough, but in the presence of sub-shells using (), can make it difficult to discern a stack of nested shell invocations and how the working directory changes when the sub-shells exit.
Er... yeah? So you expect PWD to be a global and it was actually a local?
Or more explicitly:
> One of the shell’s ickier features is that variables tend to be global. There are some exceptions and caveats, however, that lead to shell scripts that are broken or behave inconsistently.
That's not a shell feature, that's just how people frequently write it - you can make functions and declare your variables local if you want. I can easily write a python script with all my variables at the top, too.
xonsh (https://xon.sh/)
Been using it for a few years now. It's worth it.
I've only kicked the tires on both, but plumbum:
https://plumbum.readthedocs.io/en/latest/
and sh: https://amoffat.github.io/sh/
both feel pretty good in that regard.
For more centralized workflows, I could see Python shell replacements being helpful. Especially project specific scripts where the overhead of managing a venv is negligible.
But realistically replacing the majority of bash scripts? I just don’t see it.
If not, there are copycats like Ruby and Python . . .
Good news, they did! Even better news: it doesn't require python, it's cross platform, and supports an easy-to-read verbose style for scripts but a quick-to-write terse style for interactive usage. It's an absolute joy to use for nested data structures, everything is an object and it has full native tab completion.
Unfortunately it came out of Microsoft so a lot of people haven't given it the time of day yet. It's honestly one of the joys of modern CLI usage though.
$> ll /bin/powershell
lrwxrwxrwx 1 root root 9 Mar 26 14:40 /bin/powershell -> /bin/dash
$> cat ~/pwr_to_the_people.sh
#!/bin/powershell
echo "Hello Powershell"
$> powershell ~/pwr_to_the_people.sh
Hello Powershell
Works like a charm!https://pypi.org/project/subb/
I wrote this to simplify just that for my own tools; the subprocess module that comes with the batteries has a very general interface, i think that it is a bit complex for a quick script.
My objective was to get an abstraction, for a one line process run and extraction of the result, similar to what we had in Perl5 with the system library function. https://perldoc.perl.org/functions/system
Also the shell is impractical, when it comes to slightly more complex programs. There is a limit on what you can do with pipes. Maybe that's the reason why perl is that flexible, as they tried to bridge both realms: Perl had to be useful as a replacement for the quick shell like script, and to be useful as a general purpose programming language.
(It is late, bash examples are approximate. Don't sweat the syntax.)
the_tool *.yaml
There weren't any YAML files, so argv[1] was literally "*.yaml", which then wasn't found, leading to errors, etc. shopt -s nullglob
the_tool *.yaml
(Don't even get me started on how there's shopt -s and set -o and WTF.)There weren't any YAML files, so argc was == 0, which the program considered an error. Mea culpea, perhaps.
AN_ARRAY=(*.yaml)
if [[ < length of the array is not 0 > ]]; then
the_tool "${AN_ARRAY[@]}"
fi
This failed, b/c bash can't represent an empty array! (And we run -u, as one must, if one wants reasonable behavior.)So finally the contorted,
AN_ARRAY=(*.yaml)
if [[ "${AN_ARRAY+x}" = x && < length is not 0 > ]]; then
the_tool "${AN_ARRAY[@]}"
fi
To say "you're holding it wrong" is just folly. Bash is worthy of the same treatment that "A Fractal of Bad Design" gave PHP, and it has no place in anything that wants to call itself engineering.Use it on the command line in your day to day, fine; but if it's getting automated, jump to at least Python.
find . -maxdepth 1 -name '*.yaml' -print0 | xargs -r0 the_tool
>To say "you're holding it wrong" is just folly. Bash is worthy of the same treatment that "A Fractal of Bad Design" gave PHP, and it has no place in anything that wants to call itself engineering.If you have a problem that involves "run a command with a list of parameters but not if the list is empty" and you don't immediately think of `xargs -r`, then consider that it's because you don't understand the tools, not because the tools are bad.
Popen(["the_tool"] + list(Path().glob("*.yml")))While dethanatos's point about Bash's default behavior with a non-matching glob is on target, the remainder of the example demonstrates a problem in the_tool, which will need to be worked around regardless of how you invoke it.
find has -exec, xargs isn't necessary.
No?
But it's like, I have to turn my head around and stop doing one thing (globs), and switch over to another thing (find, xargs). The language has baited me down one way of doing it, when I shouldn't be doing it that way. I'm well aware of xargs (but not the -r flag). I typically avoid xargs, as our code has to¹ work on macOS & Linux, and xargs is one of those utilities whose available options differ between the two for even simple use cases. (Such as here, for the "does it exec on no args?".)
But it still proves the point: the equivalent Python is just so much more straight-forward, easier to read, and not apt to shoot me at a moments notice, and doesn't have bugs. A reader only familiar with Python's syntax can read and understand Python, and can write Python that is correct. Not so with bash, you have to understand, "oh, no, globs will screw you here, you need this flag from this utility" — no, it's lunacy.
Like, I can go down the road you're suggesting (and in the past, I did) and keep learning the nooks and crannies and intricacies of bash, each one adding to the nightmare fuel that is my understanding of shell. I can keep tweaking the script around various failure modes, letting each line fail in prod 3 different ways. Or, I can just re-write it in Python, or Rust, or any other sane language and have it never fail again, for reasons that would also either apply to bash, or apply and have worse consequences than bash. (E.g., the Python would raise, bash would just plow on with it.)
Your response is exactly the same as my response before I saw the light on PHP, C, etc.: "you're holding it wrong". No, it's a bad tool that guiles the user into holding it wrong. There are better tools that are easier to hold.
¹b/c despite all work being done for Linux, devs get MBPs, b/c the company can't manage more than exactly 1 type of machine, for everyone.
if ls *.yaml > /dev/null; then
the_tool *.yaml
fiIs it really right to not process any files just because one of them just disappeared?
In general, situations like this suffer from race conditions and time-of-check/time-of-use problems.
No idea what you mean here, the glob can expand to one or many.
> In general, situations like this suffer from race conditions and time-of-check/time-of-use problems.
I mean, that's a problem in every version presented so far, one that I don't think can be fixed externally without something extra like hardlinking to a new directory, because "the_tool" apparently can't handle missing files and they can go missing when "the_tool" starts up. That's why I didn't even bother.
There are many ways to run a command on every .yaml file in a directory. Personally I like using xtrace. Something like:
echo cd the_directory > 1.sh
echo "test -f *.yaml" >> 1.sh
ls -1 *.yaml|sed 's/^/the_tool /' >> 1.sh
chmod +x 1.sh
less 1.sh
sh -ex 1.sh
As mostly a day-to-day shell user who automates some things, I do not use Python. Never had an outage.is this a cloud AMD64 opinion that ignores the 2 trillion devices which Gartner predicts will never run python?
I recently finished a ~600 lines ash script and the kids at work which just graduated complained that *"Y u no python?" For them it was difficult to understand because apparently I avoided cat/grep/dirname/basename/etc and used shell syntax wherever possible to avoid shelling out. (the thing I did would only run when system reaches a certain load)
Everyone forgot that the target hardware has only busybox (ash) and was massively resource constrained. My only options were to implement what I was doing as an eBPF, in C, or shell (ash). The shell script took 4 days (and was refactored/optimized several times within that period), the C (or eBPF) program would have taken ~10 days and the eBPF which is the best solution is unmaintainable once I leave the company.
from a performance perspective I take a bash script any time. Also python programs over time have a risk of eventually pulling in a dozen dependencies.
Plus, if you are running shell out of necessity on a constrained system, that doesn't mean shell deserves to be a programming language where there's actual choice.
how so? there is no python or perl or ruby etc on the target. what would have been a maintainable option other than /bin/sh ? C/C++ ?
> where there's actual choice.
again what choice? :) as I said there is no other solution than what I've given. the system literally doesn't have anything else regardless what "I think" it should support.
Typically, time spent interpreting control flow in a bash script is dwarfed by time spent in the programs called by the script.
> It is literally interpret by lines
It's just splitting lines into words to build up commands. It's not compiling C++.
> starting a whole process per if statements..
In the common pattern `if command1; then command2; fi`, if command1 is a builtin (e.g., `test` or `[`), then bash does not create a subprocess.
In fact, handling that types of errors is what bash is suitable for. "If file is readable, process it" is not an unusual construct. Please do take care however to avoid races if this directory is a spool directory that can be populated at any time, in which case it is useful that rename() is atomic, sometimes an additional working directory is required. Again, this is how the operating system is designed and nothing the shell can gloss over.
I have seen many problems from previously unknown race conditions when concurrency is increased. Scripts have to be kept composable and trivial to understand in order to guard against that.
To be honest, I would argue to remove any script that looks like the last example above. It is not clear what side effects of that bizarre array construct are useful to the system. Keep scripts as simple and readable as possible, a trivial truth perhaps, applicable to every language, but one that needs to be reiterated until the end of our profession.
It absolutely can, I have no idea where you’re getting this idea.
I mean, I think there's something to that. Bash has a lot of arcane rules; I personally never write an `if` statement right, what with the `[[` vs `[` and having to surround the condition with whitespace. I find that bash scripts carry a million little gotchas that will cause variables to not evaluate/expand the way you'd expect.
Say what you will about Python, but it's much easier to reason about what a script will do as compared to bash (unless the bash is _extremely_ simplistic).
> The point of bash-bashing is to reduce use of the shell.
That's a tautology. I get it, we're supposed to reduce the use of bash ... but why?
> Without much real work, it’s easy to replace shell scripts with Python code.
But you're writing the same thing again? There needs to be more reason than this.
> The revised code is easier to read and maintain, runs a little faster, and can have a proper unit test suite.
Three claims. Any data to support it?
Some of the examples of "bash is bad" are also not convincing to me. Here's an example:
> An example of a shell obscurity is the way the current working directory is set. The cd command is clear enough, but in the presence of sub-shells using (), can make it difficult to discern a stack of nested shell invocations and how the working directory changes when the sub-shells exit.
So...why do you have "a stack of nested shell invocations" that all need to be working directory aware? And how would this be any better in Python? (It could be better in either Python or Bash but either one still requires the developer to avoid simple mistakes.)
I don't know. I don't get it. I feel like the author had the unfortunate experience of knowing more Python than Bash and inheriting someone else's (who also didn't have much Bash experience) crappy Bash code.
Shell is great for one liners to run commands, cancerous for anything more complex. Fabric combines the best of both worlds: shell for one liners, Python for more complex logic https://docs.fabfile.org/en/2.6/getting-started.html#addendu...
I somehow keep returning to Bash, though. I enjoy it.
I go back and forth. For the longest time I ignored Perl and just wrote scripts in C. Then I learned Perl, then Python. I prefer Ruby for no better reason than it's less boring. These scripting languages share a common strength: nested hash tables make everything easier. Still, I'd revert to Bash, explaining that if you can't accomplish the same thing with virtual text files in Bash you don't really understand Bash.
Nevertheless, switching from Bash to Ruby is like taking off a heavy backpack after a long day hiking. Who needs a joint when the weight is off your shoulders?
Otherwise they'd just be plain old compiled "languages."
There isn't any "data" on these things. It's a qualitative, holistic assessment of the risk-benefit tradeoff between two choices. Like much of life in general, in fact.
I can probably run my bash script on every linux I ever touched.
I can probably have that python code break on half of the linux boxen I use. This one does not have module X installed. This one is too old, this one is too new, this python is not holding its mouth just at the right angle to work.
Yeah, bash bash all you want. I have pulled out scripts decades old and run them. I can not say the same for other things I have written.
Exactly THIS !
Perhaps it's unfair to pick at python for this when many of these issues are not the language but because people picking python when they should have just used shell script.
If you switch from Linux to *nix where you introduce BSD variants (like macOS) it gets even worse. Python dependency management leaves much to be desired but bash has none.
Any particular version of Python is a saner choice, if you could pick one and freeze it retroactively for the last 20 years and for at least the next 20. But that is not how Python works. Python breaks every 11 minutes.
I would not write anything in Python that I cared the slightest bit about longevity or portability.
Maybe if we made a subset of python, locked down the syntax and feature set, discouraged any use of plugins or libraries in any fancy way that relies on any kind of repository or package manager system, and gave it a new name to distinguish it from normal python, maybe that would be an ok replacement for any of the shells.
But that is not happeing and don't even try to pretend like it could.
Are there any other languages with the same stability that would also fill this niche?
Also, I’ve been using various lisps (elisp in emacs org-mode and Common Lisp) and they both can be written in ways that will probably last a long time.
Using awk as a general purpose language means slightly bending it out of shape, by mostly ignoring the main section (which runs once per selected record of input) and writing your entire program in a big END{} section.
BUT
* It's installed everywhere and has been forever just like sh
* A 20 year old awk script from hpux or something, works in todays gawk, just like sh.
* Unlike sh, the language is more like a normal generic language than, making it more readable and less arcane for non-trivial jobs.
For example, bash has a handy substitute feature, in the form of a brace expansion. ${FOO//a/b}
Awk has an actual sub() or gsub() function.
A lot of things in bash require tricks essentially abusing various fancy brace expansions and messing with IFS to hijack the line parser into doing things there is no explicit command for, especially if you're avoiding external commands. IE you're not simply writing code that does what it says, you're running a bash interpreter in your head and manipulating strings so that when they are expanded and resolved, they mean something to the the parser and results in the parser doing something more useful. It's like your always writing code that writes code, instead of just writing code.
External dependencies is another whole point too. Shells main explicit job is to run other programs. You actually have to work pretty hard not to run an external binary by accident, ie you have know which commands are built in and which are executables. In awk or anything else, you have to go out of your way to exec() or system().
A bit of a shame that it's seen as an anachronism to many. Perl soon rubs off on you and becomes a delight.
All of my Python 2.6-2.7 scripts haven't been touched in 10 years in production and they aren't likely to ever be updated. I actually now refuse to write new Python that isn't compatible with both 2&3 for this reason. Python3 refuses to stabilize.
Python 2 went away in macOS. Bash still exists.
`/usr/bin/python` invokes Python 2.7.18 on my just-bought Macbook Pro 14 running MacOS 12.2. Maybe that's because I installed the Xcode command line tools?
[0] https://www.macrumors.com/2022/01/28/apple-removing-python-2...
There's still a macOS installer for the latest release of Python2.7 (4/20/2020).
[0] Sunsetting Python 2. https://www.python.org/doc/sunset-python-2/
If you mean my software I mentioned if something breaks I'm still required to fix it. I just doubt that will happen at this point and have other tasks to do.
https://www.bleepingcomputer.com/news/software/python-27-rea...
Apparently RHEL will maintain any security problems until 2024, but yeah it's pretty much dead. Huh. Not sure how to react to that.
And frankly...bash kinda sucks as a programming language and environment. It's a footgun factory. Even when I was most fluent, I constantly had to deal with nested quotes, escaping, stringly types, empty variables, implicit behavior.
Python's biggest weak point is definitely its packaging and dependency ecosystem... but it at least has one. Bash doesn't even have modules. And it's gotten MUCH better over the years, with pyproject.toml, poetry, actual version resolvers, pipx makes managing venvs for cli tools way easier.
"but bash is everywhere" says many folks. yeah, and? There was a point in time where it wasn't. Python is seeing way more market penetration.
Xonsh (python based shell) and plumbum (python library which makes pipe-operating and subprocessing easier) are both great.
This article says that, "The [bash] shell isn't a complete programming language". While I agree that bash lacks any real data types besides strings, bash is a Turing-complete programming language[1], so it's theoretically possible to write _any_ program in bash that can be written in Python. It might be a hell of a lot uglier and lack things like imports, modules, etc., but it can be done.
For example, there is an implementation of an HTTP daemon written purely in bash[2]. Once again-- terrible idea, but great execution.
[1]: https://en.wikibooks.org/wiki/Bash_Shell_Scripting#A_few_not...
[2]: https://github.com/avleen/bashttpd
edit: added newline to separate references
It's not pure bash, as it relies on netcat or socat plus some other external programs (ls, tree, cat, date).
There does exist a pure bash httpd though. It relies on a loadable builtin (from the bash tree though not built by default).
Python is good for complex, structured applications, but shell scripts are not that. Shell scripts are glue, and there's arguably no better glue than Perl.
I first learned the shell, and pretty much stuck to it conservatively avoiding bash extensions.
Later, I learned perl (around version 4)
my first impression of perl was annoyance.
$foo = 123;
seemed silly because there was a dollar sign on the left side of the assignment. and... $foo, @foo, $', $_, s/abc/def/;
all the syntax seemed needlessly cryptic and reinforced the "perl is a write-only language" idea in my mind.
But then something happened.
At some point all of these idioms went away as I became fluent and could think in perl.
Because there were many ways to do something in perl, I found that it become VERY easy to get an idea in my head out into working code.
So perl was to me my most expressive language.
But what I've noticed is that many people haven't overcome the syntax barrier and perl looks like line noise to them. More worrying is the fact that everyone can get their ideas into perl, but because people solve problems so differently their perl can be vastly different from what I am accustomed to.
So a few years later I learned python. The indentation requirement was a minor annoyance, but that was quickly overcome by the consistency and visual structure it added.
A more major annoyance was that many things were harder to implement in python. Regular expressions is a huge one - they are a major feature of perl and I had learned to use them liberally.
Yet I persisted and with help from the many "batteries included" with python, I was easily writing portable maintainable scripts in python.
And they were still readable after 6 months.
Now I write only small shell scripts, and past a certain size all other scripting is in python. It's quite easy to write meaningful scripts in python. (I've pretty much lost my fluency in perl via python -- I fumble around now looking at or modifying perl scripts)
by the way argparse is quite easily my favorite import in python.
parser = argparse.ArgumentParser(argument_default=None)
parser.add_argument('-d', '--debug', action='store_true', help='debug flag')
parser.add_argument('-c', '--config', default="~/.config", help='config file name')
arg = parser.parse_args()
if arg.debug:
print('config file: %s'%arg.config)
...The TWO things about Python that make it more compelling are functions and exceptions. You stand a chance of handling errors in Python. In bash there’s very little else you can do, when encountering an exceptional situation that is an error, except stop. The nice thing about all these functions is you can put them in modules as well. Modules! Of course! A namespaced way of laying out non trivial amounts of code!
Ok so real functions, exceptions, modules. Three things that makes Python my default tool of choice. And libraries. Bash doesn’t have pip. Sure, bash’s “pip” is the commands it can run aka /usr/bin, but if that’s your interface then it’s hardly as flexible as say gitlab.py or requests.py.
So: apart from the functions, exceptions, modules, libraries. And speed. SPEED! And code formatting. And a debugger. And stack traces. …what has Python ever given us that makes it a no brainer replacement for bash scripts?
“Brought Unicode”
”Unicode! Oh shut up!”
— After MP
Maybe I'm just a terrible python programmer and also all those around me suck at it but in 20+ years I have yet to see a well optimized python script faster than a well optimized bash/ash/csh/ksh/zsh script. Maybe your point is true if a script constantly does foo=`some thing` and constantly shells out other commands.
The bash-centric solution was to write a little eui64.c and call that from the script. Bash isn’t for doing things, it’s for making other programs do things. That worked fine for a bit, but in the end it just became easier to factor and reason about the project when it wasn’t a weird mixture of clever things, and one Pythonic lump of boring and predictable things.
Python makes everything medium difficulty which can be a big win depending on the size of automation required.
Typer is one of the better packages for running Python scripts from CLI. Fabric & Invoke are pretty good too.
To run any bash commands in Python I have a Python script that creates a temporary bash file with the code to execute, makes it executable, runs it with the commands in the built-in subprocess package, then deletes the file. This makes any complex bash commands with piping and whatnot runnable straight from Python.
This way I don't have to learn all the different bash-based Python commands from pathlib and whatnot that throw unexpected errors, and I can run them using pure bash syntax from Python. But then you also get all the benefits of Python's looping syntax, classes etc.
"Moving furniture by throwing it in the back of a pickup truck is obviously bad. Here's how to do it with a forklift and 18-wheeler instead, because this is definitely better."
Bash, surprisingly, gets backwards incompatible language features too. But the vast majority of bash developers are writing for all computers, not just computers with software from the last 3 years.
Python language could work if you stuck to an old target. But python culture is a problem and makes this very unlikely. What python is, and what python devs deal with, changes constantly. That doesn't work for shell scripts where stability is king.
Python3 is just under 14 years old.
https://github.com/MoserMichael/subb
https://pypi.org/project/subb/
Python doesn't have the problem of shell scripting language, it doesn't get impractical, as the program is getting more complex. In bash you have arrays, and even maps, but these aren't pretty. Also the shell scripting language is being evaluated by an parse tree/AST interpreter, that's significantly slower than even python, in it's byte code interpreted form.
My objective was to get an abstraction, for a one line process run and extraction of the result, similar to what we had in Perl5 with the system library function. https://perldoc.perl.org/functions/system
Also the shell is impractical, when it comes to slightly more complex programs. There is a limit on what you can do with pipes. Maybe that's the reason why perl is that flexible, as they tried to bridge both realms: Perl had to be useful as a replacement for the quick shell like script, and to be useful as a general purpose programming language.
Yes, there are moments when you try to solve something in bash that you think is trivial in other languages but with bash is painful. Few days ago tried to test if a value is present in a bash array using a function. Couldn't get it working after trying few things. Then I realized I could perform a very similar (and good enough) check just seeing if a directory and a file do exist in my relative path to the script I was running: `if [-d DIR ] && [ -f FILE ]; then ...`.
So I needed to change my mindset a bit. Another example of where bash is really good is for tiny small tasks: I wrote a small script to increase/decrease volume that is coupled in i3 and calls the `mixer` command in FreeBSD. Works perfectly and never touched once it was working. Another one: I get an acoustic alert when my battery drops down below certain percentage. And a couple more: set a random background every time I log in to i3, download a page for offline reading.
Again, doing any of these things (or similar ones) with something different than bash (or maybe your favorite shell lang) seems like picking up the wrong tool for the job. There was a time when I cursed shell scripting a lot and that was because I couldn't see when to use shell scripting.
EDIT: Paragraph spacing.
Without questioning the "why" of the script, sans my customary morning coffee, and without having tested the code below.
Maybe I'd do something like this.
For the small price of function invocation overhead, the nice benefit of doing it this way is that one can `source` the functions into one's Bash shell and use each one as a standalone Unix tool, complete with tabtab completion, pipelines etc.
#!/usr/bin/env bash
stop_app() {
pkill ${1:?Fail. App name required.}
}
today() {
date +%Y%m%d
}
analyse() {
local analyser=${1:?Fail. Analyser script name required.}
local out_dir=$(printf "results_%s" $(today))
while yaml_file
do local out_file="${out_dir}/summary_$(basename yaml_file).txt"
python3 ${analyser} ${yaml_file} > ${out_file}
printf "%s\n" ${out_file}
done
}
exec_analytics() {
local analyser=${1:?Fail. Analyser script name required.}
local source_dir=${2:-"~/Documents/ExtFin-EFS/smoke/"}
find ${source_dir} -type f -name *.yaml |
sort -r |
analyse ${analyser} |
tail -1
}
update_current() {
local latest_outfile=${1:?Fail. Provide latest output file.}
ln -sf ${latest_outfile} "current.txt"
}
And maybe one can invoke it like... stop_app "whatever_app" && (
trap "rm -f /tmp/module_design_analytics_outfile" 0 HUP TERM PIPE INT
if exec_analytics module_design_analytics.py |
tail -1 > /tmp/module_design_analytics_outfile
then update_current $(printf /tmp/module_design_analytics_outfile)
echo "Done"
else echo "Oops. Something went wrong."
fi
trap - 0 HUP TERM PIPE INT
)Previous discussion: https://news.ycombinator.com/item?id=14998213
That's why I've done it before, to write tools like custom log rotating / clean-up stuff; another project that comes to mind was a data pipeline that used some Python regex stuff to clean up non-printable characters and ugly stuff from text files before importing data. Both of these projects had lots of tests to verify that the code did what we wanted it to do.
Primarily, because of a library I compiled and installed, I'm getting other nasty side effects form it. Primarily that protonvpn-cli isnt now running becasue of some broken library.
I've never had bash break like that. Quirks, sure. But never this sort of brokenness.
For ProtonVPN you can also use the OpenVPN config files they provide to avoid any Python client issues. Ironically I have a Bash script written around that to generate individual ovpn files as needed from the zip for myself.
No environment issues or missing and clashing libraries.
PS. I'd still go for bash 99/100.
Bash is usually everywhere even on "new installations". I'd hate to fight package compatibility, virtual environments etc..
YMMV
I wouldn’t want to worry about python environment when dealing with OS post-install scripting for instance.
Like testing (Bats sucks).
Like string manipulation (use other utils or even languages callable from Bash).
Like complicated logic. Just don't do that in Bash.
Bash is for (glorified) one-liners. And for I-can't-believe-you-did-that-in-Bash-ers.
And give a wonderful syntax error.
The team was supposed to maintain this mess. We decided to create a compiler that translated .BAT scripts to C#. The idea was that the C# code would be easier to understand and modify. I left before it was complete, so I'm not sure how that worked out. But last I heard, they were pleased that some parts of it now ran thousands of times faster.
mkdir -p would create the directory without error. checking if a directory exists is trivial before creating it. Python throws an error if the path given to os.mkdir exists too.
This article is way too long to digest, so i should stop now.
Perhaps the argument should be, rewrite hacky things in bash as programs and build them into your apps?
I'm a professional programmer by trade. Admitting to starting a program I know would be over 100 lines in shell script would be close to an admission of incompetence, because technically shell script is one of the worst computer languages out there. So I'd never admit to it. But it's so damned portable, and ubiquitous and the batteries it comes with (the 'nix cli tools) are to powerful and complete, it makes irresistible at times.
Where this article falls down is they are recommending Python3. Python2 would be fine. But in 'nix environments, where file names and configuration files can't be treated as text (Unicode) because there is no well defined system encoding, Python3 manages to introduce more rare bugs than shell script. It encourages you to treat everything a text, the dies ignominiously when decoding the 'nix byte stream (file name or whatever) fails. (On Windows where everything is UTF16, this isn't a problem - for local files. It remains a problem for data from external systems, like the internet.)
Pull off making a programming language less reliable than shell was a mean achievement - but the Python3 devs did it. Hats off to 'em.
Lol one of the top problems with having "scripts" in python, their reliability-lifetime" (a.k.a as execute-and-forget) is very limited. Packages are outdated, api changes etc etc
https://github.com/Mylab6/PiBluetoothMidSetup
Of course I could of done this in bash, but Python is just so much cleaner.
(Sorry, Oingo Boingo)
I will never use a programming language which has significant white space, specially if I may have to view and edit the scripts in a remote terminal with vi.
I am also not too interested in using a programming language which looks the same before and after encryption: https://www.goodreads.com/quotes/tag/437174-perl-the-only-la...