Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 用于解析别名路径的OS X终端命令_Macos_Shell_Path_Terminal - Fatal编程技术网

Macos 用于解析别名路径的OS X终端命令

Macos 用于解析别名路径的OS X终端命令,macos,shell,path,terminal,Macos,Shell,Path,Terminal,我正在编写一个shell脚本,它将远程机器、一些linux、一些Mac的文件rsync到一个中央备份服务器。Mac具有根级别的文件夹,其中包含需要备份的所有文件/文件夹的别名。我可以使用什么终端命令来解析别名指向的文件/文件夹的路径?(我需要将这些路径传递给rsync)我找到了以下脚本,它完成了我需要的任务: #!/bin/sh if [ $# -eq 0 ]; then echo "" echo "Usage: $0 alias" echo " where alias is an

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

我找到了以下脚本,它完成了我需要的任务:

#!/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
#/垃圾箱/垃圾箱
如果[$#-eq 0];然后
回声“”
echo“用法:$0别名”
echo“其中alias是一个别名文件。”
echo“将文件路径返回到由引用的原始文件”
echo“Mac OS X GUI别名。使用它在计算机上执行命令”
echo“引用的文件。例如,如果别名d是的别名”
回显“目录,输入”
回显“%cd`apath别名d`”
echo“在命令行提示符下将更改工作目录”
回显“到原始目录”
回声“”
fi
如果[-f“$1”-a!-L“$1”];然后
#将stderr重定向到dev null以抑制OSA环境错误
exec 6>&2#将文件描述符6与stderr链接,以便稍后恢复stderr
exec 2>/dev/null#stderr替换为/dev/null
path=$(osascript&6>&-#恢复标准并关闭文件描述符#6。
回显“$path”
fi
我找到了


一小段编译代码,你的
.bash_profile
中的一个函数,等等。透明地处理别名,只需使用“cd”。速度也比使用Applescript快好几倍。

我遇到了这个问题,所以我实现了一个命令行工具。它在

关键是,与我找到的任何AppleScript解决方案不同,即使别名已损坏,它也能工作。因此,当大量文件更改卷时,您可以使用它编写脚本来修复别名。这就是我编写它的原因

源代码非常简短,但这里是关键部分的摘要,供需要在代码中解决此问题的其他人使用,或者希望查找相关协议的人使用

NSString *aliasPath = [NSString stringWithUTF8String:posixPathToAlias];
NSURL *aliasURL = [NSURL fileURLWithPath:aliasPath];
NSError *error;
NSData *bookmarkData = [NSURL bookmarkDataWithContentsOfURL:aliasURL error:&error];
NSDictionary *values = [NSURL resourceValuesForKeys:@[NSURLPathKey]
                                   fromBookmarkData:bookmarkData];
NSString *path = [values objectForKey:NSURLPathKey];
const char *s = [path UTF8String];

只要在脚本运行时可以访问原始项,这就可以了。如果别名被破坏或卷无法装入,这就不起作用。我在另一个答案中添加了一个工具的链接,该工具可以在这些情况下工作。为什么要复制和还原现有的fd 2?为什么不直接说
path=$(osascript 2>/dev/null@dubiousjim这会更干净。我没有编写此脚本,而是在网上找到的。尽管如此,我将测试您的建议并更新answer@Josh,如果脚本不是您自己的作品,那么您至少应该包含一个指向找到它的位置的链接。链接的有用部分(此工具):
getTrueName.c
不再可用。@danielAzuelos谢谢你让我知道!我在作者那里发了推特,链接已经修复。