我需要一个shell脚本来监视命令中给出的所有文件夹 并将通知用户是否将在其中创建某个文件(名称为 该文件将从键盘上读取。)
我被允许使用简单的命令,所以不要inotify ... 这就是我到目前为止所做的事情:
#!/bin/bash
echo "Please enter a file you want to monitor: "
read file_monitor
#this is an infinite while
while [ 1 ] ; do
#using test -e to search for the file
test -e $file_monitor && echo "The file has been created!"
sleep 5
done
我必须找到一种方法来在创建文件时停止while,并且还要在命令行中给出的文件夹中搜索该文件。请有人帮帮我吗?
答案 0 :(得分:0)
要退出循环,请使用break
:
test -e $file_monitor && echo "The file has been created!" && break
我希望首先break
,然后在循环后echo
,或者@ mkemp6建议,直接使用test作为循环的条件。
要检查文件夹,只需循环浏览它们,然后检查每个文件夹中的文件。
break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n
levels. n must be ≥ 1. If n is greater than the number of enclosing loops, all
enclosing loops are exited.
答案 1 :(得分:0)
while ! test -e "$file_monitor"; do sleep 5; done
但是你最好使用像inotify这样的东西来监控相应的目录。
答案 2 :(得分:0)
#!/bin/bash
arr=$@
for i in $arr; do
if [ ! -d $i ]
then echo "The parameter $i is no a directory!"
exit 1
fi
done
echo -n "Please give file you want to monitor: "
read file_monitor
a=1
while [ $a -eq 1 ]; do
for i in $arr; do
cd $i
test -e $file_monitor && echo "The file has been created" && a=$((a+1))
cd ..
done
done
所以这就是我设法做到的。