A tool for convenient shell scripting in Python. It allows you to write all your shell scripts in Python in a convenient way and in many cases replace Bash/Sh.
For many people bash/sh seem to be pretty complicated. An example would be regular expressions, working with json/yaml/xml, named arguments parsing and so on. There are many things that are much easier in python to understand and work with.
Shell python has no differences from python except for one. Grave accent symbol (`) does not mean eval, it means execution of shell commands. So
ls -l in shell. You can also skip one ` in the end of line
and it will also be a correct syntax. It is also possible to write multiline expressions
` echo test > test.txt cat test.txt `
and long lines
`echo This is \ a very long \ line
Every shellpy expression returns a Result
result = `ls -l
or normally raises an error in case of non zero output of a command
try: result = `ls -l non_existent_file except NonZeroReturnCodeError as e: result = e.result
The result can be either Result or InteractiveResult. Let's start with a simple Result. You can check returncode of a command
result = `ls -l print result.returncode
You can also get text from stdout or stderr
result = `ls -l result_text = result.stdout result_error = result.stderr
You can iterate over lines of result stdout
result = `ls -l for line in result: print line.upper()
and so on.
As it was said before shellpython does not differ a lot from ordinary python. You can import python modules and use them as usual
import os.path `mkdir /tmp/mydir os.path.exists('/tmp/mydir') # True
And you can do the same with shellpython modules. Suppose you have shellpy module
common as in examples directory. So this is how it looks
ls common/ common.spy __init__.spy
So you have directory
common and two files inside:
common.spy. Looks like a python module right? Exactly. The only difference is file extension. For
__init__.spy and other files it must be
.spy. Let's look inside
def common_func(): return `echo 5
A simple function that returns Result of
echo 5 execution. How is it used how in code? As same as in python
from common.common import common_func print('Result of imported function is ' + str(common_func()))
Note that the
common directory must be in pythonpath to be imported.
It uses import hooks described in PEP 0302 -- New Import Hooks. So, whenever importer finds a shellpy module or a file with .spy extension and with the name that you import, it will try to first preprocess it from shellpy to python and then import it using standard python import. Once preprocessed, the file is cached in your system temp directory and the second time it will be just imported directly.
Import of shellpython modules requires import hook to be installed. There are two way how to do it:
- run shellpython scripts with the
shellpy tool as described below in the section Running
- run your python scripts as usual with
python but initialize shellpython before importing any module with
shellpython.init() as in the Example
This script clones shellpython to temporary directory and finds the commit hash where README was created
import tempfile import os.path from shellpython.helpers import Dir
with Dir(tempfile.gettempdir()): if not os.path.exists('shellpy'): # just executes shell command `git clone https://github.com/lamerman/shellpy.git
# switch to newly created tempdirectory/shellpy with Dir('shellpy'): # here we capture result of shell execution. log here is an instance of Result class log = `git log --pretty=oneline --grep='Create' # shellpy allows you to iterate over lines in stdout with this syntactic sugar for line in log: if line.find('README.md'): hashcode = log.stdout.split(' ') print hashcode exit(0) print 'The commit where the readme was created was not found'
Two lines here are executed in shell
git clone https://github.com/lamerman/shellpy.git and
git log --pretty=oneline --grep='Create'. The result of the second line is assigned to variable
log and then we iterate over the result line by line in the for cycle
You can install it either with
pip install shellpy or by cloning this repository and execution of
setup.py install. After that you will have
shellpy command installed.
You can try shellpython by running examples after installation. Download this repository and run the following command in the root folder of the cloned repository:
There is also so called allinone example which you can have a look at and execute like this:
It is called all in one because it demonstrates all features available in shellpy. If you have python3 run instead:
It works on Linux and Mac for both Python 2.x and 3.x. It should also work on Windows.
Now it is hardcoded in
header_root.tpl with pretty generic
#!/usr/bin/env python . But maybe it should be possible to configure it.
We need to think how automatic testing of shellpy functions will be done and create a couple of examples of tests. Also some small article on it is needed in wiki.
Here is an example
^CTraceback (most recent call last):
File "/tmp/shellpy_root/root/cr/scripts/monitor.py", line 272, in
Possibly it should start with the line File "/tmp/shellpy_root/root/cr/scripts/monitor.py", line 269, in main
causes an exception to be thrown because python interprets it as a format specifier.
What if two shellpy processes will write one preprocessed file to cache in one time?
Now stdout in non interactive mode is not printed immediately, it waits when the process finishes. It would be great to see stdout, stderr as soon as it comes.
shell shell-script python