问题
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