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

2015-07-03 0 718
获取给定文件或目录的绝对路径
#!/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

遇见资源网 shell 获取给定文件或目录的绝对路径 http://www.ox520.com/17041.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务