[英]Verify if a file was created shell
我必须编写一个shell脚本,该脚本将监视命令中给定的某些文件夹,并在其中创建某些文件时给出消息(该文件的名称将从键盘读取)。
谁能告诉我为什么这不起作用?
#!/bin/sh
f=`read filename`
isIn=0
for dir in $*
do
if [ ! -d $dir ]
then
echo $dir is not a directory.
fi
for i in `find $dir -type f`
do
if [ $f=$i ]
then
echo The file $f already exists.
isIn=1
break
fi
done
if [ $isIn -eq 0 ]
then
sleep 20
isIn=0
for i in `find $dir -type f`
do
if [ $f=$i ]
then
echo The file was created\!
isIn=1
break
fi
done
fi
if [ $isIn -eq 0 ]
then
echo The file was not created\!
fi
done
我使用的想法是,我从目录中取出所有文件,并验证文件是否不存在。 如果是,请显示消息并移至下一个目录。 如果没有,我就“等待”。 如果在我等待创建某个文件的时间里,它会出现在所有文件的列表中,然后我检查一下。
我的问题是,无论我从键盘上读取了什么文件,我都会收到消息“文件已存在”。 不告诉我文件名。
read f
或read -p filename: f
代替f=`read filename`
。 find $dir -type f
打印完整的文件名,包括目录路径。 由于只需要基本名称,因此请替换两行
for i in `find $dir -type f`
同
for i in `find $dir -type f -printf '%f\\n'`
[
]
每个运算符和操作数必须是一个单独的参数。 因此更换两条线
if [ $f=$i ]
同
if [ "$f" = $i ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.