xref: /onnv-gate/usr/src/lib/libshell/common/tests/path.sh (revision 12068:08a39a083754)
14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*12068SRoger.Faulkner@Oracle.COM#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
78462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schinfunction err_exit
214887Schin{
224887Schin	print -u2 -n "\t"
234887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
244887Schin	let Errors+=1
254887Schin}
264887Schinalias err_exit='err_exit $LINENO'
2710898Sroland.mainz@nrubsig.org
284887SchinCommand=${0##*/}
294887Schininteger Errors=0
308462SApril.Chin@Sun.COM
3110898Sroland.mainz@nrubsig.orgtmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
3210898Sroland.mainz@nrubsig.orgtrap "cd /; rm -rf $tmp" EXIT
3310898Sroland.mainz@nrubsig.org
3410898Sroland.mainz@nrubsig.orgcd $tmp || exit
358462SApril.Chin@Sun.COMtype /xxxxxx > out1 2> out2
368462SApril.Chin@Sun.COM[[ -s out1 ]] && err_exit 'type should not write on stdout for not found case'
378462SApril.Chin@Sun.COM[[ -s out2 ]] || err_exit 'type should write on stderr for not found case'
388462SApril.Chin@Sun.COMmkdir dir1 dir2
398462SApril.Chin@Sun.COMcat  > dir1/foobar << '+++'
408462SApril.Chin@Sun.COMfoobar() { print foobar1;}
418462SApril.Chin@Sun.COMfunction dir1 { print dir1;}
428462SApril.Chin@Sun.COM+++
438462SApril.Chin@Sun.COMcat  > dir2/foobar << '+++'
448462SApril.Chin@Sun.COMfoobar() { print foobar2;}
458462SApril.Chin@Sun.COMfunction dir2 { print dir2;}
468462SApril.Chin@Sun.COM+++
478462SApril.Chin@Sun.COMchmod +x dir[12]/foobar
488462SApril.Chin@Sun.COMp=$PATH
498462SApril.Chin@Sun.COMFPATH=$PWD/dir1
508462SApril.Chin@Sun.COMPATH=$FPATH:$p
518462SApril.Chin@Sun.COM[[ $( foobar) == foobar1 ]] || err_exit 'foobar should output foobar1'
528462SApril.Chin@Sun.COMFPATH=$PWD/dir2
538462SApril.Chin@Sun.COMPATH=$FPATH:$p
548462SApril.Chin@Sun.COM[[ $(foobar) == foobar2 ]] || err_exit 'foobar should output foobar2'
558462SApril.Chin@Sun.COMFPATH=$PWD/dir1
568462SApril.Chin@Sun.COMPATH=$FPATH:$p
578462SApril.Chin@Sun.COM[[ $(foobar) == foobar1 ]] || err_exit 'foobar should output foobar1 again'
588462SApril.Chin@Sun.COMFPATH=$PWD/dir2
598462SApril.Chin@Sun.COMPATH=$FPATH:$p
608462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${}'
618462SApril.Chin@Sun.COM[[ ${ dir2;} == dir2 ]] || err_exit 'should be dir2'
628462SApril.Chin@Sun.COM[[ ${ dir1;} == dir1 ]] 2> /dev/null &&  err_exit 'should not be be dir1'
638462SApril.Chin@Sun.COMFPATH=$PWD/dir1
648462SApril.Chin@Sun.COMPATH=$FPATH:$p
658462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar1 ]] || err_exit 'foobar should output foobar1 with ${}'
668462SApril.Chin@Sun.COM[[ ${ dir1;} == dir1 ]] || err_exit 'should be dir1'
678462SApril.Chin@Sun.COM[[ ${ dir2;} == dir2 ]] 2> /dev/null &&  err_exit 'should not be be dir2'
688462SApril.Chin@Sun.COMFPATH=$PWD/dir2
698462SApril.Chin@Sun.COMPATH=$FPATH:$p
708462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${} again'
718462SApril.Chin@Sun.COMPATH=$p
724887Schin(PATH="/bin")
734887Schin[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
7410898Sroland.mainz@nrubsig.orgcat > bug1 <<- EOF
7510898Sroland.mainz@nrubsig.org	print print ok > $tmp/ok
7610898Sroland.mainz@nrubsig.org	/bin/chmod 755 $tmp/ok
774887Schin	function a
784887Schin	{
7910898Sroland.mainz@nrubsig.org	        typeset -x PATH=$tmp
8010898Sroland.mainz@nrubsig.org	        ok
814887Schin	}
8210898Sroland.mainz@nrubsig.org	path=\$PATH
834887Schin	unset PATH
844887Schin	a
8510898Sroland.mainz@nrubsig.org	PATH=\$path
864887Schin}
874887SchinEOF
8810898Sroland.mainz@nrubsig.org[[ $($SHELL ./bug1 2>/dev/null) == ok ]]  || err_exit "PATH in function not working"
894887Schincat > bug1 <<- \EOF
904887Schin	function lock_unlock
914887Schin	{
924887Schin	typeset PATH=/usr/bin
934887Schin	typeset -x PATH=''
944887Schin	}
9510898Sroland.mainz@nrubsig.org
964887Schin	PATH=/usr/bin
974887Schin	: $(PATH=/usr/bin getconf PATH)
984887Schin	typeset -ft lock_unlock
994887Schin	lock_unlock
1004887SchinEOF
1014887Schin($SHELL ./bug1)  2> /dev/null || err_exit "path_delete bug"
10210898Sroland.mainz@nrubsig.orgmkdir tdir
10310898Sroland.mainz@nrubsig.orgif	$SHELL tdir > /dev/null 2>&1
1044887Schinthen	err_exit 'not an error to run ksh on a directory'
1054887Schinfi
1064887Schin
1074887Schinprint 'print hi' > ls
1084887Schinif	[[ $($SHELL ls 2> /dev/null) != hi ]]
1094887Schinthen	err_exit "$SHELL name not executing version in current directory"
1104887Schinfi
1114887Schinif	[[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
1124887Schinthen	err_exit 'PATH export in command substitution not working'
1134887Schinfi
1144887Schinpwd=$PWD
1154887Schin# get rid of leading and trailing : and trailing :.
1164887SchinPATH=${PATH%.}
1174887SchinPATH=${PATH%:}
1184887SchinPATH=${PATH#.}
1194887SchinPATH=${PATH#:}
1204887Schinpath=$PATH
1214887Schinvar=$(whence date)
1224887Schindir=$(basename "$var")
1234887Schinfor i in 1 2 3 4 5 6 7 8 9 0
1244887Schindo	if	! whence notfound$i 2> /dev/null
12510898Sroland.mainz@nrubsig.org	then	cmd=notfound$i
1264887Schin		break
1274887Schin	fi
1284887Schindone
1294887Schinprint 'print hello' > date
1304887Schinchmod +x date
1314887Schinprint 'print notfound' >  $cmd
1324887Schinchmod +x "$cmd"
1334887Schin> foo
1344887Schinchmod 755 foo
1354887Schinfor PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
13610898Sroland.mainz@nrubsig.orgdo
1374887Schin#	print path=$PATH $(whence date)
1384887Schin#	print path=$PATH $(whence "$cmd")
1394887Schin		date
1404887Schin		"$cmd"
1414887Schindone > /dev/null 2>&1
1424887Schinbuiltin -d date 2> /dev/null
1434887Schinif	[[ $(PATH=:/usr/bin; date) != 'hello' ]]
1444887Schinthen	err_exit "leading : in path not working"
1454887Schinfi
1464887Schin(
1474887Schin	PATH=$PWD:
1484887Schin	builtin chmod
1494887Schin	print 'print cannot execute' > noexec
1504887Schin	chmod 644 noexec
1514887Schin	if	[[ ! -x noexec ]]
1524887Schin	then	noexec > /dev/null 2>&1
1534887Schin	else	exit 126
1544887Schin	fi
1554887Schin)
1564887Schinstatus=$?
1574887Schin[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
1584887Schinbuiltin -d rm 2> /dev/null
15910898Sroland.mainz@nrubsig.orgchmod=$(whence chmod)
1604887Schinrm=$(whence rm)
1614887Schind=$(dirname "$rm")
16210898Sroland.mainz@nrubsig.org
16310898Sroland.mainz@nrubsig.orgchmod=$(whence chmod)
16410898Sroland.mainz@nrubsig.org
16510898Sroland.mainz@nrubsig.orgfor cmd in date foo
16610898Sroland.mainz@nrubsig.orgdo	exp="$cmd found"
16710898Sroland.mainz@nrubsig.org	print print $exp > $cmd
16810898Sroland.mainz@nrubsig.org	$chmod +x $cmd
16910898Sroland.mainz@nrubsig.org	got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1)
17010898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null"
17110898Sroland.mainz@nrubsig.org	got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1)
17210898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null"
17310898Sroland.mainz@nrubsig.org	exp=$PWD/./$cmd
17410898Sroland.mainz@nrubsig.org	got=$(unset FPATH; PATH=/dev/null; whence ./$cmd)
17510898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] || err_exit "whence $cmd should find ./$cmd with PATH=/dev/null"
17610898Sroland.mainz@nrubsig.org	exp=$PWD/$cmd
17710898Sroland.mainz@nrubsig.org	got=$(unset FPATH; PATH=/dev/null; whence $PWD/$cmd)
17810898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null"
17910898Sroland.mainz@nrubsig.orgdone
18010898Sroland.mainz@nrubsig.org
18110898Sroland.mainz@nrubsig.orgexp=''
18210898Sroland.mainz@nrubsig.orggot=$($SHELL -c "unset FPATH; PATH=/dev/null; whence ./notfound" 2>&1)
18310898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'"
18410898Sroland.mainz@nrubsig.orggot=$($SHELL -c "unset FPATH; PATH=/dev/null; whence $PWD/notfound" 2>&1)
18510898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'"
18610898Sroland.mainz@nrubsig.org
1874887Schinunset FPATH
1884887SchinPATH=/dev/null
18910898Sroland.mainz@nrubsig.orgfor cmd in date foo
19010898Sroland.mainz@nrubsig.orgdo	exp="$cmd found"
19110898Sroland.mainz@nrubsig.org	print print $exp > $cmd
19210898Sroland.mainz@nrubsig.org	$chmod +x $cmd
19310898Sroland.mainz@nrubsig.org	got=$($cmd 2>&1)
19410898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null"
19510898Sroland.mainz@nrubsig.org	got=$($cmd 2>&1; :)
19610898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null"
19710898Sroland.mainz@nrubsig.org	exp=$PWD/./$cmd
19810898Sroland.mainz@nrubsig.org	got=$(whence ./$cmd)
19910898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] || err_exit "whence ./$cmd should find ./$cmd with PATH=/dev/null"
20010898Sroland.mainz@nrubsig.org	exp=$PWD/$cmd
20110898Sroland.mainz@nrubsig.org	got=$(whence $PWD/$cmd)
20210898Sroland.mainz@nrubsig.org	[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null"
20310898Sroland.mainz@nrubsig.orgdone
20410898Sroland.mainz@nrubsig.orgexp=''
20510898Sroland.mainz@nrubsig.orggot=$(whence ./notfound)
20610898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'"
20710898Sroland.mainz@nrubsig.orggot=$(whence $PWD/notfound)
20810898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'"
20910898Sroland.mainz@nrubsig.org
2104887SchinPATH=$d:
21110898Sroland.mainz@nrubsig.orgcp "$rm" kshrm
21210898Sroland.mainz@nrubsig.orgif	[[ $(whence kshrm) != $PWD/kshrm  ]]
2134887Schinthen	err_exit 'trailing : in pathname not working'
2144887Schinfi
2154887Schincp "$rm" rm
2164887SchinPATH=:$d
2174887Schinif	[[ $(whence rm) != $PWD/rm ]]
2184887Schinthen	err_exit 'leading : in pathname not working'
2194887Schinfi
2204887SchinPATH=$d: whence rm > /dev/null
2214887Schinif	[[ $(whence rm) != $PWD/rm ]]
2224887Schinthen	err_exit 'pathname not restored after scoping'
2234887Schinfi
2244887Schinmkdir bin
2254887Schinprint 'print ok' > bin/tst
2264887Schinchmod +x bin/tst
2274887Schinif	[[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
2284887Schinthen	err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
2294887Schinfi
2304887Schincd /
2314887Schinif	whence ls > /dev/null
2324887Schinthen	PATH=
2334887Schin	if	[[ $(whence rm) ]]
2344887Schin	then	err_exit 'setting PATH to Null not working'
2354887Schin	fi
2364887Schin	unset PATH
2374887Schin	if	[[ $(whence rm) != /*rm ]]
2384887Schin	then	err_exit 'unsetting path  not working'
2394887Schin	fi
2404887Schinfi
24110898Sroland.mainz@nrubsig.orgPATH=/dev:$tmp
2424887Schinx=$(whence rm)
2434887Schintypeset foo=$(PATH=/xyz:/abc :)
2444887Schiny=$(whence rm)
2454887Schin[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
2464887Schinwhence getconf > /dev/null  &&  err_exit 'getconf should not be found'
2474887Schinbuiltin /bin/getconf
2484887SchinPATH=/bin
2494887SchinPATH=$(getconf PATH)
2504887Schinx=$(whence ls)
2514887SchinPATH=.:$PWD:${x%/ls}
2524887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
2534887SchinPATH=$PWD:.:${x%/ls}
2544887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
2554887Schincd   "${x%/ls}"
2564887Schin[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
25710898Sroland.mainz@nrubsig.orgstatus=$($SHELL -c $'trap \'print $?\' EXIT;/xxx/a/b/c/d/e 2> /dev/null')
2584887Schin[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
2594887Schinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
2604887Schin[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
26110898Sroland.mainz@nrubsig.orgstatus=$($SHELL -c $'trap \'print $?\' ERR;/xxx/a/b/c/d/e 2> /dev/null')
2624887Schin[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
2634887Schinstatus=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
2644887Schin[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
2658462SApril.Chin@Sun.COM
2668462SApril.Chin@Sun.COM# universe via PATH
2678462SApril.Chin@Sun.COM
2688462SApril.Chin@Sun.COMbuiltin getconf
2698462SApril.Chin@Sun.COMgetconf UNIVERSE - att # override sticky default 'UNIVERSE = foo'
2708462SApril.Chin@Sun.COM
2718462SApril.Chin@Sun.COM[[ $(PATH=/usr/ucb/bin:/usr/bin echo -n ucb) == 'ucb' ]] || err_exit "ucb universe echo ignores -n option"
2728462SApril.Chin@Sun.COM[[ $(PATH=/usr/xpg/bin:/usr/bin echo -n att) == '-n att' ]] || err_exit "att universe echo does not ignore -n option"
2738462SApril.Chin@Sun.COM
2748462SApril.Chin@Sun.COMPATH=$path
2758462SApril.Chin@Sun.COM
27610898Sroland.mainz@nrubsig.orgscr=$tmp/script
2778462SApril.Chin@Sun.COMexp=126
2788462SApril.Chin@Sun.COM
2798462SApril.Chin@Sun.COM: > $scr
2808462SApril.Chin@Sun.COMchmod a=x $scr
2818462SApril.Chin@Sun.COM{ got=$($scr; print $?); } 2>/dev/null
2828462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "unreadable empty script should fail -- expected $exp, got $got"
2838462SApril.Chin@Sun.COM{ got=$(command $scr; print $?); } 2>/dev/null
2848462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "command of unreadable empty script should fail -- expected $exp, got $got"
2858462SApril.Chin@Sun.COM[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable empty script in [[ ... ]] should fail -- expected $exp"
2868462SApril.Chin@Sun.COM[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable empty script in [[ ... ]] should fail -- expected $exp"
2878462SApril.Chin@Sun.COMgot=$($SHELL -c "$scr; print \$?" 2>/dev/null)
2888462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable empty script should fail -- expected $exp, got" $got
2898462SApril.Chin@Sun.COMgot=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
2908462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable empty script should fail -- expected $exp, got" $got
2918462SApril.Chin@Sun.COM
2928462SApril.Chin@Sun.COMrm -f $scr
2938462SApril.Chin@Sun.COMprint : > $scr
2948462SApril.Chin@Sun.COMchmod a=x $scr
2958462SApril.Chin@Sun.COM{ got=$($scr; print $?); } 2>/dev/null
2968462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "unreadable non-empty script should fail -- expected $exp, got $got"
2978462SApril.Chin@Sun.COM{ got=$(command $scr; print $?); } 2>/dev/null
2988462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "command of unreadable non-empty script should fail -- expected $exp, got $got"
2998462SApril.Chin@Sun.COM[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
3008462SApril.Chin@Sun.COM[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
3018462SApril.Chin@Sun.COMgot=$($SHELL -c "$scr; print \$?" 2>/dev/null)
3028462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable non-empty script should fail -- expected $exp, got" $got
3038462SApril.Chin@Sun.COMgot=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
3048462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable non-empty script should fail -- expected $exp, got" $got
3058462SApril.Chin@Sun.COM
3064887Schinexit $((Errors))
307