OILS / spec / blog1.test.sh View on Github | oilshell.org

95 lines, 34 significant
1#
2# Tests for the blog.
3#
4# Fun game: try to come up with an expression that behaves differently on ALL
5# FOUR shells.
6
7#### ${##}
8set -- $(seq 25)
9echo ${##}
10## stdout: 2
11
12#### ${###}
13set -- $(seq 25)
14echo ${###}
15## stdout: 25
16## N-I osh stdout-json: ""
17## N-I osh status: 2
18
19#### ${####}
20set -- $(seq 25)
21echo ${####}
22## stdout: 25
23## N-I osh stdout-json: ""
24## N-I osh status: 2
25
26#### ${##2}
27set -- $(seq 25)
28echo ${##2}
29## stdout: 5
30## N-I osh stdout-json: ""
31## N-I osh status: 2
32
33#### ${###2}
34set -- $(seq 25)
35echo ${###2}
36## stdout: 5
37## BUG mksh stdout: 25
38## N-I osh stdout-json: ""
39## N-I osh status: 2
40
41#### ${1####}
42set -- '####'
43echo ${1####}
44## stdout: ##
45
46#### ${1#'###'}
47set -- '####'
48echo ${1#'###'}
49## stdout: #
50
51#### ${#1#'###'}
52set -- '####'
53echo ${#1#'###'}
54# dash and zsh accept; mksh/bash/osh don't.
55## status: 2
56## stdout-json: ""
57## OK dash/zsh status: 0
58## OK dash stdout: 4
59## OK zsh stdout: 1
60## N-I bash/mksh status: 1
61
62#### Julia example from spec/oil-user-feedback
63
64case $SH in (dash|mksh|zsh) exit ;; esac
65
66git-branch-merged() {
67 cat <<EOF
68 foo
69* bar
70 baz
71 master
72EOF
73}
74
75shopt -s lastpipe # required for bash, not OSH
76
77branches=() # dangerous when set -e is on
78git-branch-merged | while read -r line; do
79 line=${line# *} # strip leading spaces
80 if [[ $line != 'master' && ! ${line:0:1} == '*' ]]; then
81 branches+=("$line")
82 fi
83done
84
85if [[ ${#branches[@]} -eq 0 ]]; then
86 echo "No merged branches"
87else
88 echo git branch -D "${branches[@]}"
89fi
90
91## STDOUT:
92git branch -D foo baz
93## END
94## N-I dash/mksh/zsh STDOUT:
95## END