Shell power for Python.
Project description
Bring the good part of Shell scripting to Python.
Install
$ pip install shell.py
Usage
Execute a shell command
Block until return:
>>> from shell import ex
>>> ex('echo hello shell.py').stdout()
'hello shell.py\n'
Asynchronous execution:
>>> from shell import ex
>>> c = asex('echo hello shell.py')
>>> # do something else
...
>>> c.stdout() # wait until process exit and read stdout
'hello shell.py\n'
Pipe commands
from shell import ex
re = (ex('ifconfig')
| 'grep -A 1 eth0'
| 'grep inet'
| 'awk "{print $2}"'
| 'cut -d: -f 2').stdout()
Or
from shell import pipe_all
re = pipe_all(['ls -la ~',
'awk "{print $9}"',
'grep -E "^\."',
'wc -l']).stdout()
Use string as stdin
>>> from shell import instream
>>> instream('1 2 3').p('awk "{print $1}"').stdout()
'1\n'
This is equivalent to:
>>> from shell import ex
>>> ex('echo 1 2 3').p('awk "{print $1}"').stdout()
IO redirect
Overwrite a file:
>>> from shell import ex
>>> ex('echo yolo').wr('/tmp/out')
>>> ex('echo yolo') > '/tmp/out'
Append to a file:
>>> from shell import ex
>>> ex('echo yolo').ap('/tmp/out')
>>> ex('echo yolo') >> '/tmp/out'
Run commands in parallel
Block until all commands return:
>>> from shell import parallel as par
>>> par.ex_all(['sleep 2', 'sleep 2']) # return in 2s
Asynchronous parallel execution:
>>> from shell import parallel as par
>>> pe = par.asex_all(['sleep 2', 'sleep 2']) # return immediately
>>> # do something else
...
>>> pe.wait()
See test cases for more examples.
Tests
Run tests with nosetests(at least v1.3.0):
$ make test
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shell.py-0.4.0.tar.gz
(5.5 kB
view details)
File details
Details for the file shell.py-0.4.0.tar.gz
.
File metadata
- Download URL: shell.py-0.4.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3ce3e6e07d975fc4270208dd36f540e37491b33054540f4102ee8ea63fff2c2 |
|
MD5 | ab8c0d8d2e191c8cab6e1dc0fb1356ca |
|
BLAKE2b-256 | 8b6c2660dcc4043d989b36e515e2299e469ad13acd377d55cb0a2389d20300ae |