summaryrefslogtreecommitdiffhomepage
path: root/tests/zash/cond.sh
blob: 10bf23e353f8782cefaffe5fc5cf90852fd704d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# [[ ]]: the Korn shell conditional command.
x=hello e= sp='a b  c'
[[ $x == h* ]] && echo "glob match"
[[ $x == "h*" ]] || echo "quoted pattern is literal"
[[ $x = hel?o ]] && echo "single = also matches"
[[ $x != w* ]] && echo "!= pattern"
p='h*'; [[ $x == $p ]] && echo "pattern from variable"
[[ $x == "$p" ]] || echo "quoted variable is literal"
[[ $x == [gh]ello ]] && echo "bracket pattern"
[[ $sp == "a b  c" ]] && echo "no field splitting"
[[ $e ]] || echo "empty string is false"
[[ -n $x && -z $e ]] && echo "&&"
[[ -z $x || -n $x ]] && echo "||"
[[ ! -z $x ]] && echo "!"
[[ ! ! -n $x ]] && echo "!!"
[[ -n $x && ( -z $x || $x == hello ) ]] && echo "parentheses"
[[ -z $x && -n $x || -n $x ]] && echo "&& binds tighter than ||"
[[ a < b && b > a ]] && echo "< and > compare strings"
[[ 10 < 9 ]] && echo "string order, not numeric"
[[ 10 -lt 9 ]] || echo "-lt is numeric"
[[ 1+2 -eq 3 ]] && echo "operands of -eq are arithmetic"
n=4; [[ n*2 -ge 8 && $n -ne 5 && n -le 4 && n -gt 3 ]] && echo "-ge -ne -le -gt"
[[ abc =~ ^a.c$ ]] && echo "regex"
[[ abc =~ b ]] && echo "regex is unanchored"
[[ abc =~ "a.c" ]] || echo "quoted regex text is literal"
[[ a.c =~ "a.c" ]] && echo "... and matches itself"
re='^(ab|cd)+$'; [[ abcdab =~ $re ]] && echo "regex from variable"
[[ x =~ '(' ]] || echo "quoted ( is literal"
[[ x =~ [ ]] 2>/dev/null; echo "bad regex status=$?"
: >f; mkdir d; ln -s f lnk
[[ -f f && -d d && -h lnk && -L lnk && -e f && -a f ]] && echo "file tests"
[[ -f nope || -e nope ]] || echo "missing file"
[[ f -ef lnk ]] && echo "-ef"
set -o noglob; [[ -o noglob ]] && echo "-o option on"; set +o noglob
[[ -o noglob ]] || echo "-o option off"
[[ -o nosuchoption ]] || echo "-o unknown option is false"
[[ -v x ]] && echo "-v set"; [[ -v e ]] && echo "-v set to empty"
[[ -v nosuch ]] || echo "-v unset"
[[ -f ]] && echo "lone -f is a string"
[[ -f == -f ]] && echo "-f as an operand"
[[ * == \* ]] && echo "no globbing of operands"
[[ ~ == "$HOME" ]] && echo "tilde expansion"
[[ $(echo hi) == hi ]] && echo "command substitution"
[[ -z $e && $((1 + 1)) == 2 ]] && echo "arithmetic expansion"
c=0; [[ -n "" && $((c += 1)) ]]; echo "&& short-circuits expansions: c=$c"
[[ -n x || $((c += 1)) ]]; echo "|| short-circuits expansions: c=$c"
[[ a == a ]]; echo "true=$?"
[[ a == b ]]; echo "false=$?"
[[ 1 -eq 1 ]] && [[ 2 -eq 2 ]] && echo "chained with &&"
if [[ $x == hello ]]; then echo "in if"; fi
while [[ $n -gt 2 ]]; do n=$((n - 1)); done; echo "while: n=$n"
[[ $x == hello ]] >out && echo "redirection after ]]"
[[ a <b ]] && echo "< needs no spaces"
f() { [[ $1 == y* ]]; }; f yes && echo "in a function"; f no || echo "false in a function"
[[ $x == hello
]] && echo "newline before ]]"
[[ -n $x &&
   -z $e ]] && echo "newline after &&"
set -e; [[ a == b ]] || echo "errexit: tested"; set +e
(set -e; [[ a == b ]]; echo "not reached"); echo "errexit: status=$?"
v='[[ a == a ]] && echo "from eval"'; eval "$v"
for s in '[[' '[[ a' '[[ ]]' '[[ a == ]]' '[[ ( a ]]' '[[ a b ]]' '[[ a && ]]'; do
    $TSH -c "$s" 2>/dev/null; echo "syntax $s: $?"
done
echo [[ and ]] are words elsewhere