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