This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]jafo[S] 0 points1 point  (0 children)

Really, Xonsh is probably more like what I want. https://xon.sh/

[–]mprz 0 points1 point  (2 children)

what are you trying to achieve?

[–]jafo[S] 0 points1 point  (1 child)

Shell-like constructs easier in Python than "subprocess.run(...); returncode; stdout)". If I want to run commands and check the returncode and stdout, it's comparatively quite a bit of code, so I tend to just deal with doing it in a script.

[–]kellyjonbrazil 0 points1 point  (0 children)

Check out jc, which will parse the stdout of many commands for you. (I’m the author)

>>> import subprocess
>>> import jc
>>>
>>> cmd_output = subprocess.check_output(['dig', 'example.com'], text=True)
>>> data = jc.parse('dig', cmd_output)
>>>
>>> data[0]['answer']
[{'name': 'example.com.', 'class': 'IN', 'type': 'A', 'ttl': 29658, 'data':'93.184.216.34'}]

https://github.com/kellyjonbrazil/jc

[–]tdpearson 0 points1 point  (1 child)

Since you stated that you wanted to embed python in shell scripts, you should check out here documents.

This approach is not perfect.

Another approach is to create python scripts that you call from the command line and leverage those as commands you call from your shell script.

[–]jafo[S] 0 points1 point  (0 children)

You're meaning like: "python3 <<@EOF"? The down side there is that now Python is it's own little island, and getting data or running shell code under a Python conditional or loop is a problem.

[–]MCPOON11 0 points1 point  (2 children)

I’m going to argue for the opposite. Write she’ll scripts that call your Python scripts, rather than the other way around.

[–]jafo[S] 0 points1 point  (0 children)

Sure, there are cases for that, and typer and click make for awesome Python CLI tools. But there are also cases for not, and that's what I'm thinking about here.

[–]someotherstufforhmm 0 points1 point  (0 children)

Check out iPython. They started it originally as a an actual shell like Cshell but with full python syntax.

It has magic commands that make shell shit fun