6 ms·
I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If o
by yjftsjthsd-h 5y ago
I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If only.
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.
- BeetleB 5y ago> I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If only. xonsh (https://xon.sh/ https://xon.sh/) Been using it for a few years now. It's worth it.
- andrewshadura 5y agohttps://amoffat.github.io/sh/ https://amoffat.github.io/sh/
- blueblob 5y agoI agree with your sentiment. People like to complain about tools. The nice things about bash (and even sh) are that you can combine a bunch of simple tools together quickly because the syntax doesn't get in the way of it. The language designers didn't intend for people to be writing thousands of lines of scripts; they intended them to be using them locally and focused on interactivity. So naturally the syntax is nice for interactive work. It's a stretch to pretend that working with python paths is easier than working with shell paths, just like it's a stretch to say your bash machine learning library is better than a python one. The author should let things excel at what they're good at and stop pretending everything needs to be unified.
- hoistbypetard 5y ago> I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If only. I've only kicked the tires on both, but plumbum: https://plumbum.readthedocs.io/en/latest/ https://plumbum.readthedocs.io/en/latest/ and sh: https://amoffat.github.io/sh/ https://amoffat.github.io/sh/ both feel pretty good in that regard.
- mbreese 5y agoEven if you have a great library that abstracts away shell like things, you’d still probably be better served with a shell script. Shell scripts aren’t supposed to be big. They are quick one offs that solve immediate problems. This means that they are also spread around the filesystem. I’m of the opinion that one shouldn’t install libraries into the system Python path. I also tend to make heavy use of virtual envs. Setting up a venv for each off scripts doesn’t seem like a great plan. 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.
- yjftsjthsd-h 5y agoAfter going over it again, I think I can be more precise: What annoys me is that it's inconsistent to the point of being disingenuous. Like... You want to say error handling is hard to do right? That's fine, it is. You wanna write "Checking the status of programs using $? can be accidentally left off, leading to inconsistent behavior" and then in the same article write in explicit exception handlers, and act like somehow those can't be forgotten!? Get lost.
- psd1 5y agoIf you forget an error handler in python, execution terminates. So I don't see your point.
- kimmeld 5y agoWe have a tool that’s better at dealing with shell stuff. It’s called Perl.
- drewcoo 5y agoIs it Christmas already? If not, there are copycats like Ruby and Python . . .
- GSGBen 5y ago> I was hoping someone finally had a library or something to make python good at the things shell does well (mostly, running stuff and piping output around). If only. 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.
- derekbaker783 5y agoWhat is Powershell
- DyslexicAtheist 5y agoI just added powershell support to my Debian. $> 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!
- aloisdg 5y agoA dotnet based shell where you can share around dotnet object instead of plain text.
- MichaelMoser123 5y agohttps://github.com/MoserMichael/subb https://github.com/MoserMichael/subb https://pypi.org/project/subb/ 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 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.
- deathanatos 5y agoWe had some problem, roughly, "run a tool on all YAML files in the directory" or something. The first attempt that led to a production outage: (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.
- Izkata 5y agoMy preferred way under those conditions would be more along the lines of "see if a safe command fails, do the desired command if not", like: if ls *.yaml > /dev/null; then the_tool *.yaml fi
- xorcist 5y agoThat only looks simple because we don't know what problem was being solved. Is 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.
- Izkata 5y ago> Is it really right to not process any files just because one of them just disappeared? 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.
- Xavdidtheshadow 5y ago> obscure and difficult-to-predict 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).