1 |
# Oil Blocks |
2 |
|
3 |
|
4 |
#### cd with block |
5 |
shopt -s oil:all |
6 |
# OLDPWD is NOT defined |
7 |
cd / { echo $PWD; echo OLDPWD=${OLDPWD:-} }; echo done |
8 |
echo $(basename $PWD) # restored |
9 |
cd /tmp { |
10 |
write PWD=$PWD |
11 |
write -sep ' ' pwd builtin: $(pwd) |
12 |
} |
13 |
echo $(basename $PWD) # restored |
14 |
## STDOUT: |
15 |
/ |
16 |
OLDPWD= |
17 |
done |
18 |
oil-blocks.test.sh |
19 |
PWD=/tmp |
20 |
pwd builtin: /tmp |
21 |
oil-blocks.test.sh |
22 |
## END |
23 |
|
24 |
#### cd with block: fatal error in block |
25 |
shopt -s oil:all |
26 |
cd / { |
27 |
echo one |
28 |
false |
29 |
echo two |
30 |
} |
31 |
## status: 1 |
32 |
## STDOUT: |
33 |
one |
34 |
## END |
35 |
|
36 |
|
37 |
#### cd with block: return in block |
38 |
shopt -s oil:all |
39 |
f() { |
40 |
cd / { |
41 |
echo one |
42 |
return |
43 |
echo two |
44 |
} |
45 |
echo 'end func' |
46 |
} |
47 |
f |
48 |
## STDOUT: |
49 |
one |
50 |
end func |
51 |
## END |
52 |
|
53 |
#### cd with block: break in block |
54 |
shopt -s oil:all |
55 |
f() { |
56 |
cd / { |
57 |
echo one |
58 |
for i in 1 2; do |
59 |
echo $i |
60 |
break # break out of loop |
61 |
done |
62 |
|
63 |
break # break out of block isn't valid |
64 |
echo two |
65 |
} |
66 |
echo end func |
67 |
} |
68 |
f |
69 |
## status: 1 |
70 |
## STDOUT: |
71 |
one |
72 |
1 |
73 |
## END |
74 |
|
75 |
#### cd with block exits with status 0 |
76 |
shopt -s oil:all |
77 |
cd / { |
78 |
echo block |
79 |
|
80 |
# This return value is ignored. |
81 |
# Or maybe this should be a runtime error? |
82 |
return 1 |
83 |
} |
84 |
echo status=$? |
85 |
## STDOUT: |
86 |
block |
87 |
status=0 |
88 |
## END |
89 |
|
90 |
#### block has its own scope, e.g. shadows outer vars |
91 |
shopt -s oil:all |
92 |
var x = 1 |
93 |
cd / { |
94 |
#set y = 5 # This would be an error because set doesn't do dynamic lookup |
95 |
var x = 42 |
96 |
echo "inner x = $x" |
97 |
} |
98 |
echo "outer x = $x" |
99 |
## STDOUT: |
100 |
inner x = 42 |
101 |
outer x = 1 |
102 |
## END |
103 |
|
104 |
|