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