【发布时间】:2020-12-04 05:03:48
【问题描述】:
我正在编写一个脚本,我需要以“8 月 14 日”格式处理今天日期的 ls -l 和 grep 文件。我将变量中的日期指定为dt=date "+%b %d",但我无法获取文件。
我可以通过什么方式做到这一点?
我的脚本:
#!/bin/bash
#1st Argument i.e., $1 will be file name and 2nd Argument i.e., $2 will be directory path where file is located.
cd $2
command=`ls -l | grep $dt| grep $1`
if
[ -z "$command" ]; then
echo "Critical : File $1 doesn't exists for $dt"
exit 2
else
echo "OK : File $1 exists for $dt"
exit 0
fi
运行脚本:
【问题讨论】:
-
将 grep 括在引号中并将其转换为命令:` ls -l | grep "$(date "+%b %d")" `
-
你能把你的问题说清楚一点吗?您是否尝试在文件名或文件内容中查找带有日期的文件?
-
@kaylum 我正在尝试检查是否存在具有今天时间跨度的特定内容。鲍勃的回答对我有用
标签: linux bash date scripting grep