不能用 i 从 1 到 n 循环重复执行 shell 脚本 [英] Can't do shell script with a repeat with i from 1 to n loop

查看:20
本文介绍了不能用 i 从 1 到 n 循环重复执行 shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效(例如打印3 个参数"):

This works (prints, for example, "3 arguments"):

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

这不会(仅打印参数 3:三",而不是前两个参数):

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

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

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.

如果我在 do shell script 上面添加一个 display dialog 语句,它会抛出一个不允许用户交互"的错误,所以我知道它正在执行循环身体.

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.

我做错了什么?这个循环是什么导致 osascript 不打印任何东西?

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

推荐答案

试试这个以避免不得不使用临时文件.

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

这篇关于不能用 i 从 1 到 n 循环重复执行 shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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