4 ms·
I'm curious if there's an equivalent of this for shell scripts, or people use Xonsh itself for this. Simple bash scripts can easily grow into tangled behemoths
by phillco 6y ago
I'm curious if there's an equivalent of this for shell scripts, or people use Xonsh itself for this.
Simple bash scripts can easily grow into tangled behemoths and there's not an easy, incremental rewrite step (like JS → TS) that would add a lot of structure. Jumping straight to Python is usually painful because of all the subprocess calls.
It seems that xonsh could bridge that gap, but I'm not sure how many people use it for that purpose, or if people use something else (e.g. Oil shell).
- geophile 6y agoMarcel (referenced elsewhere in this discussion) addresses this point exactly. I didn't want to invent another scripting language. Python is great for that purpose, except that, as you point out, shelling out is painful. So marcel includes a module, marcel.api, which gives you access to marcel shell capabilities inside of Python. The example I gave elsewhere explores a directory recursively, finds .c and .h files, and counts the lines in them. The shell command was this: ls -fr \ | select (f: f.suffix in ('.c', '.h')) \ | map (f: f.read().count('\n')) \ | red + In Python, it would be: from marcel.api import * print(first(ls(file=True, recursive=True) | select(lambda f: f.suffix in ('.c', '.h')) | map(lambda f: f.read().count('\n')) | red('+'))) You can see that the ls, select, map, and red commands are called directory from the Python code, without having to call Popen, os.system(), etc. Each version of the code (shell, script) runs in one Python process, (not one process per command).
- phillco 6y agoThanks!
- kortex 6y agoI've started using it to replace bash for automated testing. It's easy to add since I'm already installing pytest et al in the container/env. Makes it easy to test situations where I have some python program interacting with other programs thru pipes and such.
- BeetleB 6y agoNot sure what you mean. Are you asking if people can do some kind of shell scripting in Xonsh? The answer is "yes". It's mostly Python, but with extra features so you don't have to do stuff like subprocess, shutil, etc. I've written simple small scripts with it for my last two jobs.