Ubuntu 14.04.1。
我有一个 bash 脚本,由 cron 每 10 分钟调用一次,它基本上在子目录中查找文件,然后循环遍历并处理每个文件。但是如何检查是否没有找到文件?如果没有找到文件,我不想处理它们,也不想通过 cron 收到一封电子邮件,上面写着“找不到文件”,因为 cron 每 10 分钟运行一次。每天有 144 封电子邮件说“没有找到我不想收到的文件”。
这是我的基本脚本。
#!/bin/bash
# Cron requires a full path in $myfullpath
myfullpath=/home/comp/progdir
files=`ls $myfullpath/input/fedex*.xlsx`
# How do I check for no files found here and exit without generating a cron email?
for fullfile in $files
do
done
Run Code Online (Sandbox Code Playgroud)
谢谢!我什至不知道该用什么谷歌搜索这个。
编辑:我的脚本现在是这样的:
#!/bin/bash
# Script: gocronloop, Feb 5, 2015
# Cron requires a full path to the file.
mydir=/home/comp/perl/gilson/jimv/fedex
cd $mydir/input
# First remove spaces from filesnames in input/
find -name "* *" -type f | rename 's/ /-/g'
cd $mydir
shopt -s nullglob
if [ $? -ne 0 ]; then
echo "ERROR in shopt"
exit 1
fi
for fullfile in "$mydir"/input/fedex*.xlsx
do
# First remove file extension to get full path and base filename.
myfile=`echo "$fullfile"|cut -d'.' -f1`
echo -e "\nDoing $myfile..."
# Convert file from xlsx to xls.
ssconvert $myfile.xlsx $myfile.xls
# Now check status in $?
if [ $? -ne 0 ]; then
echo "ERROR in ssconvert"
exit 1
fi
perl $1 $mydir/fedex.pl -input:$mydir/$myfile.xls -progdir:$mydir
done
Run Code Online (Sandbox Code Playgroud)
第一件事:不要解析ls.
现在我们已经解决了这个问题,使用通配符,以及nullglob:
shopt -s nullglob
for fullfile in "$myfullpath"/input/fedex*.xlsx
do
#.......
done
Run Code Online (Sandbox Code Playgroud)
通常使用通配符,如果*不匹配任何内容,则保留原样。使用nullglob,它不会被替换,因此不会触发错误匹配。
例如:
$ bash -c 'a=(foo/*); echo ${a[@]}'
foo/*
$ bash -c 'shopt -s nullglob; a=(foo/*); echo ${a[@]}'
$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4480 次 |
| 最近记录: |