1#!/bin/bash 2 3[ -f testing.sh ] && . testing.sh 4 5testing "integer" "expr 5" "5\n" "" "" 6testing "integer negative" "expr -5" "-5\n" "" "" 7testing "string" "expr astring" "astring\n" "" "" 8testing "addition" "expr 1 + 3" "4\n" "" "" 9testing "5 + 6 * 3" "expr 5 + 6 \* 3" "23\n" "" "" 10testing "( 5 + 6 ) * 3" "expr \( 5 + 6 \) \* 3" "33\n" "" "" 11testing ">" "expr 3 \> 2" "1\n" "" "" 12testing "* / same priority" "expr 4 \* 3 / 2" "6\n" "" "" 13testing "/ * same priority" "expr 3 / 2 \* 4" "4\n" "" "" 14testing "& before |" "expr 0 \| 1 \& 0" "0\n" "" "" 15testing "| after &" "expr 1 \| 0 \& 0" "1\n" "" "" 16testing "| & same priority" "expr 0 \& 0 \| 1" "1\n" "" "" 17testing "% * same priority" "expr 3 % 2 \* 4" "4\n" "" "" 18testing "* % same priority" "expr 3 \* 2 % 4" "2\n" "" "" 19testing "= > same priority" "expr 0 = 2 \> 3" "0\n" "" "" 20testing "> = same priority" "expr 3 \> 2 = 1" "1\n" "" "" 21 22testing "00 | 1" "expr 00 \| 1" "1\n" "" "" 23testing "-0 | 1" "expr -0 \| 2" "2\n" "" "" 24testing '"" | 1' 'expr "" \| 3' "3\n" "" "" 25testing "/ by zero" "expr 1 / 0 2>/dev/null; echo \$?" "2\n" "" "" 26testing "% by zero" "expr 1 % 0 2>/dev/null; echo \$?" "2\n" "" "" 27 28testing "regex position" "expr ab21xx : '[^0-9]*[0-9]*'" "4\n" "" "" 29testing "regex extraction" "expr ab21xx : '[^0-9]*\([0-9]*\).*'" "21\n" "" "" 30testing "regex no match" "expr ab21xx : x" "0\n" "" "" 31testing "long str" "expr abcdefghijklmnopqrstuvwxyz : '\(.*\)' = a" "0\n" "" "" 32 33# result of ':' regex match can subsequently be used for arithmetic 34testing "string becomes integer" "expr ab21xx : '[^0-9]*\([0-9]*\)' + 3" \ 35 "24\n" "" "" 36 37testing "integer comparison" "expr -3 \< -2" "1\n" "" "" 38testing "string comparison" "expr -3 \< -2s" "0\n" "" "" 39testing "integer expression comparison" "expr 2 - 5 \< -2" "1\n" "" "" 40# result of arithmetic can subsequently be compared as a string 41testing "string expr comparison" "expr 2 - 5 \< -2s" "0\n" "" "" 42 43testing "parens around literal" "expr \( a \)" "a\n" "" "" 44 45testing "exit code when true" "expr a; echo \$?" "a\n0\n" "" "" 46testing "exit code when false" "expr 0; echo \$?" "0\n1\n" "" "" 47testing "exit code with syntax error" "expr \( 2>/dev/null; echo \$?" \ 48 "2\n" "" "" 49testing "exit code when evaluating to 0" "expr -1 + 1; echo \$?" "0\n1\n" "" "" 50 51# BUG: segfaults because '3' is coerced to integer and regexc gets NULL 52testing "regex segfault" "expr 3 : '\(.\)'" "3\n" "" "" 53 54# syntax errors 55testing "no expression" "expr 2>/dev/null; echo \$?" "2\n" "" "" 56testing "empty ()" "expr \( \) 2>/dev/null; echo \$?" "2\n" "" "" 57testing "missing )" "expr \( 1 2>/dev/null; echo \$?" "2\n" "" "" 58testing "missing outer )" "expr \( 1 + \( 2 \* 3 \) 2>/dev/null; echo \$?" \ 59 "2\n" "" "" 60testing "bad operator" "expr 1 @ 2 2>/dev/null; echo \$?" "2\n" "" "" 61testing "adjacent literals" "expr 1 2 2>/dev/null; echo \$?" "2\n" "" "" 62testing "non-integer argument" "expr 1 + a 2>/dev/null; echo \$?" "2\n" "" "" 63