Linux 验证是否已在shell中创建文件

Linux 验证是否已在shell中创建文件,linux,bash,file,shell,directory,Linux,Bash,File,Shell,Directory,我必须编写一个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

我必须编写一个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
我使用的想法是从目录中获取所有文件,并验证该文件是否已经存在。 如果是-显示消息并移动到下一个目录。 如果没有,我就‘等’。如果在我等待创建某个文件时,它会出现在所有文件的列表中,我会检查它

我的问题是,无论我从键盘上读到什么文件,我都会得到文件已经存在的消息。没有告诉我文件的名字

将f=`read filename`替换为正确用法read f或read-p filename:f。 find$dir-typef打印包含目录路径的完整文件名。因为您只需要basename,所以请替换这两行

    for i in `find $dir -type f`
        if [ $f=$i ]

[]中的每个运算符和操作数必须是单独的参数。因此,更换两条线路

    for i in `find $dir -type f`
        if [ $f=$i ]


您希望f='read filename'做什么?格式不正确;我用撇号代替了背面的记号。请参阅脚本的第二行。read函数不向stdout打印任何内容;而是将输入存储在$filename中$f将是空字符串。它应该从键盘读取。我需要把它放在“工作”和“我需要给他一个参数”之间。不,你不需要使用反勾号。只需写读文件名;它将变量$filename设置为用户键入的任何类型。使用$filename而不是$f。如果使用撇号,则不正确。但是我以前用过它,而且它很有效@KeithThompsonso我用f=read文件名试过了。我在第2行看到:filename:notfound@基思汤普森