Q:

Compare variables

I have a date in file called lastbackup with date in the yyyymmdd format. Now, I would like to compare this string with current date in the same format in BASH.
I have:
#!/bin/bash

cd ~

DATE=`date +%Y%m%d`
LASTBACKUP=sudo cat ./lastbackup

echo $LASTBACKUP
echo $DATE

if [ "$LASTBACKUP" == "$DATE" ]; then
  echo "Do nothing, already processed."
else 
  echo "Do something, not processed"
  echo -n $DATE > ./lastbackup
fi    

I keep getting Do something... and I can't figure out why DATE and LASTBACKUP variables are different, because in output it looks like they are equal:
$ testeq 
20140407
20140407
Do something, not processed

When I use (as suggested by Alex):
echo "LASTBACKUP: '$LASTBACKUP'"
echo "DATE: '$DATE'"

Then I get:
$ testeq 
20140407LASTBACKUP: ''
DATE: '20140407'

And now it is clear to me what is going on.

A:

Here's the reason this is wrong:
LASTBACKUP=sudo cat ./lastbackup

When you execute a command in this form: 
var=val var2=val2 ... command arg arg ...

The variables are put into the environment of the command, and only into that environment. So, while cat is running, it has an environment variable named LASTBACKUP with the value "sudo". Once cat is finished, that variable will disappear.
The contents of the ./lastbackup file get cat'ed to stdout. I'm surprised you don't see a blank line when you echo $LASTBACKUP. Check your files for CRLF line endings, or perhaps the ./lastbackup file does not end with a newline.
When you're debugging and printing variables out, do something more obvious to see their value: 
printf "LASTBACKUP='%s'\n" "$LASTBACKUP"
# or
for var in DATE LASTBACKUP; do
    printf "%s='%s'\n" "$var" "${!var}"
done

bash has a builtin way to read a file so you don't have to call cat: this will have you a couple of milliseconds:
lastbackup=$(< ./lastbackup)

Get out of the habit of using ALL_CAPS_VARS -- one day you'll use PATH=something and then wonder why your script stops working.

A:

Yep, the problem is probably that you're not actually initializing the LASTBACKUP variable anywhere.
But the fact that you're trying to read it with sudo bothers me. Doesn't the user that runs the script has permission to read a file that is in its $HOME? You are cd'ing into ~, which is the user's home, but since you're sudoing its read makes me think that maybe that file is in /root/lastbackup? Why would you sudo its reading otherwise?
This should do the trick:
#!/bin/bash

# For testings:
# date +%Y%m%d > $HOME/lastbackup # Uncomment to create a "./lastbackup" test file.

# Let's beging

cd ~

DATE=`date +%Y%m%d`
LASTBACKUP=`cat ./lastbackup`

echo $LASTBACKUP
echo $DATE

if [ "$LASTBACKUP" == "$DATE" ]; then
  echo "Do nothing, already processed."
else 
  echo "Do something, not processed"
  echo -n $DATE > ./lastbackup
fi 

Another option, if you're running a fairly new version of bash, and since your date format doesn't have any non-number character as separator (no - , / ...) would be consider the values numbers in the if:
if (( $LASTBACKUP == $DATE )); then
  echo "Do nothing, already processed."
else 
  echo "Do something, not processed"
  echo -n $DATE > ./lastbackup
fi

And, while testing, don't escape the LASTBACKUP or DATE variables with quotation marks ". If there was a problem reading the file (or getting the current date, but I doubt that one) you'll see an error. It'll help debugging.

