Need some help in fixing simple bash script below. What it does is to compare dates in epoch format using if else. The script does not work as I intended because it always goes to the first condition DEPLOY all the time.
Even if I set the deploymentDate variable to be greater than currentDate it still goes to the first condition.
Can anyone suggest on how to fix it?
#!/bin/bash
currentDate=$(date +s% )
deploymentDate=1513516201
if [ "$currentDate" > "$deploymentDate" ]
then
XSL="DEPLOY"
else
XSL="DO NOT DEPLOY"
fi
echo $XSL
Output
DEPLOY
%s
nots%
also within command substitution$(date +%s)
– αғsнιη Dec 18 '17 at 04:50Is [[ ]] preferable over [ ] in Bash?
– αғsнιη Dec 18 '17 at 05:33[[ x > y ]]
(a ksh/zsh/bash operator) performs a lexical comparison (though typically, you'd have a problem if the deployment date was before September 2001), for a numerical comparison, use[ "$currentDate" -gt "$deploymentDate" ]
(POSIX) or((currentDate > deploymentDate))
(ksh, zsh, bash) – Stéphane Chazelas Dec 18 '17 at 12:45