Unable to execute shell script repeating with i from 1 to n loop

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

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

This is not (only Argument 3: Three prints, 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 run the script using osascripton Mac OS X 10.5.5. Call example:

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

I do not redirect the output, so I know that the script does not throw an error.

If I add the instruction display dialogabove do shell script, it throws a "no user intervention" error, so I know that it is executing the body of the loop.

What am I doing wrong? What is this loop that makes osascript not print anything?

+3
source share
2 answers

, .

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
+2

, -, 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:

Q: script . ?

A: , , shell script, . Unix . (. ), , .

, AppleScript-fu, , AppleScript , , , .

0

Source: https://habr.com/ru/post/1699313/


All Articles