【问题标题】:Iterate shell script over list of subdirectories在子目录列表上迭代 shell 脚本
【发布时间】:2019-02-27 15:47:23
【问题描述】:

我有一个文件夹(我们称之为 folder1,它只包含子目录。我想编写一个 shell 脚本,在每个子目录上迭代几个 python 脚本。事实上,我必须输入每个子目录的绝对路径在脚本中,但我希望能够 cdfolder1 并简单地从那里运行 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

然后我可以通过打开终端 (Mac OSX v 10.13.6) 并运行 sh path/to/shellscript.sh 来运行它。我希望脚本早期的 arr 声明能够根据我所在的任何 cwd 的内容自动填充。我发现 this 有用的链接并且能够在终端中将其作为独立命令运行,但不能弄清楚如何将其合并到 shell 脚本中。有什么建议吗?

【问题讨论】:

    标签: bash shell loops ls


    【解决方案1】:
        for dir in ./* ./**/*    # list directories in the current directory
        do
            python $dir
        done
    

    ./* 是目录中的文件,./**/* 是子文件夹中的文件。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 2017-01-12
      相关资源
      最近更新 更多