1 #
2 # Miscellaneous tests for the command language.
3
4 #### Command block
5 { which ls; }
6 ## stdout: /bin/ls
7
8 #### Permission denied
9 touch $TMP/text-file
10 $TMP/text-file
11 ## status: 126
12
13 #### Not a dir
14 $TMP/not-a-dir/text-file
15 ## status: 127
16
17 #### Name too long
18 ./0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
19 ## status: 127
20 ## OK dash status: 2
21 ## OK bash status: 126
22
23 #### External programs don't have _OVM in environment
24 # bug fix for leakage
25 env | grep _OVM
26 echo status=$?
27 ## stdout: status=1
28
29 #### File with no shebang is executed
30 # most shells execute /bin/sh; bash may execute itself
31 echo 'echo hi' > $TMP/no-shebang
32 chmod +x $TMP/no-shebang
33 $SH -c '$TMP/no-shebang'
34 ## stdout: hi
35 ## status: 0
36
37 #### File with relative path and no shebang is executed
38 cd $TMP
39 echo 'echo hi' > no-shebang
40 chmod +x no-shebang
41 "$SH" -c './no-shebang'
42 ## stdout: hi
43 ## status: 0
44
45 #### File in relative subdirectory and no shebang is executed
46 cd $TMP
47 mkdir -p test-no-shebang
48 echo 'echo hi' > test-no-shebang/script
49 chmod +x test-no-shebang/script
50 "$SH" -c 'test-no-shebang/script'
51 ## stdout: hi
52 ## status: 0
53
54 #### $PATH lookup
55 cd $TMP
56 mkdir -p one two
57 echo 'echo one' > one/mycmd
58 echo 'echo two' > two/mycmd
59 chmod +x one/mycmd two/mycmd
60
61 PATH='one:two'
62 mycmd
63 ## STDOUT:
64 one
65 ## END
66
67 #### filling $PATH cache, then insert the same command earlier in cache
68 cd $TMP
69 PATH="one:two:$PATH"
70 mkdir -p one two
71 rm -f one/* two/*
72 echo 'echo two' > two/mycmd
73 chmod +x two/mycmd
74 mycmd
75
76 # Insert earlier in the path
77 echo 'echo one' > one/mycmd
78 chmod +x one/mycmd
79 mycmd # still runs the cached 'two'
80
81 # clear the cache
82 hash -r
83 mycmd # now it runs the new 'one'
84
85 ## STDOUT:
86 two
87 two
88 one
89 ## END
90
91 # zsh doesn't do caching!
92 ## OK zsh STDOUT:
93 two
94 one
95 one
96 ## END
97
98 #### filling $PATH cache, then deleting command
99 cd $TMP
100 PATH="one:two:$PATH"
101 mkdir -p one two
102 rm -f one/mycmd two/mycmd
103
104 echo 'echo two' > two/mycmd
105 chmod +x two/mycmd
106 mycmd
107 echo status=$?
108
109 # Insert earlier in the path
110 echo 'echo one' > one/mycmd
111 chmod +x one/mycmd
112 rm two/mycmd
113 mycmd # still runs the cached 'two'
114 echo status=$?
115
116 ## STDOUT:
117 two
118 status=0
119 status=127
120 ## END
121
122 # mksh and zsh correctly searches for the executable again!
123 ## OK zsh/mksh STDOUT:
124 two
125 status=0
126 one
127 status=0
128 ## END
129
130 #### Non-executable on $PATH
131
132 # shells differ in whether they actually execve('one/cmd') and get EPERM
133
134 cd $TMP
135 PATH="one:two:$PATH"
136 mkdir -p one two
137 rm -f one/mycmd two/mycmd
138
139 echo 'echo one' > one/mycmd
140 echo 'echo two' > two/mycmd
141
142 # only make the second one executable
143 chmod +x two/mycmd
144 mycmd
145 echo status=$?
146 ## STDOUT:
147 two
148 status=0
149 ## END
150
151 #### hash without args prints the cache
152 whoami >/dev/null
153 hash
154 echo status=$?
155 ## STDOUT:
156 /usr/bin/whoami
157 status=0
158 ## END
159
160 # bash uses a weird table. Although we could use TSV2.
161 ## OK bash stdout-json: "hits\tcommand\n 1\t/usr/bin/whoami\nstatus=0\n"
162
163 ## OK mksh/zsh STDOUT:
164 whoami=/usr/bin/whoami
165 status=0
166 ## END
167
168 #### hash with args
169 hash whoami
170 echo status=$?
171 hash | grep -o /whoami # prints it twice
172 hash _nonexistent_
173 echo status=$?
174 ## STDOUT:
175 status=0
176 /whoami
177 status=1
178 ## END
179
180 # mksh doesn't fail
181 ## BUG mksh STDOUT:
182 status=0
183 /whoami
184 status=0
185 ## END
186
187 #### hash -r doesn't allow additional args
188 hash -r whoami >/dev/null # avoid weird output with mksh
189 echo status=$?
190 ## stdout: status=1
191 ## OK osh stdout: status=2
192 ## BUG dash/bash stdout: status=0