简体   繁体   English

嵌套for bash脚本中的目录循环

[英]Nested for loops for directory in bash script

for d in dirlist;  
do  
    cd $d  
    echo $PWD "primul"  
    for p in *.HEIC;  
    do  
        echo $PWD "din al doilea"  
        if [ -e "COPY ${p%.*}.jpg" ]; then  
            echo "COPY ${p%.*}.jpg exists"  
        else  
            convert $p "COPY ${p%.*}.jpg"  
            echo "COPY ${p%.*}.jpg created"  
        fi  
    done  
done 

I have written a script to convert all files that are .HEIC to .jpg. 我编写了一个脚本,将所有.HEIC文件转换为.jpg。 Now I have a structure of folders that is 现在我有一个文件夹结构

/main/2019/01  
/main/2019/03  
/main/2018/12  
/main/2018/11  
/main/2018/10  
/main/2018/09  

And I want to run the second for in each directory. 我想在每个目录中运行第二个。

What I get when I run the commands below is only a list of directories and a mention that there are no files to convert. 当我运行以下命令时,我得到的只是目录列表,并且提到没有要转换的文件。

How do I change the directory using the first for and run the second for in that directory? 如何使用第一个for更改目录并在该目录中运行第二个for目录?

cd - changes to the previous directory. cd -更改到上一个目录。 That one solution to put it after the first done (second for cycle). 在第一次done之后将其置于一个解决方案(第二个用于循环)。

pushd and popd using stack to handle directory changes 使用堆栈pushdpopd以处理目录更改
check out: https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html 签出: https : //www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM