不能做shell脚本与我到n循环重复1 [英] Can't do shell script with a repeat with i from 1 to n loop

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

问题描述

该作品(版画,例如,3个参数):

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

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

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

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

在这两种情况下,我正在使用 osascript 在Mac OS X 10.5.5的脚本。例如调用:

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.

如果我添加一个显示对话框上方做shell脚本,它抛出一个不允许用户交互声明的错误,所以我知道它是在执行循环体。

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

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

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