如果没有找到时间,它应该告诉下次可用时间

时间:2015-05-18 10:50:57

标签: bash function shell time scripting

是否有时间功能,可以检查时间,如果没有找到,请告诉我们下一个可用日期。

#!/bin/bash
read -p " enter the App name : " app
file="/logs/$app/$app.log"
if ! [ -f "$file" ]; then
    echo "$app.log not found, Please check correct log name in deployer"
    exit
fi

format=$(head -1 $file | awk '{print $1,$2,$3}')
format1=$(tail -1 $file | awk '{print $1,$2,$3}')
read -p " Enter the Date in this Format --'$format' : " first
until grep -q "$first" "$file"
do
  echo "Date not found"
  read -p " Enter the Date in this Format --'$format' : " first
done
final_first=$first
echo "logs are present till this '$format1' date, use this date if you want logs from Provided to latest."
read -p "Enter the end date : " end
until grep -q "$end" "$file"
do
  echo "Date not found"
  read -p "Enter the end date : " end
done
final_end=$end
cd /logs/$app
sed -n " /$final_first/,/$final_end/ "p $file >$app.txt
zip $app.zip $app.txt
rm $app.txt  

输出

./app_log_extract 
 enter the App name : cspt
 Enter the Date in this Format --'Sep 08 04:53:30' : Sep 08 05
Date not found
 Enter the Date in this Format --'Sep 08 04:53:30' : 

现在我想和Date一起发现它应该告诉,Log存在于9月08日20:09:39

日志文件是

Sep 08 20:09:39 INFO  [main] common.ComponentManagerImpl - added model class 'com.ge.oilandgas.cspt.model.RoleValidModel' under key 'role-valid'
Sep 08 20:09:39 INFO  [main] common.ComponentManagerImpl - added dispatcher class 'com.ge.casper.servlet.GenericDispatcher' for key 'html'
Sep 08 20:09:39 INFO  [main] servlet.CasperServlet - CASPER is online.
Sep 09 10:57:20 INFO  [http-0.0.0.0-8086-9] model.SessionValidModel - CSPT - User null is logged on!
Sep 09 10:58:49 FATAL [http-0.0.0.0-8086-3] np.dss - UserBeanRepository.query->error in creation of document:

AWK工具主要用于以有用的方式报告某些数据。如果没有这些BEGIN和END块,输出将意味着更少,未定义此端块。调试此代码

./test
 enter the App name : cspt
++ file=/logs/cspt/cspt.log
++ '[' -f /logs/cspt/cspt.log ']'
+++ head -1 /logs/cspt/cspt.log
+++ awk '{print $1,$2,$3}'
++ format='Sep 08 04:53:30'
+++ tail -1 /logs/cspt/cspt.log
+++ awk '{print $1,$2,$3}'
++ format1='at java.lang.Thread.run(Thread.java:662) '
++ read -p ' Enter the Date in this Format --'\''Sep 08 04:53:30'\'' : ' first
 Enter the Date in this Format --'Sep 08 04:53:30' : Sep 08 21:19:19
++ grep -q 'Sep 08 21:19:19' /logs/cspt/cspt.log
++ read month_in date_in time_in
++ awk '
BEGIN {
    split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month)
    for (i in month) 
        {
        month_nums[month[i]]=i
        echo "$i"    
        }
}

(month_nums[$1] >= month_in) && ($2>=date_in) && ($3>=time_in)

{
   echo "logs are available from    "
    exit 0;   
}
' month_in=Sep date_in=08 time_in=04:53:30 /logs/cspt/cspt.log
++ echo Sep
Sep
++ echo 08
08
++ echo 04:53:30
04:53:30

1 个答案:

答案 0 :(得分:0)

这样的事情可能有所帮助:

format="Sep 08 05" # actually, this is swt by user input.

read month_in date_in time_in <<< "$format"
awk '
BEGIN {
    split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month)
    for (i in month) {
        month_nums[month[i]]=i
    }
}

(month_nums[$1] >= month_nums[month_in]) && ($2>=date_in) && ($3>=time_in){
    print "logs are available from " $1, $2, $3;
    exit 0;   
}
' "month_in=$month_in" "date_in=$date_in" "time_in=$time_in" logfile