将字符串转换为日期并在Shell脚本中执行日期比较

时间:2018-09-26 01:18:42

标签: git shell

我是Shell脚本的新手,正在尝试执行以下操作:

if [[ $($last_update_date) -ge '2018-09-20' ]]; then
   echo "hello";
fi

last_updated_date就像2018-09-01。我需要将其与特定日期进行比较,并执行一些git操作。

有帮助吗?

for branch in $(git branch -r | sed 's/^\s*//'); do 

    ## Info about the branches before deleting
    echo branch: $branch;
    hasAct=$(git log --abbrev-commit --pretty=format:"%ad" --date=relative -1 $branch); 
    lastActivity=$(echo "$hasAct" | grep Date: | sed 's/Date: //');

    last_updated_date=$(git log --pretty=format:"%ad" --date=short -n 1 $branch);

    echo "$last_updated_date";
    echo "$hasAct";

    if [[ $($last_update_date) -ge '2018-09-20' ]]; then
       echo "hello";
    fi
    ## Delete from the remote
    ##git push origin --delete $k
done

1 个答案:

答案 0 :(得分:0)

-ge进行数字比较,但是像2018-09-20这样的日期不是数字。但是可以按字典顺序比较YYYY-MM-DD格式的日期,因此您可以尝试:

if [[ $last_update_date > '2018-09-20' ]]; then
   echo "hello";
fi

或者如果不使用sh代替bash:

if [ "$last_update_date" \> '2018-09-20' ]; then
   echo "hello";
fi
相关问题