BASH:IF / ELIF错误触发

时间:2017-06-02 20:41:53

标签: bash if-statement conditional

我不仅仅是针对以下情况的答案,而是请给出解释。 有2个文件 - NEW和OLD:

touch NEW ; 
touch -t $(date -d '3 week ago' +%Y%m%d%H%M) OLD

现在我根据新文件与旧文件的比较结果使脚本输出不同的消息:

#!/bin/bash                                                                                                           
if [[ "NEW" -ot "OLD" ]]                                                          
        then echo "file is old"                                                                                       
elif [[ "NEW" -nt "OLD" ]]                                                        
        then echo "file is new"                                                                                       
fi

但无论哪种条件是正确的,"文件都是旧的"将始终打印。

与IF ELSE FI语句相同的情况。

档案信息:

    stat NEW
  File: 'NEW'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd05h/64773d    Inode: 3227449730  Links: 1
Access: (0644/-rw-r--r--)  Uid: (48935/ UNKNOWN)   Gid: (48935/ UNKNOWN)
Access: 2017-06-02 20:53:27.461237303 +0000
Modify: 2017-06-02 20:53:27.461237303 +0000
Change: 2017-06-02 20:53:27.461237303 +0000
 Birth: -

stat OLD
  File: 'OLD'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd05h/64773d    Inode: 3227452336  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (48935/ UNKNOWN)   Gid: (48935/ UNKNOWN)
Access: 2017-06-02 20:29:05.607617334 +0000
Modify: 2017-05-12 20:20:00.000000000 +0000
Change: 2017-06-02 20:23:42.872363548 +0000
 Birth: -

我哪里错了?

1 个答案:

答案 0 :(得分:3)

您正在比较变量 $NEW中指定的文件和变量 $OLD中指定的文件,而不是两个名为{的文件{1}}和NEW

以下表现符合预期:

OLD