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.
- 10-18-2012 #1Just 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.
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"Code:LASTSAT=$(cal|awk '{if(NF==7){SAT=$7}};END{print SAT}')
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
- 10-18-2012 #2Linux Enthusiast
- Join Date
- Apr 2012
- Location
- Virginia, USA
- Posts
- 561
Try putting everything to the right of the = sign in ""
- 10-19-2012 #3Just 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
- 10-19-2012 #4Linux Enthusiast
- Join Date
- Apr 2012
- Location
- Virginia, USA
- Posts
- 561
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)
- 10-20-2012 #5Just 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?
- 10-20-2012 #6Linux Enthusiast
- Join Date
- Apr 2012
- Location
- Virginia, USA
- Posts
- 561
-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.


Reply With Quote
