如何在bash中使用ldapsearch的输出

时间:2017-05-10 07:11:24

标签: bash ldap openldap

假设我以这种格式从ldapsearch输出:

dn: user1...
dateOfExpire: someDate
dn: user2
dateOfExpire: someDate
....

我该怎么做:

if [ "$someDate" = "$now" ]; then
    echo "same day"
elif expr "$someDate" "<" "$now" >/dev/null; then
    do some action with user account which date is expired
fi

这成功地比较了两个日期,但我不知道如何使用相关帐户。

全部谢谢

2 个答案:

答案 0 :(得分:0)

如果你有GNU date,你可以将日期转换为Epoch,进行简单的整数比较,并获得退出状态以进行进一步的操作。例如:

% date -d 'yesterday' '+%s'
1494314230
% date '+%s'               
1494400638

% (( $(date '+%s') > $(date -d 'yesterday' '+%s') ))
% echo $?
0

% (( $(date '+%s') < $(date -d 'yesterday' '+%s') ))
% echo $?                                           
1

答案 1 :(得分:0)

你可以试试这个:

#!/bin/bash

ldapDate='20170406000000'
today=$(date '+%Y/%m/%d 00:00:00')
someDate=$(sed -E 's|^([0-9]{4})([0-9]{2})([0-9]{2}).*$|\1/\2/\3 00:00:00|g' <<< $ldapDate)
today_t=$(date -d "$today" '+%s')
somedate_t=$(date -d "$someDate" '+%s')

if [[ "$somedate_t" -eq "$today_t" ]]; then
    echo "Same Day"
elif [[ "$somedate_t" -le "$today_t" ]]; then
    echo "Expired"
elif [[ "$somedate_t" -ge "$today_t" ]]; then
    echo "Not Expired"
else
    echo "Unknown"
fi