Thursday, 15 January 2015

unix - Compare dates with bash -



unix - Compare dates with bash -

i need collect user input determine date , compare today's date.

here have:

echo "enter current date (yymmdd):" read date current_date= date +"%y%m%d" if [ "$date" == "$current_date" ]; echo "match" else echo "no match" fi

it prints no match when expected match , no match.

try this:

echo "enter current date (yymmdd):" read date current_date=`date +"%y%m%d"` if [ "$date" = "$current_date" ]; echo "match" else echo "no match" fi

change on line :

current_date=`date +"%y%m%d"`

reason: in code, using current_date= date +"%y%m%d". though intention utilize date command generate current date, line behaves differently (becomes equivalent current_date= ; date +"%y%m%d"; , results in current_date beingness empty. can echo $current_date verify this. correction provided, executing date command , saving output current_date variable. hope clear now. don't know how explain in improve way... :-)

bash unix

No comments:

Post a Comment