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

59 lines, 32 significant
1# Demonstrations for users. Could go in docs.
2
3#### Iterate over command sub output with split()
4shopt -s oil:upgrade
5
6output=$(echo '1 one'; echo '2 two')
7
8for x in @[split(output)]; do
9 write -- $x
10done
11
12echo ___
13
14# Now change IFS. split() is affected.
15IFS=$'\n'
16for x in @[split(output)]; do
17 write -- $x
18done
19
20## STDOUT:
211
22one
232
24two
25___
261 one
272 two
28## END
29
30#### split with explicit IFS argument
31shopt -s ysh:upgrade
32
33# demonstrate that -- is not special to 'write'
34output=$(echo '1 one'; echo --; echo '2 two')
35
36# TODO: accept named arg IFS=
37for x in @[split(output, $'\n')]; do
38 write -- $x
39done
40
41## STDOUT:
421 one
43--
442 two
45## END
46
47#### split on \0 delimiters
48shopt -s ysh:upgrade
49
50output=$(echo $'1 one\x002 two\x00')
51
52for x in @[split(output, $'\0')]; do
53 write -- $x
54done
55
56## STDOUT:
571 one
582 two
59## END