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