微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在 Applescript 调用中使用命令替换来调用 `do shell script`?

如何解决是否可以在 Applescript 调用中使用命令替换来调用 `do shell script`?

我有一个自动化服务,我正在更新该服务以组合处理一系列输入文件(而不是像目前那样连续处理)。它做了很多事情,但在其中的一个组件中,我需要处理 N 个文件内容,并将每个文件输出处理交给单个 paste 命令,以将其组合起来并进一步处理组合。在命令行上,我会使用进程替换来完成,例如:

paste <(commands processing file 1) <(commands processing file 2) ... | other processing commands

但是如果我从一个applescript里面这样做,就像这样:

set output to (do shell script "paste <(commands processing file 1) <(commands processing file 2) ... | other processing commands")

我从 applescript 收到一个错误

The action “Run AppleScript” encountered an error: “Finder got an error: sh: -c: line 0: Syntax error near unexpected token `('
sh: -c: line 0: `paste <(commands processing file 1) <(commands processing file 2) ... | other processing commands'”

我了解到这是因为 sh 没有做 bash 做的那些花哨的事情,例如过程替换。

我知道我可以只写临时文件来实现我的目标,但如果有办法解决的话,我宁愿不必这样做。

我尝试使用 bash -s解决这个问题:

set output to (do shell script "bash -s <<'EOF'" & return & "paste <(commands processing file 1) <(commands processing file 2) | other processing commands" & return & "EOF")

但这会产生同样的错误

知道如何在无需编写临时文件的情况下完成此操作吗?

更新:我意识到我把问题缩小了太多。还有一点。我正在使用的一系列命令(包括已经提到的 paste 命令),包括包含变量和单引号的单行代码,因此解决方案必须防止变量的 shell 插值并且不干扰单个-引号。我将更新下面的玩具示例以包含这些详细信息。


玩具示例(applescript):

文件 1:

this is the first test

文件 2:

this is the second test

苹果脚本:

set file1 to "~/file1.txt"
set file2 to "~/file2.txt"
set output to (do shell script "paste <(head " & file1 & " | perl -ne 'chomp;print(substr($_,4),qq(\\n))') <(head " & file2 & " | perl -ne 'chomp;print(substr($_,-4),qq(\\n))')"
display dialog output

预期输出

this    test

请注意,还有其他变量和另一个使用单引号的命令 (awk)。

解决方法

天哪!在我发布这篇文章后,我意识到我需要做的就是逃避括号。我和SRR1,SRR2,1 SRR1,2 SRR3,SRR4,1 SRR3,2 走对了!我认为写出问题只是我实现答案所必须经历的过程!

bash -s

我不敢相信我在发布问题之前没有看到它!

,

以下对我有用:

set file1 to "~/file1.txt"
set file2 to "~/file2.txt"
set output to do shell script "bash -c \"paste <(head " & file1 & ") <(head " & file2 & ")\""
display dialog output

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。