Loops in Action Scripts

Hello All,

We are trying to utilize Loops in action Scripts.

The objective is to take comma-separated input via relevance output and append it to the file.

We are utilizing the below-mentioned method but it is not working.

    foreach {relevance expression returning multiple values} {wait|run} <executable>

We are trying to do something like this.

foreach {substrings separated by "," of "a,b,c,d"}

But we are getting errors - not correct syntax in the fixed debugger action tab.

I have never successfully utilized loops within Action Script. In fact, I cannot find foreach defined in the Action Script language reference.

Instead I would recommend implementing your loop and related logic into a separate script (i.e. Bash, PowerShell, etc.) and executing via wait command and having error checking immediately following.

For example, the following lines of Action Script are from my Linux-WatchDog install fixlet for BESRelays.

// Inspect crontab tmp file for watchdog keywords & insert record if necessary
if {if (exists file "/var/opt/BESTools/crontab.tmp") then (if (not exists (lines whose (it contains "besrelay_watchdog.sh") of file "/var/opt/BESTools/crontab.tmp" as trimmed string)) then (True) else (False)) else (False)}
	wait sudo su - root -c "echo \"@hourly /var/opt/BESTools/besrelay_watchdog.sh\" >> /var/opt/BESTools/crontab.tmp; crontab /var/opt/BESTools/crontab.tmp"
	parameter "__ExitCode5" = "{if exist exit code of action then exit code of action as string else "995"}"
	if {parameter "__ExitCode5" != "0"}
		exit {parameter "__ExitCode5"}
	endif
endif

I hope this helps.

2 Likes

Agree with Casey, there is no built-in loop construct in ActionScript. It is pretty common practice to emulate a loop though, by building a batch file or shell script containing each thing you’d want to “loop” on-the-fly.

Given that %0d%0a is the Carriage Return / Line Feed combination used to mark the end of a line in a Windows file, we could create and execute a batch the following way. For Linux shell scripts you’d terminate the end-of-line with a single %0a character instead.

delete __appendfile
appendfile {concatenation "%0d%0a" of (<relevance expression returning multiple values that are executables or batch command lines>)}
move __appendfile MyBatchScript.cmd
wait cmd.exe /c MyBatchScript.cmd
2 Likes

You can also call a CMD “for” command (or PowerShell Cmdlet) to loop through results of relevance substitution. For this example I created various folders with the name starting TEST then this action script command deletes them via a CMD “for” loop

run cmd.exe /k "for %a in ({concatenation " " of pathnames of folders whose (name of it starts with "TEST") of folder "C:\TEMP"}) do rmdir %a /q

3 Likes

Thanks lot for reply, let me try out all the suggestions. Will catch up in case of doubts.

Thanks a lot for the help.

It worked for me