https://github.com/obfusk/map.sh
map.sh - map/filter for bash
https://github.com/obfusk/map.sh
bash map shell
Last synced: about 2 months ago
JSON representation
map.sh - map/filter for bash
- Host: GitHub
- URL: https://github.com/obfusk/map.sh
- Owner: obfusk
- Created: 2013-10-23T01:07:26.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-11-18T22:02:07.000Z (over 12 years ago)
- Last Synced: 2025-06-04T11:36:54.925Z (about 1 year ago)
- Topics: bash, map, shell
- Language: Shell
- Homepage:
- Size: 137 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[]: {{{1
File : README.md
Maintainer : Felix C. Stegerman
Date : 2013-11-18
Copyright : Copyright (C) 2013 Felix C. Stegerman
Version : v0.2.0
[]: }}}1
## Description
[]: {{{1
map.sh - map/filter for bash
`map` evaluates an expression for arguments, files, or lines;
`filter` filters arguments, files, or lines based on the exit status
of an expression; the expression can refer to its argument as `$it`.
Options:
* `-v` prints the expression before evaluating it; only for `map`
* `-t` ignores non-zero exit status; only for `map`
* `-l` maps/filters lines instead of arguments
* `-0` input lines end with 0 byte, not newline; implies `-l`
* `-z` output lines end with 0 byte, not newline; only for `filter`
* `-f` treats arguments/lines as files, providing the canonical path
as `$path`, the absolute path as `$abs`, the basename as `$base`,
the dirname as `$dir`, and the dirnames of the canonical and
absolute paths as `$path_dir` and `$abs_dir`
#
The canonical path is the result of `Cwd::abs_path`: the absolute
path with all symlinks followed.
[]: }}}1
## Examples
[]: {{{1
[]: {{{2
```bash
map 'echo $it' foo bar baz
# foo
# bar
# baz
map -v 'echo "$it"; echo ${#it}' foo 'bar baz'
# ==> echo foo
# foo
# ==> echo 3
# 3
# ==> echo 'bar baz'
# bar baz
# ==> echo 7
# 7
map -f 'echo "$path"' foo bar/baz
# /canonical/path/to/foo
# /canonical/path/to/baz
```
```bash
# find files and echo dirname of canonical path
find -type f -print0 | sort -z | map -f0 'echo "$path_dir"'
# print message for existing files
map -t 'test -e "$it" && echo "$it exists"' foo bar baz
# run git command in multiple directories, like mgit
map 'cd "$it"; pwd; git status; echo' *
```
[]: }}}2
[]: {{{2
```bash
filter '[ -e "$it" ]' existing-file nonexisting-file
# existing-file
filter '[[ "$it" =~ foo ]]' foo bar food
# foo
# food
filter '[ -L "$it" ]' some/file some/link | map -fl 'echo "$abs"'
# /absolute/path/to/some/link
```
```bash
# find 4-char files
find -type f -print0 | sort -z | filter -f0 '[ "${#base}" == 4 ]'
```
[]: }}}2
[]: }}}1
## License
[]: {{{1
GPLv2 [1].
[]: }}}1
## References
[]: {{{1
[1] GNU General Public License, version 2
--- http://www.opensource.org/licenses/GPL-2.0
[]: }}}1
[]: ! ( vim: set tw=70 sw=2 sts=2 et fdm=marker : )