获取给定文件或目录的绝对路径

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
 
### change relative path to absolute path
### or get absolute path of a file
### return HOME of current account if no parameter given
function get_fullpath()
{
   _PWD=`pwd`
   if [ -d $1 ]; then
      cd $1
   elif [ -f $1 ]; then
      cd `dirname $1`
   else
      cd
   fi
   echo $(cd ..; cd -)
   cd ${_PWD} >/dev/null
}
 
### test
FILEARRAY="/root/ /root / ./../../not_exist_file ../../ . .. "
for FILE in ${FILEARRAY}
do
   PATH=`get_fullpath ${FILE}`
   echo "path of <${FILE}> is <${PATH}>"
done