Hacker News new | ask | show | jobs
by hansdieter1337 2171 days ago
Even better: Don’t use bash. I started using Python instead of bash. It’s way better to read and more maintainable. If I need the performance of native-Unix commands, I can still use them using subprocess.
4 comments

Python is not as handy/efficient as bash when you want to utilize some existing Unix commands. And normally it’s crucial to stop bleeding fast if something bad is happened to you server/cluster
I've been migrating shell scripts to python and have found the 'sh' library invaluable for pulling in pure CLI commands from the bash script and adding to the python implementation. http://amoffat.github.io/sh/ It's essentially an abstraction layer above subprocess. Quick example - to use ifconfig natively in python:

from sh import ifconfig print(ifconfig("wlan0"))

Python is surprisingly bad at managing subprocesses, though. Read the subprocess docs closely, it's quite easy to get deadlocks if you try to compose operations the way you can in bash.
There are libraries that fix that: https://pypi.org/project/sh/ if you are in position to use external libraries.
bash is everywhere
So is python
But which python?