Multiple scripts with one do shell script

Hi,

I think I know how to run multiple shell scripts from one ‘do shell script’, but how do you get a return from each of them?

Thanks,
kel

Hello.

Commands in a subshell share the same input and output streams. A subshell, is when you are putting commands within parenthesis.
The line below, illustrates a possible way, there may be others. :slight_smile:

set a to do shell script "(echo \"a\";echo \"b\"; echo \"c\" )"

Of course you’ll have to find some way to extract the output of the different commands, possible by the help of interspersed echo commands outputting some “special” token.

  1. Pipe the output of each command to it’s own file and read those files afterwards.
  2. Print a separator between each command and use text item delimiters afterwards to split the data again.

Hello.

DJ Bazzie Wazzie is perfectly right, you really don’t need to put the semi-colon separated commands in a subshell, the do shell script collects it all. However, If you want to pipe the combined output from several commands into another command, then you have to assemble the commands in a subshell before piping it further.

Hi,

I can’t wait to try these things. I was thinking that it might save time using one ‘do shell script’ call.

Thanks a lot,
kel

Hi,

I tried running the processes from the semi-colon delimited list, but found that they run in sequence. I wanted to run different processes in parallel and found that I need to use the ‘&’ something like this:

-- semicolon delimited list runs processes in sequence
-- and runs in new subshell
set subshell1 to "(osascript -e 'beep 1'; sleep 5; echo 'a')"
set subshell2 to "(echo 'b'; sleep 2; osascript -e 'beep 1'; echo 'c')"
set cmd to subshell1 & " & " & subshell2 -- '&' runs processes in parallel
do shell script cmd

“b” is returned before “a”. This is what I wanted. Now I see why each process needs to write to file their results. Otherwise, you don’t know which parallel process ended first. The idea about adding tags to the results of each parallel process is also a good one.

Edited: btw, reviewed the bash manual and it’s just as long as it was before! :slight_smile:

Edited: renamed the variables in the above example script.

Thanks a lot,
kel

I found that adding tags doesn’t work.

-- semicolon delimited list runs processes in sequence
-- and runs in new subshell
set subshell1 to "(echo 'a'; osascript -e 'beep 1'; sleep 5; echo 'b')"
set subshell2 to "(echo 'c'; sleep 2; osascript -e 'beep 1'; echo 'd')"
set cmd to subshell1 & " & " & subshell2 -- '&' runs processes in parallel
do shell script cmd

→ “a
c
d
b”

Guess I need to pipe the output of each subshell and concatenate that result with the tags. Otherwise, need to write to file.

Thanks,
kel

Think I found how to do it with the wait command:

-- semicolon delimited list runs processes in sequence
-- and runs in new subshell
set subshell1 to "(result1='Message1'; sleep 4; osascript -e 'beep 1'; wait; echo '<a>'$result1'<b>')"
set subshell2 to "(result2='Message2'; sleep 2; osascript -e 'beep 2'; wait; echo '<c>'$result2'<d>')"
set cmd to subshell1 & " & " & subshell2 -- '&' runs processes in parallel
do shell script cmd

Hope there’s nothing wrong with this one.

Edited: wait, I didn’t need the wait command when written like that:

-- semicolon delimited list runs processes in sequence
-- and runs in new subshell
set subshell1 to "(result1='Message1'; sleep 4; osascript -e 'beep 1'; echo '<a>'$result1'<b>')"
set subshell2 to "(result2='Message2'; sleep 2; osascript -e 'beep 2'; echo '<c>'$result2'<d>')"
set cmd to subshell1 & " & " & subshell2 -- '&' runs processes in parallel
do shell script cmd

I better take a rest from this.

I was thinking that it might be better to send the results back as key-value pairs or a record. Then I don’t need to deal with the tags and parsing the text. I wonder which way is better. Probably parsing the text is better I think. Interesting.

Edited: my guess is that reading a record with AppleScript would be faster than parsing the text or getting a value from a key-value pair. Maybe in a Cocoa-AppleScript app getting the value from xml might be faster. Just guessing.