Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Stuck on an IF!

  1. #1

    Default 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"
    else
    tar "incremental"
    fi

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

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Stuck on an IF!

    Quote Originally Posted by Alexart74 View Post
    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"
    else
    tar "incremental"
    fi

    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:

    Code:
    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.

  3. #3

    Default Re: Stuck on an IF!

    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?

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Stuck on an IF!

    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.

  5. #5

    Default Re: Stuck on an IF!

    Quote Originally Posted by ken_yap View Post
    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...

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Stuck on an IF!

    Quote Originally Posted by Alexart74 View Post
    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:

    Code:
    12 2 * * 1-4,6,7 root incr-backup
    12 2 * * 5 root full-backup
    Pass an argument to the script:

    Code:
    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.

    Code:
    12 2 * * 1-4,6,7 root incr-backup
    12 2 * * 5 root full-backup
    and inside the script:

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

  7. #7

    Default Re: Stuck on an IF!

    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!

  8. #8
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Stuck on an IF!

    Quote Originally Posted by Alexart74 View Post
    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 &#37;, 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.

  9. #9

    Default Re: Stuck on an IF!

    Quote Originally Posted by ken_yap View Post
    That should be a backtick, not a single quote, and also you should put spaces around the words, otherwise the syntax is wrong.
    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?

  10. #10
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Stuck on an IF!

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •