Created
January 13, 2013 01:14
-
-
Save ormaaj/4521570 to your computer and use it in GitHub Desktop.
more crazy example code
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
#!/usr/bin/env bash | |
${BASH_VERSION+shopt -s extglob lastpipe} | |
function doCompat { | |
if [[ ${!KSH_VERSION} == .sh.version ]]; then | |
builtin pids 2>/dev/null | |
{ | |
eval function BASHPID.get "{ $(</dev/fd/$(($? + 3))) || exit 1; }" | |
} <<-"3" 3<&0- <<-\4 4<&0- | |
.sh.value=$(pids -f '%(pid)d') | |
3 | |
read -r .sh.value _ </proc/self/stat | |
4 | |
elif ! ${BASHPID+false}; then | |
: # test for mksh or Bash | |
else | |
return 1 | |
fi | |
} | |
function let2 { | |
typeset IFS=, | |
printf %s $(($*)) | |
((_)) | |
} | |
function f { | |
if [[ -p /dev/fd/0 ]]; then | |
{ (( $(</dev/fd/0) )) && f || return 1; } <<<"$(( $(<"$1") - 1 ))" | |
elif [[ -f /dev/fd/0 ]]; then | |
{ let2 "$(</dev/fd/0) - 1" || kill -"$BASHPID"; } | f || return 1 | |
else | |
echo 'unexpected input. (shouldnt happen).' >&2 | |
return 1 | |
fi | |
} | |
function main { | |
if doCompat; then | |
if [[ $1 == +([[:digit:]]) ]]; then | |
set -m | |
f <<<"$1" | |
else | |
echo 'First arg must be a digit.' >&2 | |
exit 1 | |
fi | |
else | |
echo 'Requires Bash, A recent ksh93, or mksh >= R41' >&2 | |
exit 1 | |
fi | |
} | |
FUNCNEST=10 | |
main "$@" | |
# vim: set fenc=utf-8 ff=unix ft=sh: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment