MacOS – Running a bunch of commands in Terminal

macosscriptterminal

I run the following commands for logkext a lot in my terminal and would like a know a way to automate the whole process.

  1. Open terminal
  2. type "sudo logKextClient"
  3. Type in administrator password
  4. logKextClient has its own password which I need to type now
  5. logKextClient is running now. prompt is logKextClient >
  6. I have to type a command here. "open"
  7. Then I need to close a window that opens. Window is titled out_logFile.txt. Usually it opens in TextEdit. I don't mind force quitting it. As a copy of the file is saved on the desktop.

I'd like to be able to double click something and execute the above list of commands/actions flawlessly.

Many thanks for your suggestions.

Best Answer

I went through the exact same process back when I was playing around with logKext. The unix command you may want to explore is /usr/bin/expect.

It can get complex quickly, but basically what it does is act as a mediator between you and the programs you're running so it can provide answers that you would normally have to type. As an example, I built this script so I could automate the process of printing the logKext output. You should recognize all of the commands you'd normally type to logkextClient yourself...

#!/usr/bin/expect -f
spawn logkextClient
expect "logKext Password:"
send "mylogkextpassword\r"
expect "logKextClient > "
send "print\r"
expect "logKextClient > "
send "quit\r"
close

Back in 10.5 and 10.6 this worked well for me for outputting the logKext print so I could pipe it into an email and send it. However, I was running this logged in as root to terminal, so it was simple.

Theoretically, if you weren't logged in as root, you could instead say

#!/usr/bin/expect -f
spawn sudo -k logkextClient
expect "Password:"
send “myrootpassword\r”
expect "logKext Password:"
send "mylogkextpassword\r"
expect "logKextClient > "
send "print\r"
expect "logKextClient > "
send "quit\r"
close

(note that I used sudo -k intentionally to be consistent and require a password every time)

So you could use your favorite command line editor to create this script, do a chmod +x to it and drag it to the dock for launching... Theoretically.

But I have been having problems in Mavericks getting /usr/bin/expect to behave properly with sudo, so this isn't working for me in other scripts like it should. And I don't have logKext installed at all anymore for testing anyway.

But I think this is the direction you may want to head.

Good luck!