mdfind用于创建符号链接无法正常工作 [英] mdfind used for creating symlinks not working as expected

查看:193
本文介绍了mdfind用于创建符号链接无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mdfind的输出来创建一堆符号链接. mdfind的输出是这样的:

I am trying to use the output from mdfind to create a bunch of symlinks. Output of mdfind is like this:

/pathtofile1/
/pathtofile2/
/pathtofile3/

/pathtofile1/
/pathtofile2/
/pathtofile3/

因此,我使用sedln -s添加到每行的开头,并添加awk {print $0 "/directory where I want this/"};

So, I used sed to add ln -s to the start of each line, and awk {print $0 "/directory where I want this/"};

单行脚本成功输出以下内容后:

after my single-line script successfully outputs this:

ln -s"/pathtofile1/""/我想要这个的目录"
ln -s"/pathtofile2/""/我想要这个的目录"
ln -s"/pathtofile3/""/我想要的目录"

ln -s "/pathtofile1/" "/directory where I want this"
ln -s "/pathtofile2/" "/directory where I want this"
ln -s "/pathtofile3/" "/directory where I want this"

问题是,当我运行此命令时,出现以下错误:"/我想要的目录:文件不存在"

Problem is, when I run this, I get this error: "/directory where I want this: File does not exist"

奇怪的是,当我分别运行这些行时,它们的链接是按预期方式创建的,但是运行整个命令将返回上面的错误.

The weird thing is that when I run these lines individually, they links are created as expected, but running the entire command returns the error above.

有什么想法吗?

我不认为这是完成我想做的事情的理想方法,因此,如果您有更好的解决方案,请告诉我.

I don't think that this is the ideal way to do what I'm trying to do, so let me know if you have any better solutions.

#! /bin/bash
itemList=`mdfind -s "$1"| awk '{ print "ln -s \""$0"\" \"/Users/username/Local/Recent\""}'`
echo "$itemList"
`$itemList`

$ 1是一个测试* .savedSearch,它返回文件列表.

$1 is a test *.savedSearch that returns a list of files.

我的结果(来自回声)是:

My result (from the echo) is:

ln -s "/Users/username/Dropbox/Document.pdf" "/Users/username/Local/Recent"
ln -s "/Users/username/Dropbox/Document2.pdf" "/Users/username/Local/Recent"

我得到的错误是:

ln:"/Users/username/Local/Recent":没有此类文件或目录

ln: "/Users/username/Local/Recent": No such file or directory

但是,如果我分别对每行进行复制粘贴,则会按预期创建链接.

But, if I run a copy-pasted of each line individually, the links are created as expected.

推荐答案

一种简单的方法:

mdfind -0 "query" | ( cd "/Users/username/Local/Recent" ; xargs -0 -I path ln -s path . )

这当然不会处理重复的文件名,等等.

This is of course doesn't handle duplicate file names, etc.

您的解决方案失败的原因是,首先,$itemList的内容被作为一个长命令执行(即,awk输出的换行被忽略),其次,命令替换发生在引号之前移动.实际处理的内容大致等同于:

The reasons your solution is failing is that, first, the contents of $itemList is being executed as one long command (i.e. the line feeds output by awk are ignored), and then, second, the command substitution occurs before quote removal. What is actually processed is roughly equivalent to:

ln '-s' '"/pathtofile1/"' '"/to"' 'ln' '-s' '"/pathtofile2/"' '"/to"' 'ln' '-s' '"/pathtofile3/"' '"/to"'

/bin/ln将此识别为:

ln [-Ffhinsv] source_file ... target_dir

命令的形式,并检查最终参数是否为现有目录.该测试失败,因为目录名包含引号.请仔细注意您报告并比较的错误消息:

form of the command and checks to see that the final parameter is an existing directory. That test fails because the directory name includes the surrounding quote marks. Note carefully the error message you report and compare:

$ ln a b c "/Users/username/Local/Recent"
ln: /Users/username/Local/Recent: No such file or directory
$ ln a b c '"/Users/username/Local/Recent"'
ln: "/Users/username/Local/Recent": No such file or directory

因此,故事的寓意是,当您在外壳中处理文件名时,最安全的解决方案是避免对文件名进行外壳处理,从而不必处理引号和其他副作用(是xargs解决方案的一大优势),并且保持简单:避免构造复杂的多行shell命令.要获得意想不到的结果太容易了.

So the morals of the story are, when you are dealing with file names in a shell, the safest solution is to avoid shell processing of the file names so you don't have to deal with quoting and other side effects (which is a big advantage of an xargs solution) and keep it simple: avoid constructing complex multi-line shell commands. It's too easy to get unexpected results.

这篇关于mdfind用于创建符号链接无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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