Find the answer to your Linux question:
Results 1 to 6 of 6
Hello! I'm making a backup script for a school assignent, includes a part where I have to check if it's the last saturday in each month or not. I've got ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    3

    Help defining a Variable. Error message: Expecting integer


    Hello!

    I'm making a backup script for a school assignent, includes a part where I have to check if it's the last saturday in each month or not.
    I've got the checking and everything already done, but I can't get the varible that's supposed to find the last saturday of each month to work.

    Code:
    LASTSAT=$(cal|awk '{if(NF==7){SAT=$7}};END{print SAT}')
    This is what I'm trying to put into the variable LASTSAT, but when I run the script I get the error message on that line telling me "integer expression expected"
    When I put it in the terminal it works, and does what I'm looking for just fine (returning the last saturday as a number)

    What am I doing wrong?

    Thanks in advance
    Jompa.
    Last edited by jompa; 10-18-2012 at 09:01 PM. Reason: Confused and tired

  2. #2
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    909
    Try putting everything to the right of the = sign in ""

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    3
    It didn't do the trick :/
    Maybe I should include the code calling the variable, I didn't think that it might be that part that could be wrong

    Code:
      if [ $DAY -eq $SATURDAY ]
        then
    	if [ $DAY -eq $LASTSAT ]
    	then
    	    echo "Idag tar vi bort alla lördags backuper som är minst $OLDLAST dagar gamla..."
    	    find . -name '$FILEL' -mtime +$OLDLAST -delete	
    	else
    	    echo "Idag tar vi bort alla lördags backuper som är minst $OLDSAT dagar gamla..."
    	    find . -name '$FILES' -mtime +$OLDSAT -delete	
    	fi                                                   
        fi

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    909
    Yes, the problem was in that second part.

    When you're running the first part LASTSAT=..., it's writing the variable as a string.

    So, just make these changes:

    if [ "$DAY" = "$SATURDAY" ]
    then
    if [ "$DAY" = "$LASTSAT" ]

    Bash is funny about variables and variable types (because they are un-typed)

  6. #5
    Just Joined!
    Join Date
    Oct 2012
    Posts
    3
    Ahh I see.
    Thanks for your help! It's really appriciated.

    So as soon as you have a varible which haven't got the very definition of it printed in black and white you have to use = instead of -eq?

  7. #6
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    909
    -eq is only for integers. Since you used awk to select a certain value from an output that was already a string, bash decided it was a string.
    I'm not a master at explaining bash scripting. check out the test man page
    man test

    That will tell you lots of different ways to compare things in bash.

Posting Permissions

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