Skip to content

Commit

Permalink
Bash functions: add stdbuf() for NetBSD and OpenBSD.
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierog committed May 19, 2024
1 parent e7d36bb commit f3cc299
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Although Moulti's Python packages, modules and functions are obviously available
- bash functions:
- `moulti_process_lines()` helps to turn arbitrary output into Moulti steps
- `moulti_check_requirements()` helps to ensure required commands are available
- `stdbuf()` replaces the `stdbuf` utility on NetBSD and OpenBSD

### Changed

Expand Down
6 changes: 3 additions & 3 deletions Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ Per se, Moulti does not:
- wrap lines (use e.g. `fold`)
- distinguish stdout from stderr (it reads a single stream anyway; use e.g. `stderred`)
- tweak the output buffering policy of other processes:
1. look for command-line options and environment variables that explicitly address the issue, e.g. `--line-buffered` or `PYTHONUNBUFFERED=1`
2. use `LD_PRELOAD`-based tools such as `stdbuf` or [cvolny/faketty](https://github.com/cvolny/faketty)
3. use tools that allocate a [pty](https://en.wikipedia.org/wiki/Pseudoterminal), e.g. `script` or [dtolnay/faketty](https://github.com/dtolnay/faketty):
1. look for command-line options and environment variables that explicitly address the issue, e.g. `--line-buffered`, `PYTHONUNBUFFERED=1` or `STDBUF` (NetBSD-specific)
2. use `LD_PRELOAD`-based tools such as `stdbuf` (from the `coreutils` package) or [cvolny/faketty](https://github.com/cvolny/faketty)
3. use tools that allocate a [pty](https://en.wikipedia.org/wiki/Pseudoterminal), e.g. `script`, `unbuffer` (from the `expect` package) or [dtolnay/faketty](https://github.com/dtolnay/faketty):
```bash
script --quiet --return --command 'your command here' /dev/null | moulti pass your_step
```
Expand Down
16 changes: 16 additions & 0 deletions examples/moulti-functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ function moulti_type {
done
}

if [ "$(uname)" == 'NetBSD' ]; then
function stdbuf {
# Ignore arguments and request line buffering for stdout:
while [[ "$1" =~ ^- ]]; do shift; done
STDBUF1='L' "$@"
}
elif [ "$(uname)" == 'OpenBSD' ]; then
if moulti_tool_is_available unbuffer; then
function stdbuf {
# Ignore arguments and call unbuffer:
while [[ "$1" =~ ^- ]]; do shift; done
unbuffer "$@"
}
fi
fi

MOULTI_NEW_STEP_PATTERN='^([-=#@]+)(\s*)(.+?)\2\1$'

function moulti_process_lines {
Expand Down
2 changes: 1 addition & 1 deletion examples/progressbar.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export MOULTI_INSTANCE='generate-ssh-keys'

set -e
source moulti-functions.bash
moulti_check_requirements stdbuf ssh-keygen || true
moulti_check_requirements 'stdbuf|unbuffer' ssh-keygen || true

# Run moulti_exec with stdbuf and a simplified ssh-keygen command in the title:
function ssh_keygen {
Expand Down

0 comments on commit f3cc299

Please sign in to comment.