使用Bash在for循环中列出所有带有前缀的文件 [英] List all the files with prefixes from a for loop using Bash

查看:262
本文介绍了使用Bash在for循环中列出所有带有前缀的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的bash脚本的一小部分,但很完整,如果有存储数组的前缀,它将查找并输出mydir中的所有文件.我注意到的奇怪之处在于,如果我从脚本中删除"-maxdepth 1 -name",则该脚本可以完美地工作,否则它只会为我提供带有数组中第一个元素前缀的文件.

如果有人向我解释这一点将有很大帮助.抱歉,如果我做的某件事显然很愚蠢.我是脚本的新手.

#!/bin/sh
DIS_ARRAY=(A B C D)
echo "Array is : "
echo ${DIS_ARRAY[*]}
for dis in $DIS_ARRAY
do
    IN_FILES=`find /mydir  -maxdepth 1 -name "$dis*.xml"`
    for file in $IN_FILES
    do
    echo $file
    done
done

输出:

/mydir/Abc.xml
/mydir/Ab.xml
/mydir/Ac.xml

预期输出:

/mydir/Abc.xml
/mydir/Ab.xml
/mydir/Ac.xml
/mydir/Bc.xml
/mydir/Cb.xml
/mydir/Dc.xml

解决方案

无论哪种方式,循环都会中断.

IN_FILES=`find mydir  -maxdepth 1 -name "$dis*.xml"`

有效,而

IN_FILES=`find mydir "$dis*.xml"`

不是因为在第一个中指定了-name.在第二个文件中,find列出了mydir中的所有文件.如果您将第二个更改为

IN_FILES=`find mydir -name "$dis*.xml"`

您将看到循环无法正常工作.

如评论中所述,您当前使用的$DIS_ARRAY语法只会为您提供数组的第一个元素.

尝试将循环更改为此:

for dis in "${DIS_ARRAY[@]}"

在您的特定情况下,扩展名周围的双引号不是严格必需的,但如果数组中的元素包含空格,则必须使用双引号,如以下测试所示:

#!/bin/bash

arr=("a a" "b b")

echo using '$arr'
for i in $arr; do echo $i; done
echo using '${arr[@]}'
for i in ${arr[@]}; do echo $i; done
echo using '"${arr[@]}"'
for i in "${arr[@]}"; do echo $i; done

输出:

using $arr
a
a
using ${arr[@]}
a
a
b
b
using "${arr[@]}"
a a
b b

有关更多详细信息,请参见此相关问题.

Here is a small[but complete] part of my bash script that finds and outputs all files in mydir if the have the prefix from a stored array. Strange thing I notice is that this script works perfectly if I take out the "-maxdepth 1 -name" from the script else it only gives me the files with the prefix of the first element in the array.

It would be of great help if someone explained this to me. Sorry in advance if there is some thing obviously silly that I'm doing. I'm relatively new to scripting.

#!/bin/sh
DIS_ARRAY=(A B C D)
echo "Array is : "
echo ${DIS_ARRAY[*]}
for dis in $DIS_ARRAY
do
    IN_FILES=`find /mydir  -maxdepth 1 -name "$dis*.xml"`
    for file in $IN_FILES
    do
    echo $file
    done
done

Output:

/mydir/Abc.xml
/mydir/Ab.xml
/mydir/Ac.xml

Expected Output:

/mydir/Abc.xml
/mydir/Ab.xml
/mydir/Ac.xml
/mydir/Bc.xml
/mydir/Cb.xml
/mydir/Dc.xml

解决方案

The loop is broken either way. The reason why

IN_FILES=`find mydir  -maxdepth 1 -name "$dis*.xml"`

works, whereas

IN_FILES=`find mydir "$dis*.xml"`

doesn't is because in the first one, you have specified -name. In the second one, find is listing all the files in mydir. If you change the second one to

IN_FILES=`find mydir -name "$dis*.xml"`

you will see that the loop isn't working.

As mentioned in the comments, the syntax that you are currently using $DIS_ARRAY will only give you the first element of the array.

Try changing your loop to this:

for dis in "${DIS_ARRAY[@]}"

The double quotes around the expansion aren't strictly necessary in your specific case, but required if the elements in your array contained spaces, as demonstrated in the following test:

#!/bin/bash

arr=("a a" "b b")

echo using '$arr'
for i in $arr; do echo $i; done
echo using '${arr[@]}'
for i in ${arr[@]}; do echo $i; done
echo using '"${arr[@]}"'
for i in "${arr[@]}"; do echo $i; done

output:

using $arr
a
a
using ${arr[@]}
a
a
b
b
using "${arr[@]}"
a a
b b

See this related question for further details.

这篇关于使用Bash在for循环中列出所有带有前缀的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆