#便会构建参数字符串'${'"$envvar"'}',也是shell展开为'${MYPATH}'的等同物。两边
#的单引号是为了避免它更进一步展开,该字串传给eval,它会将其视为两个参数:
#echo与${MYPATH}。eval在环境下寻找MYPATH,假设找到就执行展开命令,并输
#出,通过管道传给tr命令将冒号转换为空格,最后将转化值给dirpath,错误信心隐藏
#输给/dev/null
dirpath=`eval echo '${'"$envvar"'}' 2>/dev/null | tr : ' ' `
#为错误情况进行健全检测
if test -z "$envvar"
then
error Environment variable missing or empty
elif test "x$dirpath" = "x$envvar"
then
error "Broken sh on this platform: cannot expand $envvar"
elif test -z "$dirpath"
then
error Empty directory search path
elif test $# -eq 0
then
exit 0
fi
#接下来三重循环,外层处理参数文件或模式,中层循环处理查找路径下的目录,内
#层循环匹配单一目录下的文件。
for pattern in "$@"
do
result=
for dir in $dirpath
do
for file in $dir/$pattern
do
if test -f "$file"
then
result="$file"
echo $result
test "$all" = "no" && break 2
fi
done
done
test -z "$result" && warning "$pattern: not found"
done
#限制退出状态是一般linux实现上的限制
test $EXITCODE -gt 125 && EXITCODE=125
exit $EXITCODE
这里作者给留了课后作业:增添一个功能,不单单只能匹配文件,也能匹配其他东西比如:符号性连接文件,可读取文件或者可执行文件之类的,需要test -x选项来进行匹配,本人完成的如下:
#变量初始化的地方增添一个test选项变量,默认为f
testopt=f
#选项解析的位置增添test选项并检测合法性
--test | --tes | --te | --t | -test | -tes | -te | -t )
echo $2
if echo $2 | grep -e "^[bcdefgGhkLOPrSstuwx]{1}$"
then
testopt=$2
shift
else
error "Unrecognized --test option: $2"










