bash OSX:绝对桌面文件路径?(包括/卷)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26634800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:40:20  来源:igfitidea点击:

OSX : absolute desktop file path ? (including /Volumes)

macosbashapplescriptfilepathautomator

提问by AntoineG

When I try to retrieve the path of a file on my desktop, I get :

当我尝试检索桌面上文件的路径时,我得到:

/Users/MyName/Desktop/file.txt

But I need the full path (to feed a bash script):

但我需要完整路径(以提供 bash 脚本):

/Volumes/MyDrive/Users/MyName/Desktop/file.txt

I spent quite some time searching for a solution, but couldn't find any. I'm open to anything that can be run through Automator, bash or applescript.

我花了很长时间寻找解决方案,但找不到任何解决方案。我对任何可以通过 Automator、bash 或 applescript 运行的东西持开放态度。

It makes me crazy because if I simply drop my file.txt in Coda, it will output the full absolute path I want, right away, whereas Terminal won't.

这让我很抓狂,因为如果我简单地将我的 file.txt 放在 Coda 中,它会立即输出我想要的完整绝对路径,而 Terminal 不会。

Any ideas ?

有任何想法吗 ?

采纳答案by Mark Reed

So you need to find which volume under /Volumesis equivalent to /, yes? That's easy - just look for the symlink to /.

所以你需要找到下哪个体积/Volumes相当于/,是吗?这很简单 - 只需查找 / 的符号链接即可。

for vol in /Volumes/*; do 
  if [ "$(readlink "$vol")" = / ]; then 
    root_vol=$vol
  fi
done 

Then you can do

然后你可以做

echo "$root_vol$HOME/Desktop/file.txt"

or whatever.

管他呢。

回答by AntoineG

For anyone who might need it : if you're running an OSX Automator workflow/application/service and needs an absolute path to your input file, simply add a "Run shell script" action, set the input "as arguments" and use the following :

对于可能需要它的任何人:如果您正在运行 OSX Automator 工作流/应用程序/服务并且需要输入文件的绝对路径,只需添加一个“运行 shell 脚本”操作,将输入设置为“作为参数”并使用下列的 :



#!/bin/bash

## Check if the input is an absolute path including the volume name
if [[ "" = /Volumes/* ]]
    ## If TRUE, the path is already correct
    then
        echo ""

    ## If FALSE, needs fixing
    else
        ## Resolve the volume name
        ## by looping through all the volumes to find the one symlinked to "/"
        for vol in /Volumes/*
            do 
                if [ "$(readlink "$vol")" = / ]
                    then root_vol=$vol
                fi
            done
        ## Pass the path back to Automator for further use
        echo "$root_vol"
fi