String 将系统日期与文本文件中的日期进行比较
我得到文本文件中的日期并将其分配给变量。当我从文件中记下日期时,我得到了这个String 将系统日期与文本文件中的日期进行比较,string,shell,date,unix-timestamp,String,Shell,Date,Unix Timestamp,我得到文本文件中的日期并将其分配给变量。当我从文件中记下日期时,我得到了这个 Not After : Jul 28 14:09:57 2017 GMT 所以我只能用这个命令裁剪出日期 echo $dateFile | cut -d ':' -f 2,4 结果将是 Jul 28 14:57 2017 GMT 如何将此日期转换为秒数,以便与系统日期进行比较?如果超过2天 我有这个代码,但它不工作。我运行时收到一条错误消息。我认为这是因为$dateFile是一个文本文件,它不知道如何转换它。任何
Not After : Jul 28 14:09:57 2017 GMT
所以我只能用这个命令裁剪出日期
echo $dateFile | cut -d ':' -f 2,4
结果将是
Jul 28 14:57 2017 GMT
如何将此日期转换为秒数,以便与系统日期进行比较?如果超过2天
我有这个代码,但它不工作。我运行时收到一条错误消息。我认为这是因为$dateFile是一个文本文件,它不知道如何转换它。任何帮助都将不胜感激
#!/bin/bash
$dateFile=grep "After :" myfile.txt | cut -d ':' -f 2,4
AGE_OF_MONTH="172800" # 172800 seconds = 2 Days
NOW=$( date +%s )
NEW_DATE=$(( NOW - AGE_OF_MONTH ))
if [ $( stat -c %Y "$dateFile" ) -lt ${NEW_DATE} ]; then
echo Date Less then 2 days
else
echo Date Greater then 2 days
fi
您的脚本中有几个错误。请尝试以下方法:
#!/bin/bash
# capture the seconds since epoch minus 2 days
NOW=`expr $(date '+%s') - 172800`
# read every line in the file myfile.txt
while read -r line;
do
# remove the unwanted words and leave only the date info
s=`echo $line | cut -d ':' -f 2,4`
# parse the string s into a date and capture the number of seconds since epoch
date=$(date -d "$s" '+%s')
# compare and print output
if [ $date -lt $NOW ]; then
echo "Date Less then 2 days, s=$s, date=$date, now=$NOW"
else
echo "Date Greater then 2 days, s=$s, date=$date, now=$NOW"
fi
done < myfile.txt
grep和while的示例:
#!/bin/sh
dateFile=`grep "After :" my.txt | cut -d ':' -f 2,4`
# capture the seconds since epoch minus 2 days
NOW=`expr $(date '+%s') - 172800`
echo "$dateFile" | while read -r line;
do
# parse the string s into a date and capture the number of seconds since epoch
date=$(date -d "$line" '+%s')
# compare and print output
if [ $date -lt $NOW ]; then
echo "Date Less then 2 days, s=$line, date=$date, now=$NOW"
else
echo "Date Greater then 2 days, s=$line, date=$date, now=$NOW"
fi
done
#!/bin/sh
# capture the seconds since epoch minus 2 days
NOW=`expr $(date '+%s') - 172800`
grep "After :" myFile.txt | cut -d ':' -f 2,4 | while read -r line;
do
# parse the string s into a date and capture the number of seconds since epoch
date=$(date -d "$line" '+%s')
# compare and print output
if [ $date -lt $NOW ]; then
echo "Date Less then 2 days, s=$line, date=$date, now=$NOW"
else
echo "Date Greater then 2 days, s=$line, date=$date, now=$NOW"
fi
done
希望这能澄清您的问题。您的脚本中有几个错误。请尝试以下方法:
#!/bin/bash
# capture the seconds since epoch minus 2 days
NOW=`expr $(date '+%s') - 172800`
# read every line in the file myfile.txt
while read -r line;
do
# remove the unwanted words and leave only the date info
s=`echo $line | cut -d ':' -f 2,4`
# parse the string s into a date and capture the number of seconds since epoch
date=$(date -d "$s" '+%s')
# compare and print output
if [ $date -lt $NOW ]; then
echo "Date Less then 2 days, s=$s, date=$date, now=$NOW"
else
echo "Date Greater then 2 days, s=$s, date=$date, now=$NOW"
fi
done < myfile.txt
grep和while的示例:
#!/bin/sh
dateFile=`grep "After :" my.txt | cut -d ':' -f 2,4`
# capture the seconds since epoch minus 2 days
NOW=`expr $(date '+%s') - 172800`
echo "$dateFile" | while read -r line;
do
# parse the string s into a date and capture the number of seconds since epoch
date=$(date -d "$line" '+%s')
# compare and print output
if [ $date -lt $NOW ]; then
echo "Date Less then 2 days, s=$line, date=$date, now=$NOW"
else
echo "Date Greater then 2 days, s=$line, date=$date, now=$NOW"
fi
done
#!/bin/sh
# capture the seconds since epoch minus 2 days
NOW=`expr $(date '+%s') - 172800`
grep "After :" myFile.txt | cut -d ':' -f 2,4 | while read -r line;
do
# parse the string s into a date and capture the number of seconds since epoch
date=$(date -d "$line" '+%s')
# compare and print output
if [ $date -lt $NOW ]; then
echo "Date Less then 2 days, s=$line, date=$date, now=$NOW"
else
echo "Date Greater then 2 days, s=$line, date=$date, now=$NOW"
fi
done
希望这能澄清您的问题。您的代码工作得非常好。非常感谢。只是想知道为什么你需要一个循环来读取文件中的每一行,因为grep命令会自动获取你需要的信息???为了回答你的问题,我编辑了我的答案。如果您对它感到满意,请选择它作为正确答案。您的代码工作正常。非常感谢。只是想知道为什么你需要一个循环来读取文件中的每一行,因为grep命令会自动获取你需要的信息???为了回答你的问题,我编辑了我的答案。如果您对它感到满意,请选择它作为正确答案。