13 ms·
Many people advise against using shell scripts for non trivial stuff. Is there a language as convenient as shell for calling and piping external programs but wi
by jraph 4y ago
Many people advise against using shell scripts for non trivial stuff. Is there a language as convenient as shell for calling and piping external programs but with proper typing and error handling?
People advise using Python but I don't see myself using subprocess and Popen for gluing programs together and end up writing shell scripts anyway, I'm pretty comfortable with posix shell.
- grumblehound 4y agoI tried https://plumbum.readthedocs.io/en/latest/ https://plumbum.readthedocs.io/en/latest/ recently and it was quite good. You might want to write a wrapper function depending on what error or newline splitting behaviour you want.
- remram 4y agoWhat's wrong with Python and the subprocess module? It is exactly what I advise.
- travisjungroth 4y agoIf you have a single command it’s fine. Not great because of the list syntax, but fine. But I’ve found when I’m doing shell commands and using Python as glue, the Python takes up most of the script while doing barely any of the work. Don’t get me wrong, I like Python more and can barely write shell (ChatGPT/Copilot has made it easy). But it is not as convenient for calling and piping commands as shell.
- bqmjjx0kac 4y agoOnce I started running Shellcheck on my scripts, I felt like they were full of footguns waiting to go off.
- nickjj 4y ago> I'm pretty comfortable with posix shell. It's ok to trust yourself. Write 1 file shell scripts for convenient zero dependency command line tools that will work on most systems. If things start to get rough due to shell limitations, then Python scripts without dependencies is another 1 file solution that's quite portable. You can even mix and match (mostly use shell and call Python from it for the complicated bits as a last resort). I wrote about this exact topic recently around trying to deal with string splitting in Bash: https://nickjanetakis.com/blog/splitting-strings-by-a-delimiter-with-ifs-and-bash-string-replace https://nickjanetakis.com/blog/splitting-strings-by-a-delimi... The takeaway is that for 99% of cases using IFS or Bash's string replace in a variable is great but if you have strict parsing requirements then maybe going with Python is worth it. The post links to a massive SO answer that demonstrates how string manipulation in shell scripting is hard.
- loevborg 4y agoShell scripting's best kept secret is babashka (https://babashka.org/ https://babashka.org/). - It's a flavor of Lisp (Clojure, specifically), a language whose flexibility makes it ideally suited for gluing together programs and working with data - Compare to alternative scripting shells, babashka is very pragmatic and 100% production ready today, as it's built on top of Java, GraalVM and Clojure - Even though it's Java under the hood, it's FAST (`time bb -e '(+ 1 1)' -> 0.014 ms) - Support for filesystem operations (e.g. globbing) is as good or better than Bash - Working with subprocesses is better than in bash, because there are fewer gotchas - Piping is easier in bash, but most of the time you _don't_ really want to use pipes. `cat /etc/fstab | grep /usr` should be a set of function calls, not subprocesses Give it a try, you won't regret it
- User23 4y agoPerl has the attributes you describe.
- BiteCode_dev 4y agoIf you code in Python, your probably should use the language as much as possible and avoid calling shell commands. E.G: - manipulate the file system with pathlib - do hashes with hashlib - zip with zipfile - set error code with sys.exit - use os.environ for env vars - print to stderr with print(..., file=...) - sometimes you'll need to install lib. Like, if you want to manipulate a git repo, instead of calling the git command, use gitpython (https://gitpython.readthedocs.io/en/stable/ https://gitpython.readthedocs.io/en/stable/) But if you don't feel like installing a too many libs, or just really want to call commands because you know them well, then the "sh" lib is going to make things smoother: https://pypi.org/project/sh/ https://pypi.org/project/sh/ Also, enjoy the fact Python comes with argparse to parse script arguments (or if you feel like installing stuff, use typer). It sucks to do it in bash . If what you need is more build oriented, like something to replace "make", then I would instead recommend "doit": https://pydoit.org/ https://pydoit.org/ It's the only task runner that I haven't run away from yet. Remember to always to everything in a venv. But you can have a giant venv for all the scripts, and just she-bang the venv python executable so that it's transparent. Things don't have to be difficult. Finally, sometimes it's just easier to do things in ipython directly. Indeed, with a good PYTHONSTARTUP script, you don't have to import everything, you get superb completion+help, and you can call any bash command by just doing "!cmd". In fact, the result can be store in a python variable. Also you can set autocall so that you don't have to write parenthesis in simple function calls.
- c-hendricks 4y agoIs this satire?
- 8n4vidtmkvmk 4y agozx is ok. it's just a node package that makes working with subprocesses easier. but js does good async.
- deleted 4y ago[deleted]