OS X终端命令来解析别名的路径 [英] OS X terminal command to resolve path of an alias

查看:68
本文介绍了OS X终端命令来解析别名的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个shell脚本,它将脚本从远程计算机,某些linux,某些macs同步文件到中央备份服务器. Mac在根级别具有文件夹,其中包含需要备份的所有文件/文件夹的别名.我可以使用什么终端命令来解析别名指向的文件/文件夹的路径? (我需要将这些路径传递给rsync)

I'm writing a shell script which will rsync files from remote machines, some linux, some macs, to a central backup server. The macs have folders on the root level containing aliases of all files/folders which need to be backed up. What is a terminal command I can use to resolve the path to the files/folders the aliases point to? (I'll need to pass these paths to rsync)

推荐答案

我发现了以下脚本可以满足我的需要:

I found the following script which does what I needed:

#!/bin/sh
if [ $# -eq 0 ]; then
  echo ""
  echo "Usage: $0 alias"
  echo "  where alias is an alias file."
  echo "  Returns the file path to the original file referenced by a"
  echo "  Mac OS X GUI alias.  Use it to execute commands on the"
  echo "  referenced file.  For example, if aliasd is an alias of"
  echo "  a directory, entering"
  echo '   % cd `apath aliasd`'
  echo "  at the command line prompt would change the working directory"
  echo "  to the original directory."
  echo ""
fi
if [ -f "$1" -a ! -L "$1" ]; then
    # Redirect stderr to dev null to suppress OSA environment errors
    exec 6>&2 # Link file descriptor 6 with stderr so we can restore stderr later
    exec 2>/dev/null # stderr replaced by /dev/null
    path=$(osascript << EOF
tell application "Finder"
set theItem to (POSIX file "${1}") as alias
if the kind of theItem is "alias" then
get the posix path of ((original item of theItem) as text)
end if
end tell
EOF
)
    exec 2>&6 6>&-      # Restore stderr and close file descriptor #6.

    echo "$path"
fi

这篇关于OS X终端命令来解析别名的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆