如何在文件夹中查找特定文件并在它们存在时执行操作

How to find specific files in folders and do an operation in case they exist

我在 sh shell 中编写基本脚本时遇到了一些困难。 不过我想做的很简单:

我想做一个sh脚本(也可以是csh),它会查看多个文件夹,对于每个包含我感兴趣的文件的文件夹,它应该做一个特定的操作,将相应的文件名粘贴到带有 rdseed 命令的 sh 脚本。

我在 sh shell 中编写但不起作用的脚本是:

for dir in EV*
do
    echo $dir
    cd $dir 
    if [ -f GEFLE* = true ];
    then
        set dataless = gur_ini_dataless.seed
        for file GEFLE*
        do
            echo "rdseed -d -o 2 -f "$file " -g " $dataless >> runmseed2ahGEFLE.sh
        done
    else
       echo "File does not exists"
    fi
    sleep 0.5
    cd ..
done

有人知道解决办法吗?

请试试这个...我正在向这些行添加一些评论...

#!/bin/sh

for dir in EV*
do
    echo $dir
    cd $dir
    if [ -f GEFLE* ]   # true if at least one FILE named "GEFLE*" exists
    then
        dataless=gur_ini_dataless.seed   # no `set`, no spaces
        for file in GEFLE* # will match all FILES/DIRS/... that start with "GEFLE"
        do
            echo "rdseed -d -o 2 -f $file -g $dataless" >> runmseed2ahGEFLE.sh  # vars are substituted in double quoted strings
        done
    else
       echo "File does not exists"
    fi
    cd ..
done

请注意,这只会查看目录的一层。如果你需要一些恢复,你最好使用像

这样的东西
for dir in `find . -type d -name 'EV*'`; do
    # ...
done

我的表达方式是:

for f in `find EV* -name GEFLE* -type f`; do
    echo "rdseed -d -o 2 -f ./$f -g gur_ini_dataless.seed >> ./`dirname $f`/runmseed2ahGEFLE.sh"
done