Stuck on an IF!

Hi everybody

after years of programming on windows I started to write some scripts on linux, and I’m stuck on a IF! I feel so rookie…

if ‘date +%u’=5]; then
tar “full”
tar “incremental”

I’d like to make a full backup on th 5th day of the week, and incremental all the others.
Where I do wrong?!?!

That should be a backtick, not a single quote, and also you should put spaces around the words, otherwise the syntax is wrong. So:

if  `date +%u` = 5 ]; then

Better still, if you are running it from a crontab you can specify the day of the week in the cron job’s rule and not do this test in the script.

Thankyou very much!
Yes, I’ll fire it from a crontab entry: so you suggest (if I’ve understood correctly: sorry but my english is not perfect…) to punt just a “$1” in the if and to add che date function to che cron line. Right?

In a cron job’s rule you can just say when the script is to be run. The fields are

min hour date month weekday

followed by the command (but there is a 6th field for the user in system crontabs, so watch out). So you don’t have to do any day testing in the script at all.

ok, but if I want different tar options according to the day of the week I suppose I have to make a test somewere…or not?
The cron should just run the script, with no options…

There are various ways to do this. Use different scripts:

12 2 * * 1-4,6,7 root incr-backup
12 2 * * 5 root full-backup

Pass an argument to the script:

12 2 * * 1-4,6,7 root do-backup i
12 2 * * 5 root do-backup f

Or if you can be clever and test the name of the script in the script, and make links to the same script by two different names.

12 2 * * 1-4,6,7 root incr-backup
12 2 * * 5 root full-backup

and inside the script:

case $0 in
*incr-backup) type=incremental ;;
*full-backup) type=full ;;
*) echo "Huh?"; exit 1 ;;

Thankyou very much for your answer and for you patience!
Is it possible to do something like this:
12 2 * * 1-7 root do-backup <arguments>
and put as <argument> the function that retrieves the day of the week?
So in the script I’d just make the test as an “if $0…” and I can test the script manually passing the argument!

Well that line is interpreted like a command typed to the shell so you could put a date command in backticks there, but that defeats the purpose of taking the test out of the script, you’ve just executed the date command somewhere else. (And you have to watch out for %, which is special in crontabs, see the manual page.) Since you only have two cases, incremental and full, I think it is simpler to have two lines in the crontab.

Also * is the same as 1-7 in the weekday field.

And $0 is not the first argument, it’s the name of the script itself.

Still many thanks.
Last question: when the syntax on an if condition is wrong the script consider che condition false instead of returning an error?

It depends on the error. It could be a syntax error, or the result could be anything unexpected. You can’t rely on a wrong program to produce any particular result.

Thanks a lot, also for the replies on the other post!