Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

I'd argue that you should probably stick to one subset of things in your bootstrap script -- and I'd say grep, awk, sed and (ba)sh go together, anything "higher level" like python/ruby/perl/tcl does not fit within that. You might want to check for python with a combination of "python -V" and the dance described above -- and, as part of bootstrapping, make a symlink (or copy, if you need to support windows and/or a filesystem without symlink support) to eg: python2. Save that tidbit as "assert-python2.sh" and then first "assert-python2.sh" then "check-bootstrap-deps.sh" and finally "bootsrap.sh" :-)


sort by: page size:

Bash is the glue that's present everywhere. If you need a script to bootstrap something you're better off writing it in Bash than Python.

If you think Python is the answer then you have to deal with the whole nonsense around pipenv/poetry/pyenv/asdf/virtualenv/setup.py/requirements.txt/pyproject.toml/wheels/... 5 hours later .../system packages/anaconda.


python is also extremely fragile compared to something compiled

just setting up the python env to run the "script" requires a ton of bash (or simpler python)

of course it's true in all cases that if you know what's your supported/required/target environment is, then just a few lines of bash/python is enough. but the wider the spectrum of complexity one must handle the more it pays off to front load said managing, and distribute a compact and powerful program with few dependencies and as minimal complicated bootstrap steps as possible.


If you’re writing shell scripts you should have https://www.shellcheck.net/ in your editor and pre-commit hooks to catch common footguns.

Even then, my threshold for “this should be Python” has shrunk over the years: I used to say “greater than one screen of code” but now it’s more like “>1 branch point or any non-trivial scalar variable”.


I recommend Greg's Bash Wiki ... https://mywiki.wooledge.org/BashGuide. See general notes, then at the bottom of the page are many links to additional considerations.

Like others say, "bash" is a hard tool to get right (and I'm not saying I do it right either, necessarily, but Greg's Wiki was real helpful!). I'm building a hybrid bash/python3 environment now (something I'll hopefully open-source at some point), and bash is just the "glue" to get things set up so most aspects of development can funnel through to python3 + other tools.

But ... things that make bash real useful:

    * it's available everywhere (even in Windows with Ubuntu-18.04/WSL subsystem)
    * it can bootstrap everything else you need
    * it can wrap, in bash functions, aliases, and "variables" (parameters), the
      real functionality you want to expose ... the
      guts can be written in python3 or other tools
Without a good bash bootstrap script you end up writing 10 pages of arcane directions for multiple platforms telling people to download 10 packages and 4 pieces of software per platform, and nobody will have consistent reproducible environments.

EDIT: I think there's a revised version of Greg's Bash Wiki in the works.


Ah, so using python for stuff bash is usually used for. I thought OP meant writing python scripts to be used as components of bash scripts.

For _that_ type of python, I usually use the sh module[0]. It handles piping pretty well, especially if you use StringIO to deal with the pipes. Unfortunately, return code-based conditionals are nowhere as simple as || and && are in shell.

[0] http://amoffat.github.io/sh/


plus if you're going for bash instead of python to avoid dependencies, just go for sh. It's not like it would be hard to code or anything in sh.

So... He was ... Bashing bash?,

I for one like my shells dirty with hidden corners and black magic. Name anywhere where you do not have bash available; embedded, grub, rescue boot shells. I don't do embedded. Never will.

But on a serious note, does anyone know a good resource where I can pick up a better la guage and never have to bash script again? I love writing it. It I am sooo restricted by it, yet keep solving problems with it and also making good money like last night. Maybe Python is the way?


I usually replace shell scripts with python (using sh module: https://amoffat.github.io/sh/ for calling other scripts/programs).

Ok, go ahead and port all 200 of these scripts to Python please: https://github.com/shawwn/scrap

PR's welcome. ;)

Python makes sense for scripts that need good argument parsing, or complicated intermediate input processing. But it's pretty annoying to get a shell pipeline working in Python. `foo | bar | baz` is about 15 characters in bash.

Here's an example. I use `llbranch` all the time: https://github.com/shawwn/scrap/blob/master/llbranch

It lists all branches in a project. It's nicely colorized. What would the equivalent Python be? Who knows, but it'd be much longer.


It checks if the variables 'ls' and 'la' exist, and if they do, it'll run it as python. If not, it'll try to parse it as bash. If it can't parse it as bash, it'll try again in python mode.

https://xon.sh/tutorial.html#python-mode-vs-subprocess-mode


Tried python-sh?

Your first mistake is dropping those scripts everywhere. Name them sanely and drop them somwhere in PATH. Add your own path to PATH just for these scripts if you don't have it already.

Your second mistake may be that you are writing python instead of a nice shell (eg. zsh is pretty good, way more powerful than bash) and ofloading to python only those parts that aint suitable for shell.

Now since you'd be offloading only sh-unsuitable parts as other programs, you can as well write them in whatever is best fit. Be it python, rust, c, haskell or whatever.


Python with sh.py is often alright in the place of a shell script.

Sometimes it's nice to have a pure bash script if you don't know your environment.

If the script is simple enough it might be worth learning and implementing a bash script than trying to test if Python is installed and the right version etc.


FWIW I wrote a pretty complete shell in ~11K lines of Python:

https://github.com/oilshell/oil (osh/ and core/ directories):

Right now the goal is to clone bash but have better parse time and runtime errors. I hope to make an initial release this summer. But the project is much larger (see the blog if interested).


So you use python instead of sh, but you argue that we should use sh instead of bash, perl, or python. I'm not convinced.

IMHO, we should use proper tool for the job instead of making artificial limitations. Bash is proper tool in lot of cases, unless old proprietary OS or very limited embedded OS are targeted.


Thanks for the recommendation but the shell script i have works good enough and has been for a few years now. Plus the container (FreeBSD jail to be precise) is pretty low footprint so while i don't have an issue with Python itself, it's an additional package I don't really need.

Shellcheck clean. But I still want to port it to python.

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"))

next

Legal | privacy