7 ms·
I've come to use more and more Python for tasks I would have used Shell in the past. Because of weird syntax with anything but simple variables or endless fiddl
by ofrzeta 7y ago
I've come to use more and more Python for tasks I would have used Shell in the past. Because of weird syntax with anything but simple variables or endless fiddling with quoting.
You can get a lot done quite easily and concise in Python 3, for instance with the pathlib module that has methods for recursive globbing and stuff like that.
https://docs.python.org/3.7/library/pathlib.html https://docs.python.org/3.7/library/pathlib.html
- badsectoracula 7y agoThe nice thing about Bash which can obviously not be said about Python is that Bash scripts will keep working (assuming whatever they call is also there of course) since Bash goes out of its way to preserve backwards compatibility. Considering its legacy, Bash can run scripts more than a decade before Python even existed. The only other scripting language i know of that has similar backwards compatibility is Tcl. Perhaps Perl too (ignoring Perl 6 that was renamed to something else again due to it being practically a different language).
- Doingmything123 7y agoI guess it's the trade off between backwards compatibility and readability/correctness/maintainability. Both languages will have technical debt overtime. In my opinion, I would much rather have to rarely update python scripts in exchange to for peace of mind that I can fix/update the script as need be. In my experience, *sh scripts can be fragile and hard to ensure correctness.
- Aloha 7y agoHaving migrated something recently from python2 to python3, I'll take bash. I actually ported two of the three scripts to bash, and the third one I reluctantly moved to python3 (I needed python-pil). String handling in python3 in my opinion is a tire fire compared to bash.
- lizmat 7y ago> Perl 6 that was renamed to something else Indeed, it has been renamed to Raku (https://raku.org https://raku.org using the #rakulang tag on social media).
- ShorsHammer 7y agoHaving watched people hack together utterly bizarre bash scripts with awk and grep to handle json responses I'd very much agree here. And before someone raises jq, what % of servers already have python installed vs jq?
- heavyset_go 7y ago> And before someone raises jq, what % of servers already have python installed vs jq? How many of those servers have something like requests installed out of the box vs needing to fetch it via a package manager or PyPI? jq is available in Ubuntu's universe repository along with requests.
- ShorsHammer 7y agoit's python, requests is one of it's many libraries for the purpose, would actually be quite concerned hiring someone who doesn't know the multiple stdlib solutions for this and still claims ability in python. import json import urllib.request tough hey
- heavyset_go 7y agoI'd be as concerned with someone not opting to use what the documentation recommends[1] in lieu of the stdlib, as I would someone who puts together an awkward Bash script to parse JSON instead of just installing the 50kb package for jq. > The Requests package is recommended for a higher-level HTTP client interface. [1] https://docs.python.org/3/library/urllib.request.html https://docs.python.org/3/library/urllib.request.html
- ShorsHammer 7y agoIt's a lot easier to hire people than fire them friend. The documentation recommends nothing of the sort, it's a direction for consumers. We all live in varied and different worlds, how many varied server environments have you been responsible for all at once? Is it really beyond your amazing programming skills to use the tools at hand in every circumstance? Perhaps it's a chance to demonstrate yet another 3rd party tool which fixes non-existent problems. It's a few bytes difference in your script that is using requests over the numerous and plentiful stdlib options there.
- ekianjo 7y agoPython needs to be installed, maintained, and every single library will have its list of dependencies (along with breaking API changes once in a while). There's a reason Bash exists: it's everywhere by default and you know it will still work as it works now in x years.
- epr 7y agoI'm sorry, but for the type of work he is suggesting (bash script equivalent), you don't need anything besides the standard library. I cannot think of a single system I have used in recent decades that didn't come with python installed by default. For all intents and purposes, Python is as ubiquitous as bash at this point.
- tjoff 7y agoMy phone and router have bash. Even if pyhton is installed it isn't certain if it is v.2 or 3.
- thewhitetulip 7y agoWe use AIX at work and it didn't have Python installed. We had to install it for a code review tool I built. But despite Bash's weird syntax, I love how we can do very complex tasks within a few lines.
- vaingloriole 7y agoBalderdash and hokum. z=1 ; while test ${z} -lt 4 ; do a=${RANDOM:1:3} ; dd if=/dev/urandom count=12 bs=4 of=${a} && b=$(cat ${a}) && echo ${b:$(expr 1 + ${RANDOM} % 4):12} ; ((z++)); done You have one tool that you know well. I have thousands of tools that I've learned daily and I wrap with a bash script..except when I need python/tcl or C. Most of what I do in python/tcl/C is truly complicated, needs to be fast or use a data structure other than an array: as it should be. Most of what I do in the shell is as simple as the userspace tools and bash will let it be. Write the one liner above in python without importing a library.
- hannob 7y agoActually I have recently analyzed some python code that does some shell calls and if I can eliminate them with pure python. It was quite frustrating. A lot of stuff that the shell has simple functionality for can't be done easily in python. Examples: * Call to chown -R. Sure, python can change file owners (os.chown()). But it has no parameter to do this recursively. You need to build a loop around it over some file iteration function. * Call to setfacl. Can't be done with the python standard library, needs pylibacl and you can't rely on it being installed. * Call to killall -HUP [processname]. You can send kill signals with python, but only to pids, not to process names. You'd have to parse the system process list or something like that in order to simulate. In all cases I decided to keep the shell call, it just wasn't worth it converting that to less readable python code.
- ofrzeta 7y agoFair enough, choosing the right tool for the job. On my own servers I can take care that the requires modules are installed and even use virtual envs for stable library versioning. This is how we do Let's Encrypt renewal because I can use a Python library for Route53 authentication that works. Your first example could be solved with something like [os.chown(p, uid, gid) for p in pathlib.Path('.').rglob('*')] Obviously this is more verbose than chown but I think it's ok in a script.