Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timofurrer/shellfuncs
Python API to execute shell functions as they would be Python functions
https://github.com/timofurrer/shellfuncs
api bash environment execute function import interface shell source subprocess
Last synced: 3 days ago
JSON representation
Python API to execute shell functions as they would be Python functions
- Host: GitHub
- URL: https://github.com/timofurrer/shellfuncs
- Owner: timofurrer
- License: mit
- Created: 2016-11-27T14:06:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-05-27T06:49:21.000Z (over 1 year ago)
- Last Synced: 2024-10-23T08:51:02.352Z (12 days ago)
- Topics: api, bash, environment, execute, function, import, interface, shell, source, subprocess
- Language: Python
- Size: 33.2 KB
- Stars: 101
- Watchers: 11
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- starred-awesome - shellfuncs - Python API to execute shell functions as they would be Python functions (Python)
README
# shellfuncs
[![Build Status](https://travis-ci.com/timofurrer/shellfuncs.svg?token=qRcMyciKYsuEPapoF8ny&branch=master)](https://travis-ci.com/timofurrer/shellfuncs)
[![PyPI package version](https://badge.fury.io/py/shellfuncs.svg)](https://badge.fury.io/py/shellfuncs)
[![PyPI python versions](https://img.shields.io/pypi/pyversions/shellfuncs.svg)](https://pypi.python.org/pypi/shellfuncs)Python API to execute functions written in shell script.
Let's assume you have a shell script *counters.sh* like this:
```bash
count_python_imports() {
find -name '*.py' | xargs grep -e '^import os$' -e '^import sys$' -e '^import re$' | cut -d: -f2 | sort | uniq -c
}
```And you want to execute the `count_python_imports` function within Python. Instead of using cumbersome *subprocess* wouldn't it be awesome to do something like this:
```python
import shellfuncsfrom counters import count_python_imports
returncode, stdout, stderr = count_python_imports()
```*Yeah, yeah, I know about easier ways of achieving the above, too. Thanks.*
## Why should I use that?
* use existing shell scripts in a pythonic way
* complex piping stuff might be easier to implement in shell script
* testing shell scripts is a pain in the a\*\* - with Python it'll be easier## Installation
The recommended way to install **shellfuncs** is to use `pip`:
```shell
pip install shellfuncs
```## Usage
**shellfuncs** can be configured on different levels.
The following configuration variables are available:
* shell (defaults to `/bin/sh`)
* env (defaults to `os.environ`)### Configuration via environment variables
Set the default shell via `SHELLFUNCS_DEFAULT_SHELL` environment variable:
```bash
export SHELLFUNCS_DEFAULT_SHELL=/bin/bash
```### Configuration via context manager
Set the configuration block-wise with a context manager:
```python
import shellfuncswith shellfuncs.config(shell='/bin/bash'):
from counters import count_python_importscount_python_imports() # the shell used will be /bin/bash
```### Configuration for specific function call
Set the configuration when function is executed:
```python
import shellfuncsfrom counters import count_python_imports
count_python_imports(shell='/bin/bash')
```***
*
This project is published under [MIT](LICENSE).
*
A [Timo Furrer](https://tuxtimo.me) project.
- :tada: -