Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Retrieving terminal output status?

Hello,


I was wondering if it where possible to retrieve the terminal output status and store it into a variable once a command has been executed?


For example:

I write a applescript file using sheel commands to tell terminal to do something. Now terminal repleys and I would like this message to be saved in an variable in my applescript program? Possible? When yes, how?


Thanks - Help is appreciated!

Posted on Apr 28, 2011 3:21 PM

Reply
Question marked as Best reply

Posted on Apr 28, 2011 3:40 PM

Hi,


If you are using do shell script, you set a variable to the result. like this:


set myVar to do shell script "ls -al"


Although, on second reading, what you say you are doing may be more convoluted. Could you expand on what the actual process is?


Best wishes

John M

21 replies

Apr 28, 2011 4:42 PM in response to hi]

If you're using Terminal.app to run the shell commands (rather than AppleScript's built-in do shell script) then there's no easy way to capture the output since AppleScript has no knowledge of the shell process, or when it completes. The only way to do this with Terminal.app is to periodically poll the process list to see if your process is still running, then grab the output from the respective window.


Calling your shell command via do shell script is much easier.

Jul 21, 2012 11:37 PM in response to MacSter2008

Sure, but as I stated earlier, that is far from a 'solution'


For one, that returns the entire window contents - potentially a lot of data - and you have to parse through that to find the data you need,
Secondly, How do you know if your process has finished? It might be a task that takes a long time - or, even worse, a variable amount of time - so you have to implement some technique to detect when the process has finished and the 'data you seek' is actually available.


It might be the way to go, but for 99% of tasks do shell script (which doesn't use Terminal.app at all) is a better solution.

Jul 21, 2012 11:43 PM in response to Camelot

do shell script runs a different form of shell, not the same shell as Terminal app.

do shell script runs a unique shell that ends and can never be referenced ever again.

So if your shell commands require any follow up commands, they must be executed in the same line seporated with ; otherwise entering username and passwords for example, at multiple prompts are impossible.

So for 99% of tasks that require reading the output data, "do shell script" is not going to work.

If you need the output, you proobobly intend to follow up with another shell command based on that output.

That cannot be done in a unique applescript shell that ends and can never continue where it left off.


You can check the progress of a running shell with


set commandString to "/bin/ls -AelO" as text
tell application "Terminal"
set windowCount to (count of the windows)
if windowCount is greater than 0 then
repeat with w from 1 to windowCount
tell window 1
set windowID to id
set theTabs to tabs
repeat with thisTab in theTabs
if thisTab is not busy then
do script commandString in thisTab
set selected of thisTab to true
set frontmost to true
return
end if
end repeat
end tell
set frontmost of window id windowID to false
end repeat
end if
tell window 1
do script commandString
set frontmost to true
end tell
end tell


However I prefer the ease of do shell script when you only need one line of shell to run, and do not need any followup commands.

Jul 21, 2012 11:47 PM in response to Camelot

Not only is that THE solution (vs your "far from a solution") it is simple easy, and you mislead people by saying "there's no easy way to capture the output since AppleScript has no knowledge of the shell process, or when it completes."


When I read that I thought you where saying it was impossible, not easy without some output to file that AS must read etc. Then I find it is a one line command to get the entire window. Sure you have to parse the data, but is you are scripting in the shell.. we are above and past calling that difficult.

Jul 22, 2012 9:46 AM in response to MacSter2008

Regarding your

set exitStatus to do shell script "command; echo $?"

"do shell script" is Apple scripts way of running a unique shell script which has no connection to anything currently running in any window or tab in the terminal app.

This commant is therefore useless.

While the Applescript fragment is not mine (Linc's actually), there is nothing useless about it. It just might not be useful for something you want to do.


Linc's example will tell you if the 'command' you specified completed successfully or not. That can be very useful if you know that the command you are using will return a non-zero status if it did not do what was requested.


Now, reading the other things you have posted in this thread that you are hijacking, it would appear you are not interested in either capturing the output from a command executed from within the Applescript, nor capturing the completion status from a command executed from within the Applescript. It appears you want an Applescript that interacts with a Terminal session.


Given that, you would be better off a) starting a new thread, so you can give points for useful answers (the only payment anyone gets in these forums), and b) clearly stating what problem you are trying to solve.

Jul 22, 2012 9:58 AM in response to BobHarris

Perhaps you can give me an example of how "set exitStatus to do shell script "command; echo $?" will ever have any result other than "0"?

Because my understanding is that everytime you call "do shell script" in AS, you get a fresh new & Unique shell to execute the command(s) that have no way of picking up where the last "do shell script" left off. Therefore I must be wrong in what I read because it would mean you could never get any result other than "0" with that command that I reffered to as useless. So please prove me wrong.


Am I hyjacking?


I came to this thread while looking for a method to read the terminal app output from within AS because "do shell script" cannot continue multiple commands from where the last "do shell script" left off. It does however make an easy single line shell that returns a clean result good for a one time only thing. Even for multiple commands on the same line, but not for functions that change depending on the output from the shell.


I have found the solution was to use terminal, and the problem was "do shell script" does not execute the same bash as terminal, nor can it continue where it left off.


My "hyjacking" was to correct the incorrect statements made by others so that other people who are trying to solve something and find this thread find the answer as well as know some of the information posted on this thread was dead wrong.


I am not looking for points, payment, kudos or political he-said-she-said.

Just correcting people that post pointless code such as the one you quoted me commenting on as being useless because it will always return "0" no matter what. Right?

Jul 22, 2012 10:55 AM in response to MacSter2008

So how is echo returning any output that you aren't already getting returned on that same line?


The difference is, that the error is caught, but the error status is saved in the variable "errorstatus". Your script can continue.


Without the "echo" the script will be terminated with an error message.


set exitStatus to do shell script "cat /Users/nonexisting"

return "Done"

terminates with an error, the "return" will not be executed.


set exitStatus to do shell script "cat /Users/nonexisting; echo $?"

return "Done"

terminates without an error, after executing the following commands, but you save the error status in "exitstatus"

Retrieving terminal output status?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.