Last active
August 29, 2015 13:56
-
-
Save hypersoft/8811532 to your computer and use it in GitHub Desktop.
Path Tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#inline prefix | |
sed -re "$(printf 's\1^(.+)$\1'; | |
printf '%s\\1\\n' "$@"; | |
printf '\1')"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#inline suffix | |
sed -re "$(printf 's\1^(.+)$\1'; | |
printf '\\1%s\\n' "$@"; | |
printf '\1')"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
Create a shell glob list for *.c
and *.h
files
./subdirs * | ./isuffix '/*.'{c,h}
Find all *.c
and *.h
files
./subdirs * | ./isuffix '/*.'{c,h} | ./globlines -e
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
All *.c
files as *.o
files
$ ./subdirs * | ./isuffix '/*.c' | ./globlines -e | ./irep -r '.c$' '.o'
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
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 .