Shell scripts (as an alternative to applescript)

daver969

Registered
Hi,
I have a couple of questions, related to my ongoing quest to find the ultimate backup script. Basically, I want to set up a script to backup the files in my user space once a day. I used to use Applescript to do this, but it required me to actively execute the script every day, and now that I've migrated to os X my applescripts don't work anymore. So I'm thinking of ditching them and writing some scripts in the shell language (is C-shell X's default shell?)

Here are my questions:

1) what is the unix command to determine if a file/folder exists?

2) can I write a script that is automatically executed once a day, say at 4 am?

and a sort of meta-question, is there a good book available for learning UNIX as it pertains to OS X?
 
1) what is the unix command to determine if a file/folder exists?

depends on the shell you are using... This works in an sh script
#!/bin/sh

if [ -e afile ]
then
echo afile exists
fi

2) can I write a script that is automatically executed once a day, say at 4 am?

cron is the service that schedules regular events. the command "crontab" allows you too edit your own scheduled events. many man pages to read to get the format just right. You can check out the file /etc/crontab for an example of the format. You probably shouldn't add things to the root crontab unless you really need the script to be run as root.
 
A word of caution about crontab. You should not modify /etc/crontab (or any individual's crontab) directly with an editor. Instead the command 'crontab -e' should be used. This will ensure that the crontab file is properly loaded by cron after the modifications. I've seen really odd problems with cron if the crontab file is not handled properly. Besides crontab -e makes it very simple to manage the file. See 'man crontab' for more info...
 
You do not necessarily need to learn another scripting language. Open your existing Applscript is OS X's Script Editor (found in /Applications/AppleScript ), make any modifications that may be necessary to make them run under OS X (shouldn't be very much if you are just scripting the Finder), and save it as an application.

Now, set up your crontab to run the application you just created (using the "open" command, which allows you to open non-terminal apps from the terminal). If you want it to run at 4 AM, your crontab would look something like this:

0 4 * * * /usr/bin/open /home/me/BackupScript

You probably don't even need to specify /usr/bin, but crontab runs with a very limited environment that may not have /usr/bin in the path.

Post back whether or not this works. I would also be interested to know why your AppleScripts aren't running under X. I could potentially help with that as well.
 
There might be a problem with opening Applescripts via /usr/bin/open when the user running them is not logged in.

Does anyone know, has Apple made this possible in OS X? I don't know any Applescript, so it's not quite worth my while to learn it just to satisfy my curiosity - I do fine with /bin/sh...
 
That's right, the open command doesn't work unless that particular user is logged in.

You should check man osascript, which allows you to run AppleScripts from the command-line.

For running a text file (it doesn't even have to be precompiled), you just type

osascript foo.scpt

or to run a single line,

osascript -e "the command"
 
I need to think more before I post.

AppleScript doesn't do much on it's own, it simply tells other applications to do things (e.g. to copy a file, you tell the Finder to copy a file). So, if you aren't logged in, and the Finder (or other apps that you are trying to script) isn't running, Applescripts will return errors when you try to run them, whether you use "open" or "osascript" to run them.

If you want to learn another scripting language, I would suggest Perl. I think you will find that it is much more useful than shell scripting languages.

You asked about books. Take a look at mac.oreilly.com . Most of the books listed there aren't Mac specific, but they should give you a good overview of UNIX in general. They will soon be publishing a book called "Mac OS X: The Missing Manual", but I don't know how much UNIX it will cover. If you do decide to go with Perl, I would start with "Learning Perl", and then move up to "Programming Perl" (both by O'Reilly) if you need to get more advanced.
 
If you're going to use a shell script to make backups by copying files into a different drive (or whatever) you should be aware that standard Unix cp will not copy resource forks.

If you have the Dev tools installed, use /Developer/Tools/CpMac. It is basically the same as cp, but it copies resource forks. No man page that I've found though...

If you aren't worried about resource forks, you could just use something simple like:

gnutar -czf /Volumes/Otherdrive/backup.tar.gz /Users
 
Thanks everybody for your help, here's what I decided to do:

1) write a shell script that does what my old applescript did. It was easy since i basically know unix, all i needed was the "if [ -e file ]" part to test for existence.

2) use cron to run it once a day: 0 4 * * * * sh myscript


Now I want to get a little more fancy, and write a better script that involves
saving information from session to session, so I need to save information in a file and update it every time the script runs. I'm trying to figure out how to read the contents of the file into a variable. According to some guides I've read, the Bourne shell will evaluate the following

set todaysdate = 'date'

by running the date program and putting the result in the var todaysdate.
So I figured that I could do

set result = 'cat thefile'

to get the contents of "thefile" into 'result'. But it doesn't work in OS X's shell (neither does the date example above)

so I'm stuck with more questions:

1) how do I read the contents of a file into a variable using OS X's default shell?

2) what shell does OS X use? and

3) where can I find a decent ref. guide to scripting this shell? (other than the man pages)
 
OK, after doing a little research I discovered the following things:

1) the correct format is set todaysdate = `date` where the quote is the 'backquote' and not the straight up&down quote.

2) this only works in the C-shell (i.e. csh, and not sh)

3) OS X has both shells, the regular sh shell, (Bourne?) and the C-shell.

4) scripting the shell automatically with cron is cool.
 
Back
Top