Bash shell脚本在文件夹结构中循环并执行shell脚本

Bash shell脚本在文件夹结构中循环并执行shell脚本,bash,shell,sh,Bash,Shell,Sh,我在文件夹stucrtre中找到了以下内容 /var/data/2017/01/$day/文件 我想在所有月份/天内执行shell脚本到for循环,并对所有文件运行shell脚本 DIR_PROC = /var/data/2017/01/$day/$file for day in {1..30} do echo "processing file $file sh /opt/myscript.sh /var/data/2017/1/$day/$fil

我在文件夹stucrtre中找到了以下内容

/var/data/2017/01/$day/文件

我想在所有月份/天内执行shell脚本到for循环,并对所有文件运行shell脚本

DIR_PROC = /var/data/2017/01/$day/$file
for day  in  {1..30}   do
        echo "processing file  $file
        sh  /opt/myscript.sh   /var/data/2017/1/$day/$file

    done
我不确定这里的逻辑,我错过了列出如何获得它的文件

有什么建议吗

dir_proc=/var/data/2017/01
# maybe {1..31} because month can have 31 days
for day in {1..30}; do
    # check directory exist || otherwise continue with next day
    [[ -e $dir_proc/$day ]] || continue
    for file in "$dir_proc/$day/"*; do
        # check file exist      || otherwise continue with next file
        [[ -e $file ]] || continue
        # do something with "$file"
    done
done
按月份编辑:

dir_proc=/var/data/2017
for month in {01..12}; do
    for day in {01..31}; do
        # check directory exist || otherwise continue with next day
        [[ -e $dir_proc/$month/$day ]] || continue
        for file in "$dir_proc/$month/$day/"*; do
            # check file exist      || otherwise continue with next file
            [[ -e $file ]] || continue
            # do something with "$file"
        done
    done
done
或更短,一个用于

dir_proc=/var/data/2017
for month_day in {01..12}/{01..31}; do
    # check directory exist || otherwise continue with next day
    [[ -e $dir_proc/${month_day} ]] || continue
    for file in "$dir_proc/${month_day}/"*; do
        # check file exist      || otherwise continue with next file
        [[ -e $file ]] || continue
        # do something with "$file"
    done
done
按月份编辑:

dir_proc=/var/data/2017
for month in {01..12}; do
    for day in {01..31}; do
        # check directory exist || otherwise continue with next day
        [[ -e $dir_proc/$month/$day ]] || continue
        for file in "$dir_proc/$month/$day/"*; do
            # check file exist      || otherwise continue with next file
            [[ -e $file ]] || continue
            # do something with "$file"
        done
    done
done
或更短,一个用于

dir_proc=/var/data/2017
for month_day in {01..12}/{01..31}; do
    # check directory exist || otherwise continue with next day
    [[ -e $dir_proc/${month_day} ]] || continue
    for file in "$dir_proc/${month_day}/"*; do
        # check file exist      || otherwise continue with next file
        [[ -e $file ]] || continue
        # do something with "$file"
    done
done

如果要为目录结构中的所有文件运行脚本,只需使用
find

find /var/data/2017/01 -type f -exec sh /opt/myscript.sh {} \;
或者使用
处理…
行(使用GNU查找):


如果只想在树中的某些子目录上运行,则需要使用
-path
-name
-prune
来限制匹配。

如果要为目录结构中的所有文件运行脚本,只需使用
查找

find /var/data/2017/01 -type f -exec sh /opt/myscript.sh {} \;
或者使用
处理…
行(使用GNU查找):


如果您只想在树中的某些子目录上运行,则需要使用
-path
-name
-prune
来限制匹配。

什么是“错过文件列表”,什么工作不正常?我的意思是,当我每天循环时,我会在那里有多个文件来针对每个文件运行shell脚本,从第一天到第二天,我如何将文件添加到循环中,等等,你说的“错过文件列表”是什么意思,什么是不工作的?我的意思是,当我每天循环时,我会有多个文件在那里运行shell脚本来针对每个文件,我如何将文件添加到循环中,一旦从第一天开始,我移动到第二天,所以旁观者很好,如果我想把这个月本身作为变量?所以这将是第{1..12}个月?顺便说一句,您不需要
[[-e$file]]]
测试<代码>$file作为“$dir\u proc/$month/$day/”中文件的
输出的一部分而存在。当然,除非它在短时间内被删除。即使是月和日的测试也有些冗余,除了从检查不存在的目录中的文件中节省一些处理器周期之外。@Deathgrip,这是为了处理没有文件的情况:glob没有展开,默认情况下,
file='dir/*'
也可以使用
shopt-s nullglob
,我总是忘记这一点。看起来不错,如果我想将月份本身作为var?所以这将是第{1..12}个月?顺便说一句,您不需要
[[-e$file]]]
测试<代码>$file
作为“$dir\u proc/$month/$day/”中文件的
输出的一部分而存在。当然,除非它在短时间内被删除。即使是月和日的测试也有些冗余,除了从检查不存在的目录中的文件中节省一些处理器周期之外。@Deathgrip,这是为了处理没有文件的情况:glob没有展开,默认情况下,
file='dir/*'
也可以使用
shopt-s nullglob
,我总是忘记这一点。