Open a terminal window, execute 4 commands, close the Terminal Window

I want a script that opens a terminal window executes 4 commands and then closes the Terminal window.

I am able to have the terminal window open. So first what command do I use to send text to that window and execute?

Second, some of the commands have ‘sudo’ in them so it will require a password, is there anything extra that will need to be added to handle passwords or will it work like another text command?

Thanks much for any help

Sim

First, are you sure you need to use terminal.app?

The fact that you’re closing the window afterwards implies not. If that’s the case you can use the standard addition ‘do shell script’ to run shell commands without using terminal.app at all.

Do shell script also handles the ‘sudo’ element with its " with administrator privileges’ option:

do shell script “blah blah blah” with administrator privileges

If you really do need to use terminal for some reason, the easiest thing to do is put all the commands in one statement and execute that:

set shell_cmd to "sudo cmd1;sudo cmd2;sudo cmd3;sudo cmd4"
tell application "Terminal"
	do script shell_cmd
end tell

The reason why this is a better approach is that there is no feedback from the terminal, so if you did it the ‘typical’ way:

tell application "Terminal"
	do script "sudo cmd1"
	do script "sudo cmd2" in window 1
	do script "sudo cmd3" in window 1
	-- etc...
end tell

It’s almost impossible to control timing such that cmd2 executes when cmd1 has finished.

Ok, this is almost the code I need. As you said I do not need the terminal window opened so ‘do shell script’ seems to work better.

I’m not sure if the code is getting run consecutively? I get error message that directory data does not exist for the second command. But it should exist if the first command is executed to change directory. What can I do to fix that?

Also I get a window that pops up to ask for a password, what can I do to get the password in the script?

do shell script "cd /usr/local/mysql"
do shell script "sudo chown -R mysql data/" with administrator privileges
do shell script "sudo echo" with administrator privileges
do shell script "sudo ./bin/mysqld_safe&" with administrator privileges

Thanks for your time.

Your script only really needs to be a couple of lines. The first would be to change the ownership of the files. (You don’t actually HAVE to be in the directory for this to happen, you can just write out the path explicitly)

You also don’t need to echo out the statement, since you are sending it directly. So… change your ownership and then start the process in the background. If this is for a startup script, there are better ways for this to occur.

See This URL about Startup Sequences for an example.

Otherwise, your script can be shortened to:


do shell script "sudo chown -R /usr/local/mysql/data/" with administrator privileges 
do shell script "sudo /usr/local/mysql/bin/mysqld_safe &" with administrator privileges

This won’t do what you expect.

What you have to be aware of is that each ‘do shell script’ command runs in its own shell, completely disconnected from any other do shell script command you have run.

This means that your second shell script (sudo chown -R…) has no concept of the previous shell script that changed to the directory you wanted.

In order to link commands, you need to run them in a single ‘do shell script’ command, using semi-colons to separate the commands:

do shell script "cd /usr/local/mysql; chown -R mysql data/" with administrator privileges

Note, also, that ‘with adminstrator privileges’ takes care of the ‘sudo’ element, so you don’t need both (and, in fact, shouldn’t use sudo).

If you do not use sudo (as stated above), the ‘with administrator privileges’ element will take care of prompting you for your password in order to run the command.