| 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 | |
| 32 | #### declare -i with arithmetic inside strings (Nix, issue 864) |
| 33 | |
| 34 | # example |
| 35 | # https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L379 |
| 36 | |
| 37 | declare -i s |
| 38 | s='1 + 2' |
| 39 | echo s=$s |
| 40 | |
| 41 | declare -a array=(1 2 3) |
| 42 | declare -i item |
| 43 | item='array[1+1]' |
| 44 | echo item=$item |
| 45 | |
| 46 | ## STDOUT: |
| 47 | s=3 |
| 48 | item=3 |
| 49 | ## END |
| 50 | |
| 51 | #### append in arith context |
| 52 | declare s |
| 53 | (( s='1 ')) |
| 54 | (( s+=' 2 ')) # arith add |
| 55 | declare -i i |
| 56 | (( i='1 ' )) |
| 57 | (( i+=' 2 ' )) |
| 58 | declare -i j |
| 59 | (( j='x ' )) # treated like zero |
| 60 | (( j+=' 2 ' )) |
| 61 | echo "$s|$i|$j" |
| 62 | ## stdout: 3|3|2 |
| 63 | |
| 64 | #### declare array vs. string: mixing -a +a and () '' |
| 65 | # dynamic parsing of first argument. |
| 66 | declare +a 'xyz1=1' |
| 67 | declare +a 'xyz2=(2 3)' |
| 68 | declare -a 'xyz3=4' |
| 69 | declare -a 'xyz4=(5 6)' |
| 70 | argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}" |
| 71 | ## stdout: ['1', '(2 3)', '4', '5', '6'] |
| 72 | |
| 73 | #### declare array vs. associative array |
| 74 | # Hm I don't understand why the array only has one element. I guess because |
| 75 | # index 0 is used twice? |
| 76 | declare -a 'array=([a]=b [c]=d)' |
| 77 | declare -A 'assoc=([a]=b [c]=d)' |
| 78 | argv.py "${#array[@]}" "${!array[@]}" "${array[@]}" |
| 79 | argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}" |
| 80 | ## stdout-json: "['1', '0', 'd']\n['2', 'a', 'c', 'b', 'd']\n" |