简体   繁体   English

date:bash上OSX 10上的非法时间格式,但是该命令有效

[英]date: illegal time format on OSX 10 on bash, but command works

I'm writing a bash script that reads and parses data from log lines, and it's trying to convert the format. 我正在编写一个bash脚本,该脚本从日志行读取和解析数据,并且正在尝试转换格式。 The script is long, but I'll focus on the error: 脚本很长,但我将重点放在错误上:

if [[ $line == *"Marked: Malicious, To:"* ]]; then

QUEUE_ID=`awk ' {print $13}'  <<< $line | sed 's/,//g'` # QUEUE ID... index of the array

F_TIME["X$QUEUE_ID"]=`awk '{print $3}'  <<< $line ` # Hour
F_DAY["X$QUEUE_ID"]=`awk '{print $2}'  <<< $line ` # Day
F_MONTH["X$QUEUE_ID"]=`awk '{print $1}'  <<< $line ` # Month

#Procesing and obtaining the diff

F_FULLTIME["X$QUEUE_ID"]="${F_DAY["X$QUEUE_ID"]}-${F_MONTH["X$QUEUE_ID"]}-$year-${F_TIME["X$QUEUE_ID"]}" # creating the time in desired format

s=`date -j -f "%d-%b-%Y-%H:%M:%S"  "${F_FULLTIME[X$QUEUE_ID]}"   +%s` # ERROR LINE!! Doesn't work!

#s=`date -j -f "%d-%b-%Y-%H:%M:%S"  "2-Mar-2016-22:24:33"   +%s` # Test line

echo ".....${F_FULLTIME["X$QUEUE_ID"]}....2-Mar-2016-22:24:33...."

echo  "time $s ...\n";

fi

Test process: I comment the test line (marked on the script), and I try to do conversion to variable s. 测试过程:我注释测试行(在脚本上标记),然后尝试转换为变量s。 This is the output for each key: 这是每个键的输出:

Failed conversion of ``2-Mar-2016-20:30:03'' using format ``%d-%b-%Y-%H:%M:%S''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
        [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

.....2-Mar-2016-20:30:03....2-Mar-2016-22:24:33....

Date is not taking the format. 日期未采用格式。 And as you can see, desired format and the format I have on the variable are pretty much same. 如您所见,所需的格式和我在变量上使用的格式几乎相同。

Test #2: I comment the error line, and I remove comment the test line. 测试#2:我注释错误行,并删除对测试行的注释。 This is the result: 结果如下:

.....2-Mar-2016-22:06:10....2-Mar-2016-22:24:33....
time 1456953873 ...

Works perfectly. 完美运作。 And of course text is aparently same as my variable on the first try. 当然,文本显然与第一次尝试中的变量相同。

The code looks OK, although overly complex. 该代码看起来不错,尽管过于复杂。 Try this simplified version, which should be a little easier to debug. 试试这个简化的版本,它应该更容易调试。 We're going to work with individual variables as much as possible, rather than array entries. 我们将尽可能使用单个变量,而不是数组条目。

if [[ $line == *"Marked: Malicious, To:"* ]]; then

    # run `awk` once
    fields=$(awk '{print $1, $2, $3, $13}' <<< "$line")
    read f_month f_day f_time queue_id <<< "$fields"
    queue_id=${queue_id//,/}

    # Is the X really necessary?

    # F_TIME["X$queue_id"]=$f_time
    # F_DAY["X$queue_id"]=$f_day
    # F_MONTH["X$queue_id"]=$f_month

    #Procesing and obtaining the diff
    f_fulltime="$f_day-$f-month-$year-$f_time"
    # F_FULLTIME["X$queue_id"]=$f_fulltime

    s=$(date -j -f "%d-%b-%Y-%H:%M:%S" "$f_fulltime" )    
fi

Some questions to ask: 一些问题要问:

  1. What are the exact values of queue_id , f_time , f_day , and f_month ? queue_idf_timef_dayf_month的确切值是什么? Use, eg, printf '%s' "$queue_id" | hexdump -C 使用例如printf '%s' "$queue_id" | hexdump -C printf '%s' "$queue_id" | hexdump -C to see if there are any hidden characters from $line that are confusing date . printf '%s' "$queue_id" | hexdump -C查看$line中是否有任何隐藏的字符使date混乱。

  2. Can you reproduce the problem by hardcoding the values of the pieces instead of just f_fulltime itself? 您可以通过对片段的值进行硬编码而不是仅使用f_fulltime本身来重现该问题吗?

  3. Can you reproduce the problem by hard-coding the value of line just prior to the call to awk ? 您能否在调用awk之前通过硬编码line的值来重现该问题?

Well.. I've clean the script.., Remove extra variables, declared arrays, and I found where the error comes.. but not sure why. 好吧..我已经清理了脚本..,删除了多余的变量,声明了数组,我发现了错误的出处..但不确定为什么。 The error comes on the variable $year. 错误来自变量$ year。

This is defined as: 定义为:

year=`date +"%Y"`

Then.. when I tried this conversion: 然后..当我尝试这种转换时:

s_fulltime["X$queue_id"]="$day-$month-$year-$time" >> date: illegal time format

But.. if I harcode "2016" then.. 但是..如果我将“ 2016”编码为..

year="2016"

All works fine.. 一切正常。

Why? 为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM