假设像这样的路径
/home/albfan/Projects/InSaNEWEBproJECT
尽管事实上不使用这样的名称。有没有办法以不敏感的方式检查路径?
我遇到了这个解决方案,但如果可能的话,我想找到一个内置或 gnu 程序。
function searchPathInsensitive {
# Replace bar with comma (not valid directory character allowing parse dirs with spaces)
#also remove first / if exist (if not this create a first empty element
ORG="$1"
if [ "${ORG:0:1}" = "/" ]
then
ORG="${ORG:1}"
else
ORG="${PWD:1}/$ORG"
fi
OLDIFS=$IF
IFS=,
for dir in ${ORG//\//,}
do
if [ -z $DIR ]
then
DIR="/$dir"
else
TMP_DIR="$DIR/$dir"
DIR=$(/usr/bin/find $DIR -maxdepth 1 -ipath $TMP_DIR -print -quit)
if [ -z $DIR ]
then
# If some of the path does not exist just copy the element
# exit 1
DIR="$TMP_DIR"
fi
fi
done
IFS=$OLDIFS
echo "$DIR"
}
使用它只需:
(searching on my home)
$ searchPathInsensitive projects/insanewebproject
/home/albfan/Projects/InSaNEWEBproJECT
(inside a project)
$ searchPathInsensitive src/main/java/org/package/webprotocolhttpwrapper.java
/home/albfan/Projects/InSaNEWEBproJECT/src/main/java/org/package/WebProtocolHTTPWrapper.java
$ searchPathInsensitive src/main/resources/logout.png
/home/albfan/Projects/InSaNEWEBproJECT/src/main/resources/LogOut.PNG
我猜该解决方案与find -ipath有任何关系,因为我对该函数所做的只是搜索以不敏感方式给出的路径中的下一个元素