xref: /titanic_51/usr/src/lib/libshell/common/tests/path.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin########################################################################
2da2e3ebdSchin#                                                                      #
3da2e3ebdSchin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5da2e3ebdSchin#                      and is licensed under the                       #
6da2e3ebdSchin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
8da2e3ebdSchin#                                                                      #
9da2e3ebdSchin#                A copy of the License is available at                 #
10da2e3ebdSchin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11da2e3ebdSchin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12da2e3ebdSchin#                                                                      #
13da2e3ebdSchin#              Information and Software Systems Research               #
14da2e3ebdSchin#                            AT&T Research                             #
15da2e3ebdSchin#                           Florham Park NJ                            #
16da2e3ebdSchin#                                                                      #
17da2e3ebdSchin#                  David Korn <dgk@research.att.com>                   #
18da2e3ebdSchin#                                                                      #
19da2e3ebdSchin########################################################################
20da2e3ebdSchinfunction err_exit
21da2e3ebdSchin{
22da2e3ebdSchin	print -u2 -n "\t"
23da2e3ebdSchin	print -u2 -r ${Command}[$1]: "${@:2}"
24da2e3ebdSchin	let Errors+=1
25da2e3ebdSchin}
26da2e3ebdSchinalias err_exit='err_exit $LINENO'
2734f9b3eeSRoland Mainz
28da2e3ebdSchinCommand=${0##*/}
29da2e3ebdSchininteger Errors=0
307c2fbfb3SApril Chin
3134f9b3eeSRoland Mainztmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
3234f9b3eeSRoland Mainztrap "cd /; rm -rf $tmp" EXIT
3334f9b3eeSRoland Mainz
3434f9b3eeSRoland Mainzcd $tmp || exit
357c2fbfb3SApril Chintype /xxxxxx > out1 2> out2
367c2fbfb3SApril Chin[[ -s out1 ]] && err_exit 'type should not write on stdout for not found case'
377c2fbfb3SApril Chin[[ -s out2 ]] || err_exit 'type should write on stderr for not found case'
387c2fbfb3SApril Chinmkdir dir1 dir2
397c2fbfb3SApril Chincat  > dir1/foobar << '+++'
407c2fbfb3SApril Chinfoobar() { print foobar1;}
417c2fbfb3SApril Chinfunction dir1 { print dir1;}
427c2fbfb3SApril Chin+++
437c2fbfb3SApril Chincat  > dir2/foobar << '+++'
447c2fbfb3SApril Chinfoobar() { print foobar2;}
457c2fbfb3SApril Chinfunction dir2 { print dir2;}
467c2fbfb3SApril Chin+++
477c2fbfb3SApril Chinchmod +x dir[12]/foobar
487c2fbfb3SApril Chinp=$PATH
497c2fbfb3SApril ChinFPATH=$PWD/dir1
507c2fbfb3SApril ChinPATH=$FPATH:$p
517c2fbfb3SApril Chin[[ $( foobar) == foobar1 ]] || err_exit 'foobar should output foobar1'
527c2fbfb3SApril ChinFPATH=$PWD/dir2
537c2fbfb3SApril ChinPATH=$FPATH:$p
547c2fbfb3SApril Chin[[ $(foobar) == foobar2 ]] || err_exit 'foobar should output foobar2'
557c2fbfb3SApril ChinFPATH=$PWD/dir1
567c2fbfb3SApril ChinPATH=$FPATH:$p
577c2fbfb3SApril Chin[[ $(foobar) == foobar1 ]] || err_exit 'foobar should output foobar1 again'
587c2fbfb3SApril ChinFPATH=$PWD/dir2
597c2fbfb3SApril ChinPATH=$FPATH:$p
607c2fbfb3SApril Chin[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${}'
617c2fbfb3SApril Chin[[ ${ dir2;} == dir2 ]] || err_exit 'should be dir2'
627c2fbfb3SApril Chin[[ ${ dir1;} == dir1 ]] 2> /dev/null &&  err_exit 'should not be be dir1'
637c2fbfb3SApril ChinFPATH=$PWD/dir1
647c2fbfb3SApril ChinPATH=$FPATH:$p
657c2fbfb3SApril Chin[[ ${ foobar;} == foobar1 ]] || err_exit 'foobar should output foobar1 with ${}'
667c2fbfb3SApril Chin[[ ${ dir1;} == dir1 ]] || err_exit 'should be dir1'
677c2fbfb3SApril Chin[[ ${ dir2;} == dir2 ]] 2> /dev/null &&  err_exit 'should not be be dir2'
687c2fbfb3SApril ChinFPATH=$PWD/dir2
697c2fbfb3SApril ChinPATH=$FPATH:$p
707c2fbfb3SApril Chin[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${} again'
717c2fbfb3SApril ChinPATH=$p
72da2e3ebdSchin(PATH="/bin")
73da2e3ebdSchin[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
7434f9b3eeSRoland Mainzcat > bug1 <<- EOF
7534f9b3eeSRoland Mainz	print print ok > $tmp/ok
7634f9b3eeSRoland Mainz	/bin/chmod 755 $tmp/ok
77da2e3ebdSchin	function a
78da2e3ebdSchin	{
7934f9b3eeSRoland Mainz	        typeset -x PATH=$tmp
8034f9b3eeSRoland Mainz	        ok
81da2e3ebdSchin	}
8234f9b3eeSRoland Mainz	path=\$PATH
83da2e3ebdSchin	unset PATH
84da2e3ebdSchin	a
8534f9b3eeSRoland Mainz	PATH=\$path
86da2e3ebdSchin}
87da2e3ebdSchinEOF
88da2e3ebdSchin[[ $($SHELL ./bug1 2>/dev/null) == ok ]]  || err_exit "PATH in function not working"
89da2e3ebdSchincat > bug1 <<- \EOF
90da2e3ebdSchin	function lock_unlock
91da2e3ebdSchin	{
92da2e3ebdSchin	typeset PATH=/usr/bin
93da2e3ebdSchin	typeset -x PATH=''
94da2e3ebdSchin	}
95da2e3ebdSchin
96da2e3ebdSchin	PATH=/usr/bin
97da2e3ebdSchin	: $(PATH=/usr/bin getconf PATH)
98da2e3ebdSchin	typeset -ft lock_unlock
99da2e3ebdSchin	lock_unlock
100da2e3ebdSchinEOF
101da2e3ebdSchin($SHELL ./bug1)  2> /dev/null || err_exit "path_delete bug"
10234f9b3eeSRoland Mainzmkdir tdir
10334f9b3eeSRoland Mainzif	$SHELL tdir > /dev/null 2>&1
104da2e3ebdSchinthen	err_exit 'not an error to run ksh on a directory'
105da2e3ebdSchinfi
106da2e3ebdSchin
107da2e3ebdSchinprint 'print hi' > ls
108da2e3ebdSchinif	[[ $($SHELL ls 2> /dev/null) != hi ]]
109da2e3ebdSchinthen	err_exit "$SHELL name not executing version in current directory"
110da2e3ebdSchinfi
111da2e3ebdSchinif	[[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
112da2e3ebdSchinthen	err_exit 'PATH export in command substitution not working'
113da2e3ebdSchinfi
114da2e3ebdSchinpwd=$PWD
115da2e3ebdSchin# get rid of leading and trailing : and trailing :.
116da2e3ebdSchinPATH=${PATH%.}
117da2e3ebdSchinPATH=${PATH%:}
118da2e3ebdSchinPATH=${PATH#.}
119da2e3ebdSchinPATH=${PATH#:}
120da2e3ebdSchinpath=$PATH
121da2e3ebdSchinvar=$(whence date)
122da2e3ebdSchindir=$(basename "$var")
123da2e3ebdSchinfor i in 1 2 3 4 5 6 7 8 9 0
124da2e3ebdSchindo	if	! whence notfound$i 2> /dev/null
125da2e3ebdSchin	then	cmd=notfound$i
126da2e3ebdSchin		break
127da2e3ebdSchin	fi
128da2e3ebdSchindone
129da2e3ebdSchinprint 'print hello' > date
130da2e3ebdSchinchmod +x date
131da2e3ebdSchinprint 'print notfound' >  $cmd
132da2e3ebdSchinchmod +x "$cmd"
133da2e3ebdSchin> foo
134da2e3ebdSchinchmod 755 foo
135da2e3ebdSchinfor PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
136da2e3ebdSchindo
137da2e3ebdSchin#	print path=$PATH $(whence date)
138da2e3ebdSchin#	print path=$PATH $(whence "$cmd")
139da2e3ebdSchin		date
140da2e3ebdSchin		"$cmd"
141da2e3ebdSchindone > /dev/null 2>&1
142da2e3ebdSchinbuiltin -d date 2> /dev/null
143da2e3ebdSchinif	[[ $(PATH=:/usr/bin; date) != 'hello' ]]
144da2e3ebdSchinthen	err_exit "leading : in path not working"
145da2e3ebdSchinfi
146da2e3ebdSchin(
147da2e3ebdSchin	PATH=$PWD:
148da2e3ebdSchin	builtin chmod
149da2e3ebdSchin	print 'print cannot execute' > noexec
150da2e3ebdSchin	chmod 644 noexec
151da2e3ebdSchin	if	[[ ! -x noexec ]]
152da2e3ebdSchin	then	noexec > /dev/null 2>&1
153da2e3ebdSchin	else	exit 126
154da2e3ebdSchin	fi
155da2e3ebdSchin)
156da2e3ebdSchinstatus=$?
157da2e3ebdSchin[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
158da2e3ebdSchinbuiltin -d rm 2> /dev/null
15934f9b3eeSRoland Mainzchmod=$(whence chmod)
160da2e3ebdSchinrm=$(whence rm)
161da2e3ebdSchind=$(dirname "$rm")
16234f9b3eeSRoland Mainz
16334f9b3eeSRoland Mainzchmod=$(whence chmod)
16434f9b3eeSRoland Mainz
16534f9b3eeSRoland Mainzfor cmd in date foo
16634f9b3eeSRoland Mainzdo	exp="$cmd found"
16734f9b3eeSRoland Mainz	print print $exp > $cmd
16834f9b3eeSRoland Mainz	$chmod +x $cmd
16934f9b3eeSRoland Mainz	got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1)
17034f9b3eeSRoland Mainz	[[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null"
17134f9b3eeSRoland Mainz	got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1)
17234f9b3eeSRoland Mainz	[[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null"
17334f9b3eeSRoland Mainz	exp=$PWD/./$cmd
17434f9b3eeSRoland Mainz	got=$(unset FPATH; PATH=/dev/null; whence ./$cmd)
17534f9b3eeSRoland Mainz	[[ $got == $exp ]] || err_exit "whence $cmd should find ./$cmd with PATH=/dev/null"
17634f9b3eeSRoland Mainz	exp=$PWD/$cmd
17734f9b3eeSRoland Mainz	got=$(unset FPATH; PATH=/dev/null; whence $PWD/$cmd)
17834f9b3eeSRoland Mainz	[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null"
17934f9b3eeSRoland Mainzdone
18034f9b3eeSRoland Mainz
18134f9b3eeSRoland Mainzexp=''
18234f9b3eeSRoland Mainzgot=$($SHELL -c "unset FPATH; PATH=/dev/null; whence ./notfound" 2>&1)
18334f9b3eeSRoland Mainz[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'"
18434f9b3eeSRoland Mainzgot=$($SHELL -c "unset FPATH; PATH=/dev/null; whence $PWD/notfound" 2>&1)
18534f9b3eeSRoland Mainz[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'"
18634f9b3eeSRoland Mainz
187da2e3ebdSchinunset FPATH
188da2e3ebdSchinPATH=/dev/null
18934f9b3eeSRoland Mainzfor cmd in date foo
19034f9b3eeSRoland Mainzdo	exp="$cmd found"
19134f9b3eeSRoland Mainz	print print $exp > $cmd
19234f9b3eeSRoland Mainz	$chmod +x $cmd
19334f9b3eeSRoland Mainz	got=$($cmd 2>&1)
19434f9b3eeSRoland Mainz	[[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null"
19534f9b3eeSRoland Mainz	got=$($cmd 2>&1; :)
19634f9b3eeSRoland Mainz	[[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null"
19734f9b3eeSRoland Mainz	exp=$PWD/./$cmd
19834f9b3eeSRoland Mainz	got=$(whence ./$cmd)
19934f9b3eeSRoland Mainz	[[ $got == $exp ]] || err_exit "whence ./$cmd should find ./$cmd with PATH=/dev/null"
20034f9b3eeSRoland Mainz	exp=$PWD/$cmd
20134f9b3eeSRoland Mainz	got=$(whence $PWD/$cmd)
20234f9b3eeSRoland Mainz	[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null"
20334f9b3eeSRoland Mainzdone
20434f9b3eeSRoland Mainzexp=''
20534f9b3eeSRoland Mainzgot=$(whence ./notfound)
20634f9b3eeSRoland Mainz[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'"
20734f9b3eeSRoland Mainzgot=$(whence $PWD/notfound)
20834f9b3eeSRoland Mainz[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'"
20934f9b3eeSRoland Mainz
210da2e3ebdSchinPATH=$d:
21134f9b3eeSRoland Mainzcp "$rm" kshrm
21234f9b3eeSRoland Mainzif	[[ $(whence kshrm) != $PWD/kshrm  ]]
213da2e3ebdSchinthen	err_exit 'trailing : in pathname not working'
214da2e3ebdSchinfi
215da2e3ebdSchincp "$rm" rm
216da2e3ebdSchinPATH=:$d
217da2e3ebdSchinif	[[ $(whence rm) != $PWD/rm ]]
218da2e3ebdSchinthen	err_exit 'leading : in pathname not working'
219da2e3ebdSchinfi
220da2e3ebdSchinPATH=$d: whence rm > /dev/null
221da2e3ebdSchinif	[[ $(whence rm) != $PWD/rm ]]
222da2e3ebdSchinthen	err_exit 'pathname not restored after scoping'
223da2e3ebdSchinfi
224da2e3ebdSchinmkdir bin
225da2e3ebdSchinprint 'print ok' > bin/tst
226da2e3ebdSchinchmod +x bin/tst
227da2e3ebdSchinif	[[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
228da2e3ebdSchinthen	err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
229da2e3ebdSchinfi
230da2e3ebdSchincd /
231da2e3ebdSchinif	whence ls > /dev/null
232da2e3ebdSchinthen	PATH=
233da2e3ebdSchin	if	[[ $(whence rm) ]]
234da2e3ebdSchin	then	err_exit 'setting PATH to Null not working'
235da2e3ebdSchin	fi
236da2e3ebdSchin	unset PATH
237da2e3ebdSchin	if	[[ $(whence rm) != /*rm ]]
238da2e3ebdSchin	then	err_exit 'unsetting path  not working'
239da2e3ebdSchin	fi
240da2e3ebdSchinfi
24134f9b3eeSRoland MainzPATH=/dev:$tmp
242da2e3ebdSchinx=$(whence rm)
243da2e3ebdSchintypeset foo=$(PATH=/xyz:/abc :)
244da2e3ebdSchiny=$(whence rm)
245da2e3ebdSchin[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
246da2e3ebdSchinwhence getconf > /dev/null  &&  err_exit 'getconf should not be found'
247da2e3ebdSchinbuiltin /bin/getconf
248da2e3ebdSchinPATH=/bin
249da2e3ebdSchinPATH=$(getconf PATH)
250da2e3ebdSchinx=$(whence ls)
251da2e3ebdSchinPATH=.:$PWD:${x%/ls}
252da2e3ebdSchin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
253da2e3ebdSchinPATH=$PWD:.:${x%/ls}
254da2e3ebdSchin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
255da2e3ebdSchincd   "${x%/ls}"
256da2e3ebdSchin[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
25734f9b3eeSRoland Mainzstatus=$($SHELL -c $'trap \'print $?\' EXIT;/xxx/a/b/c/d/e 2> /dev/null')
258da2e3ebdSchin[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
259da2e3ebdSchinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
260da2e3ebdSchin[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
26134f9b3eeSRoland Mainzstatus=$($SHELL -c $'trap \'print $?\' ERR;/xxx/a/b/c/d/e 2> /dev/null')
262da2e3ebdSchin[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
263da2e3ebdSchinstatus=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
264da2e3ebdSchin[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
2657c2fbfb3SApril Chin
2667c2fbfb3SApril Chin# universe via PATH
2677c2fbfb3SApril Chin
2687c2fbfb3SApril Chinbuiltin getconf
2697c2fbfb3SApril Chingetconf UNIVERSE - att # override sticky default 'UNIVERSE = foo'
2707c2fbfb3SApril Chin
2717c2fbfb3SApril Chin[[ $(PATH=/usr/ucb/bin:/usr/bin echo -n ucb) == 'ucb' ]] || err_exit "ucb universe echo ignores -n option"
2727c2fbfb3SApril Chin[[ $(PATH=/usr/xpg/bin:/usr/bin echo -n att) == '-n att' ]] || err_exit "att universe echo does not ignore -n option"
2737c2fbfb3SApril Chin
2747c2fbfb3SApril ChinPATH=$path
2757c2fbfb3SApril Chin
27634f9b3eeSRoland Mainzscr=$tmp/script
2777c2fbfb3SApril Chinexp=126
2787c2fbfb3SApril Chin
2797c2fbfb3SApril Chin: > $scr
2807c2fbfb3SApril Chinchmod a=x $scr
2817c2fbfb3SApril Chin{ got=$($scr; print $?); } 2>/dev/null
2827c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "unreadable empty script should fail -- expected $exp, got $got"
2837c2fbfb3SApril Chin{ got=$(command $scr; print $?); } 2>/dev/null
2847c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "command of unreadable empty script should fail -- expected $exp, got $got"
2857c2fbfb3SApril Chin[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable empty script in [[ ... ]] should fail -- expected $exp"
2867c2fbfb3SApril Chin[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable empty script in [[ ... ]] should fail -- expected $exp"
2877c2fbfb3SApril Chingot=$($SHELL -c "$scr; print \$?" 2>/dev/null)
2887c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable empty script should fail -- expected $exp, got" $got
2897c2fbfb3SApril Chingot=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
2907c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable empty script should fail -- expected $exp, got" $got
2917c2fbfb3SApril Chin
2927c2fbfb3SApril Chinrm -f $scr
2937c2fbfb3SApril Chinprint : > $scr
2947c2fbfb3SApril Chinchmod a=x $scr
2957c2fbfb3SApril Chin{ got=$($scr; print $?); } 2>/dev/null
2967c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "unreadable non-empty script should fail -- expected $exp, got $got"
2977c2fbfb3SApril Chin{ got=$(command $scr; print $?); } 2>/dev/null
2987c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "command of unreadable non-empty script should fail -- expected $exp, got $got"
2997c2fbfb3SApril Chin[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
3007c2fbfb3SApril Chin[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
3017c2fbfb3SApril Chingot=$($SHELL -c "$scr; print \$?" 2>/dev/null)
3027c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable non-empty script should fail -- expected $exp, got" $got
3037c2fbfb3SApril Chingot=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
3047c2fbfb3SApril Chin[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable non-empty script should fail -- expected $exp, got" $got
3057c2fbfb3SApril Chin
306da2e3ebdSchinexit $((Errors))
307