1 |
# |
2 |
# Tests for bash's type flags on cells. Hopefully we don't have to implement |
3 |
# this, but it's good to know the behavior. |
4 |
# |
5 |
# OSH follows a Python-ish model of types carried with values/objects, not |
6 |
# locations. |
7 |
# |
8 |
# See https://github.com/oilshell/oil/issues/26 |
9 |
|
10 |
#### declare -i with += |
11 |
declare s |
12 |
s='1 ' |
13 |
s+=' 2 ' # string append |
14 |
|
15 |
declare -i i |
16 |
i='1 ' |
17 |
i+=' 2 ' # arith add |
18 |
|
19 |
declare -i j |
20 |
j=x # treated like zero |
21 |
j+=' 2 ' # arith add |
22 |
|
23 |
echo "[$s]" |
24 |
echo [$i] |
25 |
echo [$j] |
26 |
## STDOUT: |
27 |
[1 2 ] |
28 |
[3] |
29 |
[2] |
30 |
## END |
31 |
## N-I osh STDOUT: |
32 |
[1 2 ] |
33 |
[1 2 ] |
34 |
[x 2 ] |
35 |
## END |
36 |
|
37 |
#### declare -i with arithmetic inside strings (Nix, issue 864) |
38 |
|
39 |
# example |
40 |
# https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L379 |
41 |
|
42 |
declare -i s |
43 |
s='1 + 2' |
44 |
echo s=$s |
45 |
|
46 |
declare -a array=(1 2 3) |
47 |
declare -i item |
48 |
item='array[1+1]' |
49 |
echo item=$item |
50 |
|
51 |
## STDOUT: |
52 |
s=3 |
53 |
item=3 |
54 |
## END |
55 |
## N-I osh STDOUT: |
56 |
s=1 + 2 |
57 |
item=array[1+1] |
58 |
## END |
59 |
|
60 |
#### append in arith context |
61 |
declare s |
62 |
(( s='1 ')) |
63 |
(( s+=' 2 ')) # arith add |
64 |
declare -i i |
65 |
(( i='1 ' )) |
66 |
(( i+=' 2 ' )) |
67 |
declare -i j |
68 |
(( j='x ' )) # treated like zero |
69 |
(( j+=' 2 ' )) |
70 |
echo "$s|$i|$j" |
71 |
## STDOUT: |
72 |
3|3|2 |
73 |
## END |
74 |
## N-I osh status: 1 |
75 |
## N-I osh STDOUT: |
76 |
## END |
77 |
|
78 |
#### declare array vs. string: mixing -a +a and () '' |
79 |
# dynamic parsing of first argument. |
80 |
declare +a 'xyz1=1' |
81 |
declare +a 'xyz2=(2 3)' |
82 |
declare -a 'xyz3=4' |
83 |
declare -a 'xyz4=(5 6)' |
84 |
argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}" |
85 |
## STDOUT: |
86 |
['1', '(2 3)', '4', '5', '6'] |
87 |
## END |
88 |
## N-I osh STDOUT: |
89 |
['', ''] |
90 |
## END |
91 |
|
92 |
|
93 |
#### declare array vs. associative array |
94 |
# Hm I don't understand why the array only has one element. I guess because |
95 |
# index 0 is used twice? |
96 |
declare -a 'array=([a]=b [c]=d)' |
97 |
declare -A 'assoc=([a]=b [c]=d)' |
98 |
argv.py "${#array[@]}" "${!array[@]}" "${array[@]}" |
99 |
argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}" |
100 |
## STDOUT: |
101 |
['1', '0', 'd'] |
102 |
['2', 'a', 'c', 'b', 'd'] |
103 |
## END |
104 |
## N-I osh STDOUT: |
105 |
['0'] |
106 |
['0'] |
107 |
## END |