0

I am trying to learn more shell scripting. The available shells on this machine are /bin/sh, /bin/csh, and /bin/tcsh with sh being default and used here. OS is FreeBSD 9.1-RELEASE.

My current project needs to check whether a process updated the database yesterday. The first two echoes are just there for the moment verifying the variables have what I think they do.

#!/bin/sh
lastcheck=$(mysql -h dbserver.mysite.com -u myuser -pmypass mydb -se "SELECT MAX(DATE_FORMAT(datetime_sent_to_fulfiller,'%Y%m%d')) FROM print_mailing_request;"|cut -f1)
yesterday=$(echo -e "$(TZ=GMT+30 date +%Y%m%d)\n$(TZ=GMT+20 date +%Y%m%d)" | grep -v $(date +%Y-%m-%d) | tail -1)

echo "previous day was $yesterday"
echo "we last checked on $lastcheck"

if [ "$lastcheck" -eq "$yesterday" ]; then 
    echo "cool" 
else
    echo "uncool"  
fi;

One question is why the : not found: output is showing up and how do I prevent it?

Another question is why both 'cool' and 'uncool' are being echoed?

Last question is why 'else' is being echoed?

$ /bin/sh pmr.cron.sh
: not found:
previous day was 20160602
we last checked on 20160602
: not found:
: not found:
cool
: not found: else
uncool
: not found:
jerrygarciuh
  • 21,158
  • 26
  • 82
  • 139
  • 1
    I'm going to guess you have carriage returns at the end of your lines. – Mark Reed Jun 03 '16 at 15:25
  • @MarkReed - ah- yes vi did show the ^Ms that local edits on Windows created. I wasn't aware those would have that effect, Add as an answer please. – jerrygarciuh Jun 03 '16 at 15:28
  • And thank you very much. StackOverflow is great because newbs like me can get such spot-on advice here. – jerrygarciuh Jun 03 '16 at 15:28
  • 2
    Maybe see also the [Stack Overflow `bash` tag wiki](/tags/bash/info) which has a big FAQ section, where many questions and answers also apply to regular `sh`. – tripleee Jun 03 '16 at 15:37

1 Answers1

1
  1. You have carriage returns in your script; that generates the "not found" messages and is probably why both branches of your if are getting generated.
  2. Your dates are comparable as strings, no need to use -eq to compare them as numbers.
Mark Reed
  • 91,912
  • 16
  • 138
  • 175