OILS / spec / ysh-command-sub.test.sh View on Github | oilshell.org

147 lines, 75 significant
1## oils_failures_allowed: 0
2
3#### Conflict with extglob @( can be avoided with ,(
4
5shopt -s extglob
6
7[[ foo.py == @(*.sh|*.py) ]]
8echo status=$?
9
10# Synonym. This is a syntax error in bash.
11[[ foo.py == ,(*.sh|*.py) ]]
12echo status=$?
13
14## STDOUT:
15status=0
16status=0
17## END
18
19#### split command sub @() in expression mode
20shopt --set parse_proc parse_at
21
22var x = @(seq 3)
23
24write -- @x
25## STDOUT:
261
272
283
29## END
30
31#### split command sub @() in command mode
32
33shopt -s parse_at
34write -- @(seq 3)
35
36echo --
37IFS=x
38x=axbxxc
39argv.py $x
40
41# This construct behaves the same with simple_word_eval on
42# Not affected by IFS
43echo --
44shopt -s simple_word_eval
45write -- @(seq 3)
46
47echo --
48argv.py @(echo $x)
49
50## STDOUT:
511
522
533
54--
55['a', 'b', '', 'c']
56--
571
582
593
60--
61['axbxxc']
62## END
63
64#### @() decodes J8 Lines
65
66# syntax errors - TODO: document this in doc/chap-errors
67
68# - syntax error in J8 string (bad escape, no closing quote)
69# - extra text after line
70# - unquoted line isn't valid UTF-8
71
72var b = @(
73 # unquoted: spaces stripped at beginning and end
74 echo ' unquoted "" word '
75
76 # empty line is ignored
77 echo
78
79 # Not allowed, since unquoted lines should be UTF-8
80 #echo $'binary \xff';
81
82 echo ' " json \u03bc " '
83 echo ' j" json j prefix \u03bc " '
84 echo " u' j8 u \\u{3bc} ' "
85 echo " b' j8 b \\u{3bc} ' "
86
87 # no prefix is like u''
88 echo " ' j8 no prefix \\u{3bc} ' "
89)
90
91json write (b)
92
93## STDOUT:
94[
95 "unquoted \"\" word",
96 " json μ ",
97 " json j prefix μ ",
98 " j8 u μ ",
99 " j8 b μ ",
100 " j8 no prefix μ "
101]
102## END
103
104#### for loop using @(seq $n)
105shopt -s oil:upgrade
106
107for x in @(seq 3) {
108 echo "[$x]"
109}
110for x in @(seq 3) {
111 argv.py z @[glob("[$x]")]
112}
113## STDOUT:
114[1]
115[2]
116[3]
117['z']
118['z']
119['z']
120## END
121
122#### @() can't start in the middle of the word
123shopt -s extglob # this makes it match in bash
124shopt -s oil:upgrade
125
126# This is currently a RUNTIME error when simple_word_eval is on
127
128touch foo.py
129echo f@(*.py)
130## status: 1
131## STDOUT:
132## END
133## OK bash status: 0
134## OK bash STDOUT:
135foo.py
136## END
137
138#### @() can't have any tokens after it
139shopt -s oil:upgrade
140
141write -- @(seq 2)
142write -- @(seq 3)x
143## status: 2
144## STDOUT:
1451
1462
147## END