1

I've written a short script that pulls a line from /etc/sudoers (## Expires 12122012) on a remote host, and then compares it to today's date, using date +%d%m%Y. Even though both values appear the same when the variables assigned to them are dispalyed, the if statement never shows them as a match. The script and output are below. The extra echo statements are there to verify the variables actually matched.

#!/bin/bash
TODAY=`date +%d%m%Y`
EXPIRES=`ssh -t hostname "grep Expires /etc/sudoers"`
EXPIREDATE=`echo $EXPIRES | awk -F " " '{ printf $3 }'`

if [ $TODAY = $EXPIREDATE ]
    then
            echo "This matches"
            exit
    else
            echo "this doesn't match"
            echo "$TODAY"
            echo "$EXPIREDATE"
            exit
fi
exit

output: sh test

this doesn't match
12122012
12122012

I've tried multiple variations of the comparison but no luck. Any ideas are appreciated. Thanks!

UPDATE
After applying some suggestions from the comments for checking for white space, I'm able to see a bit more with the echo statements. Not quite sure how this is occuring 0_o

#>echo test${TODAY}more
   test12122012more
#>echo test${EXPIREDATE}more
   more12122012
5
  • Try quoting the strings: if [ "$TODAY" = "$EXPIREDATE" ]. Commented Dec 12, 2012 at 14:14
  • 2
    Could you send the output of your program through cat thing | od -c? Commented Dec 12, 2012 at 14:23
  • Maybe there are spaces or newlines in the strings. Instead of echo "$TODAY", print echo "test${TODAY}test", which will show you the whitespace. Commented Dec 12, 2012 at 14:23
  • 1
    @spaceknarf: it's more fun if you don't put the same thing before and after the var, in case there's a windows line-ending thing in the way. If that's what we have here, and you echo "test${EXPIREDATE}foo", the output would be foot1212... :) Commented Dec 12, 2012 at 14:33
  • @unwind I've tried the quotes on both sides with no success. Thanks for the suggestion I've added the lines before and after the var and it definitely revealed an issue. See below test12122012word word12122012 Commented Dec 12, 2012 at 14:41

1 Answer 1

2

Your remote file is using CRLF newlines. You'll need to pass your text through tr in order to strip out the CR.

... | tr -d '\r'
Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Thank you so much. I've been losing my mind trying to get this to work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.