用户登录
用户注册

分享至

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

  • 作者: 取用户名就要有内涵
  • 来源: 51数据库
  • 2022-08-17
#!/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

软件
前端设计
程序设计
Java相关