by phillco · 6y ago · 0 comments
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).
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).I've written simple small scripts with it for my last two jobs.