Code Monkey home page Code Monkey logo

shell.py's Introduction

shell.py

image

image

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

Set working directory

Set the directory in which the commands are executed:

>>> with shell.cwd('~/server/data/upload/') as old_path:
>>>     shell.ex('find ./images -name "*.png"') | 'minify ./public' >> 'upload.log'

This is equivalent to:

>>> shell.ex('find ~/server/data/upload/images -name "*.png"') | 'minify ~/server/data/upload/public' >> '~/server/data/upload.log'

See test cases for more examples.

Tests

Run tests with nosetests(at least v1.3.0):

$ make test

shell.py's People

Contributors

houqp avatar qiao avatar tburette avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.