Henin RK
Henin RK

Reputation: 298

Error Converting String to Date in Shell

Hi I get the below error when I try to execute the below code:

date_input= '2015-01-25'
date_parameter=$(date -d `echo $date_input` +%s)

min_date=$(date -d 2015-11-01 +%s)
max_date=$(date -d $(date +"%Y-%m-%d") +%s)

if [ "$date_parameter" -gt "$max_date" ] || [ "$date_parameter" -lt "$min_date" ]; then

Error -> [: : integer expression expected

Upvotes: 0

Views: 54

Answers (2)

anubhava
anubhava

Reputation: 785146

You can fix your script by using:

date_input='2015-01-25'
date_parameter=$(date -d "$date_input" '+%s')
min_date=$(date -d 2015-11-01 '+%s')
max_date=$(date '+%s')

[[ $date_parameter -gt $max_date || $date_parameter -lt $min_date ]] &&
  echo "ok" || echo "nope"

Upvotes: 3

Cyrus
Cyrus

Reputation: 88626

Remove whitespace after = in first line.

Upvotes: 2

Related Questions