Skip to content

obfusk/map.sh

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
 
 
 
 
 
 
 
 
 
 

[]: {{{1

File        : README.md
Maintainer  : Felix C. Stegerman <flx@obfusk.net>
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

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
# 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

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
# 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 : )