getting a result

I have a cpanel server and am writing a script to back it up. I want it all to be in the terminal. So far I have it working, but it’s not flexable, since I’ve hardcoded all the sites - which of course, makes it difficult to keep up when adding new sites.

I need to get some results and set them into vars - I just don’t know how to in AS.

  1. So, when I log into the server and do ‘ls’, I get a list of directories (site1/ site2/ site3/ etc.). How would I get each one and set it as a var?
  2. Once the script starts backing up each site into a tarball, I have to manually determine how long to delay the script before issuing the next command. I think it would be easier for the script to look out for a returned string and then proceed. No?

Here’s what I have (stripped down for brevity)

-- set the sites to the server directory names
set site1 to "admin"
set site2 to "foo"
set site3 to "bar"
-- would be nice to have AS get this list for me!

-- set the paramaters
set theServer to "123.456.78.90"
set theUser to "admin"
set thePassword to "password"
set remoteFolder to "/home/admin/public_html/site_backups/"
set initialPath to "public_html/site_backups"
set localFolder to "Users/admin/Sites/cPanel_Backup/"

-- login to the server
tell application "Terminal"
	try
		do script "ssh -c 3des -2 -l admin " & theServer & " -p 22" in window 1
		delay 2
		do script thePassword in window 1
	on error
		display dialog "Couldn't connect to server" buttons {"Damn!"} default button 1
	end try
end tell
delay 2
tell application "Terminal"
do script "su" in window 1
delay 1
do script thePassword in window 1
delay 1
	
do script "cd " & remoteFolder in window 1
delay 1
do script "rm -rf *" in window 1 -- delete any previous backups
delay 1

-- backup site
do script "cd /home/" & site1 in window 1
delay 1
do script "tar cvf " & remoteFolder & site1 & ".tar.gz ." in window 1
delay 60
-- would be great to have AS wait for a result, like root@server [/home/site1]#

do script "exit" in window 1
do script "exit" in window 1
delay 1
end tell

tell application "Terminal"
quit
end tell

TIA to anybody that would like to help! :slight_smile:

Used locally, this should give you a listing of a folders contents:

do shell script "ls -1 ~"
set listing to result

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 13
set listing to every text item of listing
set AppleScript's text item delimiters to ASTID

return listing

If you use “do script” in Terminal, then you won’t get a result, so I can’t think of a good way to do this remotely at the moment.

USE THE DICTIONARIES!

I’d do it guardian34’s way. But if you feel tethered to the Terminal try

get contents of window 1 of app “Terminal”

Thanks for the help.

guardian34: I get an error saying “the variable result is not defined” however, it does display the list of directories.

themacgeek: that returned everything except the directory listing :slight_smile:

this will make sure gaurdian’s will work

set resultof to do shell script "ls -1 ~"
set listing to resultof

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 13
set listing to every text item of listing
set AppleScript's text item delimiters to ASTID

return listing

That doesn’t work either. Same result. It rerturns a listing of my local home folder, but not the remote folder.
So if I change it to

set resultof to do script "ls -1" in window 1

it seems to work okay remotely - as far as listing goes - but I get an error “the variable resultof is not defined” in Script Editor.

I really appreciate the help :slight_smile:

Perhaps “do script” doesn’t return a result?