无法使用 i 从 1 到 n 循环重复执行 shell 脚本

发布于 2024-07-08 06:04:37 字数 806 浏览 7 评论 0原文

这有效(例如,打印“3 个参数”):

to run argv
    do shell script "echo " & (count argv) & " arguments"
end run

这不起作用(仅打印“参数 3:三个”,而不是前两个参数):

to run argv
    do shell script "echo " & (count argv) & " arguments"

    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "'"
    end repeat
end run

在这两种情况下,我都使用 运行脚本Mac OS X 10.5.5 上的 osascript。 调用示例:

osascript 'Script that takes arguments.applescript' Test argument three

我没有重定向输出,所以我知道脚本没有抛出错误。

如果我在 do shell 脚本 上方添加一个 display dial 语句,它会抛出“不允许用户交互”错误,因此我知道它正在执行循环体。

我究竟做错了什么? 这个循环导致 osascript 不打印任何内容的原因是什么?

This works (prints, for example, “3 arguments”):

to run argv
    do shell script "echo " & (count argv) & " arguments"
end run

This doesn't (prints only “Argument 3: three”, and not the previous two arguments):

to run argv
    do shell script "echo " & (count argv) & " arguments"

    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "'"
    end repeat
end run

In both cases, I'm running the script using osascript on Mac OS X 10.5.5. Example invocation:

osascript 'Script that takes arguments.applescript' Test argument three

I'm not redirecting the output, so I know that the script is not throwing an error.

If I add a display dialog statement above the do shell script, it throws a “no user interaction allowed” error, so I know that it is executing the loop body.

What am I doing wrong? What is it about this loop that causes osascript to not print anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2024-07-15 06:04:37

尝试这样做以避免使用临时文件。

to run argv
        set accumulator to do shell script "echo " & (count argv) & " arguments" altering line endings false
        repeat with i from 1 to (count argv)
                set ln to do shell script "echo 'Argument " & i & ": " & (item i of argv) & "'" altering line endings false
                set accumulator to accumulator & ln
        end repeat
        return accumulator
end run

Try this to avoid having to use the temporary file.

to run argv
        set accumulator to do shell script "echo " & (count argv) & " arguments" altering line endings false
        repeat with i from 1 to (count argv)
                set ln to do shell script "echo 'Argument " & i & ": " & (item i of argv) & "'" altering line endings false
                set accumulator to accumulator & ln
        end repeat
        return accumulator
end run
梦断已成空 2024-07-15 06:04:37

您的问题似乎与循环或 argv 的使用无关。 这是一个更简单的测试用例,其中只有最后一个 do shell script 实际上返回结果:

do shell script "echo foo"
delay 2
do shell script "echo bar"

此外,以下细微更改将产生预期结果:

to run argv
    do shell script "echo " & (count argv) & " arguments > /test.txt"
    
    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "' >> /test.txt"
    end repeat
end run

test.txt 将包含四个行,如下所示:

3 arguments
Argument 1: foo
Argument 2: bar
Argument 3: baz

此解决方法失败:

to run argv
    do shell script "echo " & (count argv) & " arguments > /tmp/foo.txt"
    
    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "' >> /tmp/foo.txt"
    end repeat
    
    do shell script "cat /tmp/foo.txt"
    do shell script "rm /tmp/foo.txt"
end run

即使现在,也仅返回最后一行。 这可能与TN2065的以下问题有关:

问:我的脚本会在很长一段时间内产生输出。 当结果出现时,我如何读取结果?

答:同样,简短的回答是,您不会 — do shell 脚本在命令完成之前不会返回。 在 Unix 术语中,它不能用于创建管道。 但是,您可以做的是将命令置于后台(请参阅下一个问题),将其输出发送到文件,然后在文件填满时读取该文件。

唉,我没有足够的 AppleScript-fu 来知道如何让 AppleScript 本身读取多行,我怀疑这会起作用。

Your problem appears to be unrelated to the loop, or the use of argv, for that matter. Here's a much simpler test case where only the last do shell script actually returns a result:

do shell script "echo foo"
delay 2
do shell script "echo bar"

In addition, the following slight change will produce expected results:

to run argv
    do shell script "echo " & (count argv) & " arguments > /test.txt"
    
    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "' >> /test.txt"
    end repeat
end run

test.txt will contain four lines, like so:

3 arguments
Argument 1: foo
Argument 2: bar
Argument 3: baz

This workaround fails:

to run argv
    do shell script "echo " & (count argv) & " arguments > /tmp/foo.txt"
    
    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "' >> /tmp/foo.txt"
    end repeat
    
    do shell script "cat /tmp/foo.txt"
    do shell script "rm /tmp/foo.txt"
end run

Even now, only the last line is returned. This may be related to the following question of TN2065:

Q: My script will produce output over a long time. How do I read the results as they come in?

A: Again, the short answer is that you don’t — do shell script will not return until the command is done. In Unix terms, it cannot be used to create a pipe. What you can do, however, is to put the command into the background (see the next question), send its output to a file, and then read the file as it fills up.

Alas, I don't have enough AppleScript-fu to know how to have AppleScript itself read multiple lines, which I suspect would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文