sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

D2051.sh (2665B)


      1 #!/usr/bin/env bash
      2 
      3 contexts=('echo $((%s))' 'echo $[%s]' '((%s))' 'b[%s]=' ': ${b[%s]}' 'b=([%s]=)' ': ${v:%s}' ': "${v:%s}"')
      4 quote_types=('\10' "'10'" "\$'10'" '"10"' '$"10"' 'a[\10]' 'a["10"]' '`echo 10`')
      5 
      6 function test-quote-error {
      7   local cmd
      8   printf -v cmd "$1" "$2"
      9   [[ ! $(eval "$cmd" 2>&1 1>/dev/null) ]]
     10 }
     11 
     12 function sub:test-compact {
     13   local v=1
     14 
     15   local t q cmd vec
     16   for t in "${contexts[@]}"; do
     17     vec=
     18     for q in "${quote_types[@]}"; do
     19       if test-quote-error "$t" "$q"; then
     20         vec+=o
     21       else
     22         vec+=x
     23       fi
     24     done
     25     printf -v cmd "$t" expr
     26     printf "%-20s: %s\n" "$cmd" "$vec"
     27   done
     28 }
     29 
     30 function sub:summarize-compact {
     31   printf '%-20s: 4.3     4.4     5.1     5.2     dev\n'
     32   paste \
     33     -d ' ' \
     34     <(bash-4.3 "$0" test-compact) \
     35     <(bash-4.4 "$0" test-compact | sed 's/.* \([ox]\{1,\}\)$/\1/') \
     36     <(bash-5.1 "$0" test-compact | sed 's/.* \([ox]\{1,\}\)$/\1/') \
     37     <(bash-5.2 "$0" test-compact | sed 's/.* \([ox]\{1,\}\)$/\1/') \
     38     <(bash-dev "$0" test-compact | sed 's/.* \([ox]\{1,\}\)$/\1/')
     39 }
     40 
     41 function sub:perform-test {
     42   local v=1
     43 
     44   local t q cmd vec
     45   for t in "${contexts[@]}"; do
     46     for q in "${quote_types[@]}"; do
     47       printf -v cmd "$t" "$q"
     48       if test-quote-error "$t" "$q"; then
     49         vec=o
     50       else
     51         vec=x
     52       fi
     53       printf "%-20s: %s\n" "$cmd" "$vec"
     54     done
     55   done
     56 }
     57 
     58 function main {
     59   printf '%-20s: 4.3 4.4 5.1 5.2 dev\n' COMMAND
     60   paste \
     61     -d ' ' \
     62     <(bash-4.3 "$0" perform-test | sed 's/.$/ & /') \
     63     <(bash-4.4 "$0" perform-test | sed 's/.*\([ox]\)$/ \1 /') \
     64     <(bash-5.1 "$0" perform-test | sed 's/.*\([ox]\)$/ \1 /') \
     65     <(bash-5.2 "$0" perform-test | sed 's/.*\([ox]\)$/ \1 /') \
     66     <(bash-dev "$0" perform-test | sed 's/.*\([ox]\)$/ \1/')
     67 }
     68 
     69 function sub:test-markdown {
     70   local v=1
     71 
     72   local t q cmd vec
     73   for t in "${contexts[@]}"; do
     74     for q in "${quote_types[@]}"; do
     75       printf -v cmd "$t" "$q"
     76       if test-quote-error "$t" "$q"; then
     77         vec='&#x2705;'
     78       else
     79         vec='&#x2B1C;'
     80       fi
     81       printf "| %-20s | %s\n" "<code>$cmd</code>" "$vec"
     82     done
     83   done
     84 }
     85 function sub:summarize-markdown {
     86   printf '| COMMAND | 3.0..4.3 | 4.4..5.0 | 5.1 | 5.2..dev |\n'
     87   printf '|:----------------------|:--------:|:--------:|:--------:|:--------:|\n'
     88   paste \
     89     -d '|' \
     90     <(bash-4.3 "$0" test-markdown) \
     91     <(bash-4.4 "$0" test-markdown | sed 's/.* \([^[:space:]]\{1,\}\)$/ \1 /') \
     92     <(bash-5.1 "$0" test-markdown | sed 's/.* \([^[:space:]]\{1,\}\)$/ \1 /') \
     93     <(bash-5.2 "$0" test-markdown | sed 's/.* \([^[:space:]]\{1,\}\)$/ \1 |/')
     94 }
     95 
     96 if declare -f "sub:$1" >/dev/null; then
     97   "sub:$@"
     98 else
     99   main
    100 fi