规范化 solaris 上的路径名

发布于 2024-07-30 00:04:02 字数 248 浏览 6 评论 0原文

在 GNU 系统上我只会使用 readlink -f $SOME_PATH ,但 Solaris 没有 readlink 。

我更喜欢在 bash 中运行良好的程序,但如果需要的话其他程序也可以。

编辑:到目前为止,我想到的最好的方法是使用 cd 和 pwd,但需要更多的技巧来处理文件而不仅仅是目录。

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

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`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲欢浪云 2024-08-06 00:04:02

可能有点矫枉过正,但这是操作系统可移植的,不需要先找到目录名或基名二进制文件..这个单行有效。 只需在您看到 $origFile 的位置传递文件名即可:

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

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\");"

徒留西风 2024-08-06 00:04:02
#!/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
}
#!/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
}
堇年纸鸢 2024-08-06 00:04:02

有帮助吗? 从引用的页面:

创建一个名为 canonicalize 的文件,其中包含以下内容:

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

使文件可执行:

chmod +x canonicalize`

最后:

user@host$ canonicalize ./bash_profile

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文