#!/bin/sh # #=============================================================================== # # check_your_awk version 0.2 (2011-12-10) # written by cleemy desu wayo # # This work is dedicated to the public domain. # # History: # * version 0.2 (2011-12-10) # * Added "handling null byte" issue. # * Changed a one-liner for "gsub()" issue. # * Changed a one-liner for "backslash in sub() and gsub()" issue. # * Changed one-liners for "number or string" issue. # * Rewrote check_your_awk_exec function. # * Changed some other minor stuff. # # * version 0.1 (2011-12-04) # * The initial release. # # Older versions: # http://nuullmoon.com/kura/allversions/ # # References: # http://blog.livedoor.jp/corbie/ # #=============================================================================== # LANGUAGE=C LC_ALL=C LANG=C check_your_awk_list="nawk::nawk gawk::gawk mawk::mawk busybox:awk:busybox original-awk::original-awk " check_your_awk_exec () { printf "%s\n" "==================================================" printf "%s\n" "${2}" printf "%s\n" "==================================================" printf "[code]\n%s\n\n" "`printf "%s" "${3}" | sed -e 's/#command#/awk/'`" printf "%s" "${1}" | while read check_your_awk_exec_list_curr_line ; do check_your_awk_exec_command=`printf "%s" "${check_your_awk_exec_list_curr_line}" | cut -d: -f1 2> /dev/null` check_your_awk_exec_command_param=`printf "%s" "${check_your_awk_exec_list_curr_line}" | cut -d: -f2 2> /dev/null` check_your_awk_exec_id=`printf "%s" "${check_your_awk_exec_list_curr_line}" | cut -d: -f3 2> /dev/null` if which "${check_your_awk_exec_command}" > /dev/null 2>&1 ; then printf "[%s]\n" "${check_your_awk_exec_id}" eval "`printf "%s" "${3}" | sed -e 's/#command#/'"${check_your_awk_exec_command} ${check_your_awk_exec_command_param}"'/' 2>&1`" 2>&1 printf "\n" fi done printf "\n" } printf "%-18s: %s\n" "version" "check_your_awk version 0.2 (2011-12-10)" printf "%-18s: " "time" if test ! -z "`date "+%Y-%m-%d %H:%M:%S" 2> /dev/null`" ; then date "+%Y-%m-%d %H:%M:%S" 2> /dev/null | tr -d '\n' else date 2> /dev/null | tr -d '\n' fi printf "\n" printf "%-18s: " "uname" if test ! -z "`uname -a 2> /dev/null`" ; then uname -a 2> /dev/null | tr -d '\n' else uname 2> /dev/null | tr -d '\n' fi printf "\n" printf "%s" "${check_your_awk_list}" | while read s ; do check_your_awk_command=`printf "%s" "${s}" | cut -d: -f1 2> /dev/null` check_your_awk_id=`printf "%s" "${s}" | cut -d: -f3 2> /dev/null` printf "%-18s: " "${check_your_awk_id}" if which "${check_your_awk_command}" > /dev/null 2>&1 ; then if test "${check_your_awk_command}" = "busybox" ; then printf "" | busybox 2>&1 | head -1 | tr -d '\n' else if test ! -z "`printf "" | "${check_your_awk_command}" -W version 2> /dev/null`" ; then printf "" | "${check_your_awk_command}" -W version 2>&1 | head -1 | tr -d '\n' else printf "" | "${check_your_awk_command}" --version 2>&1 | head -1 | tr -d '\n' fi fi else printf "%s" "(not found)" fi printf "%s\n" "${check_your_awk_version}" done printf "\n\n" check_your_awk_exec "${check_your_awk_list}" \ 'func' \ '#command# '"'"'func a(){ print "hello" } BEGIN{ a() }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'length()' \ '#command# '"'"'BEGIN{ a[0]=1; a[1]=2; print length(a) }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'close()' \ '#command# '"'"'BEGIN{ f="h9g7Tnw3"; while (getline s < f > 0) {print s;}; print "open done."; close(f); print "close done." }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'regex character class' \ '#command# '"'"'BEGIN{ s="a b"; if (s ~ /a[[:space:]]b/) {print s} else {print "unmatched"} }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'hyphen in regex (1)' \ '#command# '"'"'BEGIN{ s="a-b"; if (s ~ /a[a\-a]b/) {print s} else {print "unmatched"} }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'hyphen in regex (2)' \ '#command# '"'"'BEGIN{ s="a-b"; if (s ~ /a[\#\-\#]b/) {print s} else {print "unmatched"} }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'backslash in regex' \ '#command# '"'"'BEGIN{ s="a(b)c"; if (s ~ "a\(b\)c") {print s} else {print "unmatched"} }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'backslash in sub() and gsub()' \ '#command# '"'"'BEGIN{ s="a"; sub(/a/, "\\\\", s); print s }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'gsub()' \ '#command# '"'"'BEGIN{ s1=s2="aaabbbaaa"; gsub(/^a/, "", s1); gsub(/^a*/, "", s2); printf "s1 : %-10s s2 : %s\n", s1, s2 }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'output null byte (1)' \ '#command# '"'"'BEGIN{ print "\0" }'"'"' 2>&1 | od -t x1 | head -1' check_your_awk_exec "${check_your_awk_list}" \ 'output null byte (2)' \ '#command# '"'"'BEGIN{ printf "%c", "\0" }'"'"' 2>&1 | od -t x1 | head -1' check_your_awk_exec "${check_your_awk_list}" \ 'output null byte (3)' \ '#command# '"'"'BEGIN{ printf "%c", 0 }'"'"' 2>&1 | od -t x1 | head -1' check_your_awk_exec "${check_your_awk_list}" \ 'handling null byte (1)' \ '#command# '"'"'BEGIN{ s=sprintf("%c", 0); printf "%c", s }'"'"' 2>&1 | od -t x1 | head -1' check_your_awk_exec "${check_your_awk_list}" \ 'handling null byte (2)' \ '#command# '"'"'BEGIN{ s=sprintf("%c", 0); print length(s) }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'handling null byte (3)' \ '#command# '"'"'BEGIN{ s=sprintf("aa%cbb", 0); printf "%s", s }'"'"' 2>&1 | od -t x1 | head -1' check_your_awk_exec "${check_your_awk_list}" \ 'handling null byte (4)' \ '#command# '"'"'BEGIN{ s=sprintf("aa%cbb", 0); print length(s) }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'handling null byte (5)' \ 'dd if=/dev/zero bs=1 count=5 2> /dev/null | #command# '"'"'BEGIN{ RS=";" } END{ print NR }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'handling null byte (6)' \ 'dd if=/dev/zero bs=1 count=5 2> /dev/null | #command# '"'"'BEGIN{ RS=sprintf("%c", 0) } END{ print NR }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'number or string (1)' \ 'echo "a10" | #command# '"'"'function f(s){ sub(/a/, "", s); print ((s > 5) ? "num" : "str") } { f("a10"); f($1); }'"' 2>&1" check_your_awk_exec "${check_your_awk_list}" \ 'number or string (2)' \ 'echo "10" | #command# '"'"'function f(s){ print ((s > 5) ? "num" : "str") } { s1=s2=10; s3=s4=$1; sub(/a/, "", s2); sub(/a/, "", s4); f(s1); f(s2); f(s3); f(s4) }'"' 2>&1" exit 0