Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在子目录列表上迭代shell脚本_Bash_Shell_Loops_Ls - Fatal编程技术网

Bash 在子目录列表上迭代shell脚本

Bash 在子目录列表上迭代shell脚本,bash,shell,loops,ls,Bash,Shell,Loops,Ls,我有一个文件夹,我们称之为folder1,它只包含子目录。我想编写一个shell脚本,在每个子目录上迭代几个python脚本。实际上,我必须键入脚本中每个子目录的绝对路径,但我希望能够cd到folder1,然后从那里运行shell脚本,并让它自动迭代子目录,不管它们的名称或folder1的位置如何 保存为shellscript.sh的当前代码: #! /bin/sh declare -a arr=("/path/folder1/subdir1" "/path/folder1/subdir2"

我有一个文件夹,我们称之为folder1,它只包含子目录。我想编写一个shell脚本,在每个子目录上迭代几个python脚本。实际上,我必须键入脚本中每个子目录的绝对路径,但我希望能够cd到folder1,然后从那里运行shell脚本,并让它自动迭代子目录,不管它们的名称或folder1的位置如何

保存为shellscript.sh的当前代码:

#! /bin/sh

declare -a arr=("/path/folder1/subdir1"  "/path/folder1/subdir2" "/path/folder1/subdir3" "/path/folder1/subdir4")

for i in "${arr[@]}"
do
    echo "$i"
    python /path/to/pythonscript1.py "$i"
    python /path/to/pythonscript2.py "$i"
done
然后我可以打开终端MacOSX v10.13.6并运行sh path/to/shellscript.sh来运行它。我希望脚本的早期arr声明能够根据我所处的cwd的内容自动填充。我找到了有用的链接,并能够在终端中作为独立命令运行该链接,但不知道如何将其合并到shell脚本中。有什么建议吗

    for dir in ./* ./**/*    # list directories in the current directory
    do
        python $dir
    done
./*是目录中的文件,./***是子文件夹中的文件

确保目录中只有python文件,它将运行该目录中的所有文件