Content #
-nt测试会判定一个文件是否比另一个文件更新。如果文件较新,那意味着其文件创建日期更晚。-ot测试会判定一个文件是否比另一个文件更旧。如果文件较旧,则意味着其文件创建日期更早:
$ cat check_file_dates.sh
#!/bin/bash
# Compare two file's creation dates/times
#
if [ $HOME/Downloads/games.rpm -nt $HOME/software/games.rpm ]
then
echo "The $HOME/Downloads/games.rpm file is newer"
echo "than the $HOME/software/games.rpm file."
#
else
echo "The $HOME/Downloads/games.rpm file is older"
echo "than the $HOME/software/games.rpm file."
#
fi
$
$ ./check_file_dates.sh
The /home/christine/Downloads/games.rpm file is newer
than the /home/christine/software/games.rpm file.
在脚本中,这两种测试都不会先检查文件是否存在。这是一个问题。试试下面的测试:
$ rm $HOME/Downloads/games.rpm
$
$ ./check_file_dates.sh
The /home/christine/Downloads/games.rpm file is older
than the /home/christine/software/games.rpm file.
这个小示例展示了如果有其中一个文件不存在,那么-nt测试返回的信息就不正确。在-nt或-ot测试之前,务必确保文件存在。
From #
Linux命令行与shell脚本编程大全