在我的 shell 脚本中,需要将 j2re-1.4.2_10-fcs rpm pkg 的创建日期与某些文件的创建日期进行比较,并验证文件创建是否比 rpm pkg 创建更新
我的问题是:有人知道如何比较文件和 rpm 创建日期吗?为了确定文件日期是旧还是新,rpm 创建日期
rpm -qi j2re-1.4.2_10-fcs | grep "Install Date"
Install Date: Mon 20 Sep 2010 02:01:04 PM IST Build Host: localhost.localdomain
ls -ltr /etc/hosts
-rw-r--r-- 1 root root 563 Sep 7 10:28 /etc/hosts
答案1
Unix 不存储创建日期,但您可以比较修改(或访问或更改日期)。
rpmdate=$(rpm -qi j2re-1.4.2_10-fcs | sed -n '/Install Date/ s/Install Date:\(.*\)Build Host:.*/\1/p')
rpmdate=$(date -d "$d" +%s)
filedate=$(stat --printf=%Y /etc/hosts)
if (( filedate > rpmdate ))
then
echo "File is newer than RPM"
echo
echo "File is NOT newer than RPM"
fi
答案2
这应该可行。满足您的需求。
#!/bin/bash
rpm_path="$1"
file_path="$2"
file_date_row=`ls -l --time-style=+%Y%m%d $file_path`
file_date=`echo "$file_date_row" | awk -F" " '{print $6}'`
rpm_date_row=`rpm -qi $rpm_path | grep "Install Date"`
rpm_year=`echo "$rpm_date_row" | cut -d " " -f 6`
rpm_month=`echo "$rpm_date_row" | cut -d " " -f 5`
rpm_day=`echo "$rpm_date_row" | cut -d " " -f 4`
case $rpm_month in
Jan)
rpm_date="$rpm_year""01""$rpm_day"
;;
Feb)
rpm_date="$rpm_year""02""$rpm_day"
;;
Mar)
rpm_date="$rpm_year""03""$rpm_day"
;;
Apr)
rpm_date="$rpm_year""04""$rpm_day"
;;
May)
rpm_date="$rpm_year""05""$rpm_day"
;;
Jun)
rpm_date="$rpm_year""06""$rpm_day"
;;
Jul)
rpm_date="$rpm_year""07""$rpm_day"
;;
Aug)
rpm_date="$rpm_year""08""$rpm_day"
;;
Sep)
rpm_date="$rpm_year""09""$rpm_day"
;;
Oct)
rpm_date="$rpm_year""10""$rpm_day"
;;
Nov)
rpm_date="$rpm_year""11""$rpm_day"
;;
Dec)
rpm_date="$rpm_year""12""$rpm_day"
;;
esac
if [[ "$rpm_date" > "$file_date" ]]
then
echo "The RPM is newer than the file"
elif [[ "$rpm_date" < "$file_date" ]]
then
echo "The file is newer than the RPM"
else
echo "The file and the RPM have the same date"
fi