# # Written by Matz Kindahl (matkin@docs.uu.se) # function true () { return 0; } function false () { return 1; } # ---------------- String functions ---------------- # # Usage: join . . . function join () { local sep=$1 shift builtin echo "$1$(shift ; for x do builtin echo -n "$sep$x"; done)"; } # # Usage: split function split () { local IFS="$1" builtin echo $(for x in $2; do builtin echo -n "$x "; done); } # ---------------- List functions ---------------- # Usage: first ... function first () { echo $1; } # Usage: rest ... function rest () { shift ; echo "$@"; } # Usage: get . . . function get () { local pos=$1 shift eval 'echo ${'$pos'}'; } # # Usage: length . . . function length () { echo $#; } # # Usage: map . . . function map () { local func=$1 ; shift for x do $func $x done; } # # Usage: mapb . . . function mapb () { local func=$1 ; shift for x do $func $x & done; } # ---------------- Useful hacks ---------------- # Search path for files matching . is a colon-separated # list of directories. # # Usage: search_path search_path () { local IFS=: for x in $1; do for file in $x/$2; do [ -f $file ] && echo $file done done; } # A replacement for basename(1). # This version is not fully compatible with basename(1), it doesn't # strip of trailing / from the pathname. # # Usage: basename [ ] { } # function basename () { local path="${1##*/}" echo ${path%${2}}; } # 'Execute if executable' # If argument 1 is a file that is executable, call that program with # the supplied arguments. eix () { [ -x $1 ] && "$@" & }