What is sh?
sh is a unique subprocess wrapper that maps your system programs to Python functions dynamically. sh helps you write shell scripts in Python by giving you the good features of Bash (easy command calling, easy piping) with all the power and flexibility of Python.
Starting with sh
sh is a full-fledged subprocess interface for Python that allows you to call any program as if it were a function. sh lets you call just about anything that you could run from a login shell much more neatly than you can with subprocess.Popen, and more importantly lets you capture and parse the output much more readily.
The installation of sh is done through the pip command $> pip install sh
The easiest way to get up and running is to import sh directly or import your program from sh. Every command you want to run is imported like any other module. That command is then usable just like a Python statement. Arguments are passed per usual, and output can be captured and worked with in a like fashion.
Executing Commands with sh
Commands are called just like functions. "Note that these aren't Python functions, these are running the binary commands on your system dynamically by resolving your PATH, much like Bash does. In this way, all the programs on your system are easily available in Python."
Many programs have their own command subsets, like git (branch, checkout). sh handles subcommands through attribute access.
Keyword arguments also work like you'd expect: they get replaced with the long-form and short-form commandline option. [source]
"Which" finds the full path of a program, or returns None if it doesn't exist. This command is one of the few commands implemented as a Python function, and therefore doesn't rely on the "which" program actually existing.
There are many more features that you can use with sh, and you can find them all in the official documentation.
sh is capable of "baking" arguments into commands.
Baked ssh command
Calling "bake" on a command creates a callable object that automatically passes along all of the arguments passed into "bake".
Now that the “myserver” callable represents a baked ssh command, you can call anything on the server easily:
For more advanced features, please see the official documentation.