我有一个文件夹列表。在每个文件夹中,可能有或可能没有一个或两个特定扩展名的文件(特别是".fastq“扩展名)。我想要做的是编写一个脚本,输入这些文件夹,检查fastq文件是否存在。我所写的脚本如下:
#!/bin/bash
echo Beginning operation
echo
for folder in $(ls -d */);
do
cd ${folder%%/};
myfolder=${folder::-1}
echo
echo Entered $myfolder
fastq_test=(´find ./ -maxdepth 1 -name "*.fastq"´)
if [ ${#fastq_test[@]} -gt 0 ];
then
echo There are FASTQ files, will not do the operation
else
echo There are no FASTQ files, proceeding with operation
#operation here
fi
cd ..
echo Exited $myfolder
echo
done
echo Ended Operation我的问题是这个脚本不工作-它在没有FASTQ文件的文件夹中找到FASTQ文件。还有更好的办法吗?
发布于 2021-11-17 15:34:57
在bash中,您可以:
#!/bin/bash
for dir in ./*/
do
if compgen -G "$dir*.fastq" > /dev/null
then
echo 'There are FASTQ files, will not do the operation'
elif pushd "$dir" > /dev/null
then
echo 'There are no FASTQ files, proceeding with operation'
# operations...
popd > /dev/null
fi
done注:
dash.
compgen -G 'glob'中的glob只匹配目录,而开始时的./是一个安全,与以./*/开头的名称相比,这是测试与glob匹配的文件/目录的存在的一种很好的bash方式。尽管如此,在您的情况下,它的输出没有任何价值(通常用于全局匹配,因为您将需要使用name).pushd /dir /./ ././技巧来检测文件/dir././中的换行符;然后使用popd返回到前一个目录。pushd popd很有用,但是它们的输出很烦人。https://stackoverflow.com/questions/70006197
复制相似问题