canonicalize a path name on solaris

半世苍凉 提交于 2019-12-10 15:55:54

问题


On a GNU system I would just use readlink -f $SOME_PATH, but Solaris doesn't have readlink.

I'd prefer something that works well in bash, but other programs are ok if needed.

Edit: The best I've come up with so far uses cd and pwd, but requires some more hackery to deal with files and not just directories.

cd -P "$*"
REAL_PATH=`pwd`

回答1:


Does this help? From the referenced page:

Create a file called canonicalize with these contents:

#!/bin/bash
cd -P -- "$(dirname -- "$1")" &&
printf '%s\n' "$(pwd -P)/$(basename -- "$1")"

Make the file executable:

chmod +x canonicalize`

And finally:

user@host$ canonicalize ./bash_profile



回答2:


Might be overkill, but this is OS portable, and does not need to find the dirname nor basename binaries first.. this one-liner works. Just pass in your filename where you see $origFile:

perl -e "use Cwd realpath; print realpath(\"$origFile\");"




回答3:


#!/bin/bash

# Resolves a full path
# - alternative to "readlink -f", which is not available on solaris
canonicalpath() {
  if [ -d $1 ]; then
    pushd $1 > /dev/null 2>&1
    echo $PWD
  elif [ -f $1 ]; then
    pushd $(dirname $1) > /dev/null 2>&1
    echo $PWD/$(basename $1)
  else
    echo "Invalid path $1"
  fi
  popd > /dev/null 2>&1
}


来源:https://stackoverflow.com/questions/1245301/canonicalize-a-path-name-on-solaris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!