For loop 如何编写bash循环以在所有图像目录中创建index.php

For loop 如何编写bash循环以在所有图像目录中创建index.php,for-loop,For Loop,我的任务是防止在大约300个网站上对图片进行索引 我的问题是如何创建for循环,使用shell命令“touch”在all/image目录中递归创建index.php?深度可以达到5个目录或更深,我真的不知道它能达到多深…我希望有人能帮助我,因为谷歌没有(很可能是我搜索错误的关键字的错误…) 提前感谢,, Jason非常简单,请尝试以下bash脚本: visit_dirs() { # create the needed file touch index.php

我的任务是防止在大约300个网站上对图片进行索引

我的问题是如何创建for循环,使用shell命令“touch”在all/image目录中递归创建index.php?深度可以达到5个目录或更深,我真的不知道它能达到多深…我希望有人能帮助我,因为谷歌没有(很可能是我搜索错误的关键字的错误…)

提前感谢,,
Jason

非常简单,请尝试以下bash脚本:

visit_dirs() 
{    
    # create the needed file
    touch index.php

    # if you don't need them, you can undo the action, etc.
    #rm index.php

    # just for informational purposes
    pwd
    ls index.php

    # the most important thing: recursion, so that this script will work with 
    # directories structures of any depth

    # first, the loop over all files (warning: this will probably break if files or
    # directories contain spaces in their name)
    for d in *; do

        # if d is directory...
        if [ -d $d ]
        then
            # visit it
            (cd $d; visit_dirs)
        fi

    done
}

# call the function        
visit_dirs

有没有办法添加“if index.php或index.*已经存在,跳过该目录”,因为我不希望目录中已经存在index.html,它会创建一个index.php来覆盖index.html,从而在内容原来所在的位置生成一个空白页……是的,在bash中检查文件存在很容易:if[-e index.*];然后echo“索引存在”;fi所以您应该在评论之前添加适当的检查#访问它您可能需要使用此选项:检查文件是否不存在是:if[!-e index.]