可以按bash中的给定日期对过程进行排序吗?

问题描述 投票:0回答:1

我要做的就是按输入日期对流程进行排序。这是我保存日期的方式:

#!/bin/bash
echo "Insert the date yyyy-mm-dd"
read a
if ! [[ $date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]
then echo "Date is not valid"
elif b=`date --date=$a '+%Y-%m-%d'`
then 
????
fi

我现在如何显示流程?谢谢。

bash date terminal jobs ps
1个回答
0
投票

您可以使用ps -eo lstart解析日期,在这里您可以查看特定日期的过程

#!/bin/bash
declare -A MONTHS
#months Dictionary

MONTHS[01]=Jan
MONTHS[02]=Feb
MONTHS[03]=Mar
MONTHS[04]=Apr
MONTHS[05]=May
MONTHS[06]=Jun
MONTHS[07]=Jul
MONTHS[08]=Aug
MONTHS[09]=Sep
MONTHS[10]=Oct
MONTHS[11]=Nov
MONTHS[12]=Dec

echo "Insert the date yyyy-mm-dd"
read val
if [[ $val =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]
then
    echo "Date is valid!!"
    monthNumber=${val:5:2}
    correctDateFormat="${val:0:4}-${MONTHS[$monthNumber]}-${val:8:3}"
    ps -eo lstart,pid,cmd | awk {'print $5 "-" $2 "-" $3 " " $6 " " $7'} | grep "$correctDateFormat"

else
    echo "Date is not valid"
fi

输出样本:

2020-May-19 23 [crypto]
2020-May-19 230 [scsi_tmf_27]
2020-May-19 231 [scsi_eh_28]
2020-May-19 232 [scsi_tmf_28]
2020-May-19 233 [scsi_eh_29]
2020-May-19 234 [scsi_tmf_29]
2020-May-19 235 [scsi_eh_30]
2020-May-19 236 [scsi_tmf_30]
2020-May-19 237 [scsi_eh_31]
2020-May-19 238 [scsi_tmf_31]
2020-May-19 239 [scsi_eh_32]
2020-May-19 2390 /usr/lib/deja-dup/deja-dup-monitor
© www.soinside.com 2019 - 2024. All rights reserved.