bugfixes, features, documentation, examples, new tool
[hband-tools.git] / user-tools / filterexec
blobe1f9f8a462b32940483ea3048ce356fb0f79d371
1 #!/bin/bash
3 true <<'EOF'
5 =pod
7 =head1 NAME
9 filterexec - Echo those arguments with which the given command returns zero.
11 =head1 SYNOPSIS
13 filterexec I<COMMAND> [I<ARGS>] -- I<DATA-1> [I<DATA-2> [... I<DATA-n>]]
15 =head1 DESCRIPTION
17 Prints each I<DATA> (1 per line) only if command I<COMMAND ARGS DATA> exits
18 succesfully, ie. with zero exit status.
20 If you want to evaluate not command line arguments, but data read on STDIN,
21 then combine filterexec(1) with foreach(1).
23 =head1 EXAMPLE
25 filterexec test -d -- $(ls)
27 Shows only the directories.
28 The shell's tokenization may wrongly splits up file names containing space.
29 Perhaps set C<IFS> to newline only.
31 ls -1 | foreach filterexec test -d --
33 Same, but file names are supplied 1-by-1, not all at once,
34 hence filterexec(1) is invoked multiple times.
36 =cut
38 EOF
40 # text editor syntax highlighter fix: '
44 declare -a command=()
45 declare -a items=()
47 if [ "$1" = --help ]
48 then
49 pod2text "$0"
50 exit 0
53 while [ $# -gt 0 ]
55 case "$1" in
56 --)
57 shift
58 break;;
60 command+=("$1")
62 esac
63 shift
64 done
66 items=("$@")
68 for item in "${items[@]}"
70 if "${command[@]}" "$item"
71 then
72 printf '%s\n' "$item"
74 done