Skip to content

Instantly share code, notes, and snippets.

@hypersoft
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hypersoft/8811532 to your computer and use it in GitHub Desktop.
Save hypersoft/8811532 to your computer and use it in GitHub Desktop.
Path Tools
#!/bin/sh
pipe=cat;
ifilter=./ifilter; [ -e "$ifilter" ] || ifilter=ifilter;
if [ "${1:0:1}" == '-' ]; then
pipe="$ifilter $1";
shift;
fi;
{ if [ $# -gt 0 ]; then
printf "%s\n" "$@";
else while read globline; do ( IFS='';
printf "%s\n" $globline;
); done; fi; } | $pipe;
#!/bin/sh
bang=;
if [ "$1" == '--not' ]; then shift; bang=!; fi;
if [ $# == 2 ]; then
while read line; do
test $bang "$1" "$2" "$line" && printf "%s\n" "$line";
done
else
while read line; do
test $bang "$1" "$line" && printf "%s\n" "$line";
done
fi;
# Run a test on line inputs with optional inversion, printing line if tests true
#!/bin/sh
HOST=https://gist.github.com/
VENDOR=hypersoft
GIST=8811532
NAME="Path Tools"
COPYRIGHT="(C) 2014 Triston J. Taylor All Rights Reserved."
DEST="${DEST:=/usr/local/bin}";
SOURCES="
globlines
ifilter
iprefix
irep
isuffix
subdirs
"
PROVIDES="$(printf "${DEST}/%s\n" $SOURCES)"
OUTDATED="$(for name in $SOURCES; do
target="${DEST}/$name";
[ "$name" -nt "$target" ] && printf "%s\n" "$name";
done)"
REMOVE="$(for name in $PROVIDES; do
[ -e "$name" ] && printf "%s\n" "$name";
done)"
while [ "${1:0:1}" == - ]; do
[ "$1" == '--list-provides' ] && {
[ -n "$PROVIDES" ] && printf "%s\n" $PROVIDES;
exit $?;
} || [ "$1" == '--list-installed' ] && {
[ -n "$REMOVE" ] && printf "%s\n" $REMOVE;
exit $?;
} || [ "$1" == '--list-outdated' ] && {
[ -n "$OUTDATED" ] && printf "$DEST/%s\n" $OUTDATED;
exit $?;
} || [ "$1" == '--remove' ] && {
[ -n "$REMOVE" ] && {
if [ -w "$DEST" ]; then rm -v $REMOVE;
else
echo -n "Permission to write $DEST " >&2;
su root -c "rm -v $(printf "'%s' " $REMOVE)";
fi;
exit $?;
}
}
break;
done;
[ -n "$OUTDATED" ] && {
UPDATE="cp -v $(IFS=$'\n'; printf "'%s' " $OUTDATED) '$DEST'"
if [ -w "$DEST" ]; then "$UPDATE";
else
echo -n "Permission to write $DEST " >&2;
su root -c "$UPDATE";
fi;
} || {
printf "Everything up to date\n" >&2;
}
#!/bin/sh
#inline prefix
sed -re "$(printf 's\1^(.+)$\1';
printf '%s\\1\\n' "$@";
printf '\1')";
#!/bin/sh
argv='-e';
if [ "$1" == -r ]; then
argv="-re"; shift;
fi;
expr=\'s\1$1\1$2\1\';
#inline replace optional extended regular expression
eval "sed $argv $expr";
#!/bin/sh
#inline suffix
sed -re "$(printf 's\1^(.+)$\1';
printf '\\1%s\\n' "$@";
printf '\1')";
#!/bin/sh
# recursive tree list
while test $# != 0; do
test "$1" == "*" && {
shift; continue;
}
test "$1" == "./" && {
"$0" "$1"*;
shift; continue;
}
test "$1" == "." && {
"$0" "$1"/*;
shift; continue;
}
test -d "$1" && {
printf "%s\n" "$1" && "$0" "$1"/*;
}
shift;
done;
@hypersoft
Copy link
Author

To clone this project to your local machine:

git clone https://gist.github.com/hypersoft/8811532
cd 8811532

Perform an install to /usr/local/bin
./configure

Find all path subdirectories

subdirs .

@hypersoft
Copy link
Author

Create a shell glob list for *.c and *.h files

./subdirs * | ./isuffix '/*.'{c,h}

@hypersoft
Copy link
Author

Find all *.c and *.h files

./subdirs * | ./isuffix '/*.'{c,h} | ./globlines -e

@hypersoft
Copy link
Author

Building on the example above, you could then do something exotic like testing if files are older or newer using
... | ifilter "reference-file.example" -o

@hypersoft
Copy link
Author

All *.c files as *.o files
$ ./subdirs * | ./isuffix '/*.c' | ./globlines -e | ./irep -r '.c$' '.o'

@hypersoft
Copy link
Author

As above. This time, specify an output directory for ALL *.o files
./subdirs * | ./isuffix '/*.c' | ./globlines -e | ./irep -r '.c$' '.o' | ./irep -r '([^/])+' 'output'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment