Bash脚本使用关联表以特定顺序处理文件

布林

我正在使用一部分文件名的bash脚本进行工作,以便对其进行正确处理。这是一个示例,文件名的名称中包含“ Catalog”,而该文件与另一个文件名中包含“ product”的文件配对。文件更多,需要按特定顺序进行处理,首先是名称中带有“目录”的文件,然后是名称中带有“ ProductAttribute”的文件。

尝试过其他事情,但仍然无法正常工作。在这里,我有一个与文件名相关联的哈希表

declare -A files 
files=( ["Catalog"]="product" ["ProductAttribute"]="attribute")

for i in "${!files[@]}"; do
    #list all files that contain "Catalog" & "ProductAttribute" in their filenames
    `/bin/ls -1 $SOURCE_DIR|/bin/grep -i "$i.*.xml"`;

    #once the files are found do something with them
    #if the file name is "Catalog*.xml" use "file-process-product.xml" to process it
    #if the file name is "ProductAttribute*.xml" use "file-process-attribute.xml" to process it
    /opt/test.sh /opt/conf/file-process-"${files[$i]}.xml" -Dfile=$SOURCE_DIR/$i

done
格伦·杰克曼

迭代关联数组的键没有固有的顺序:

$ declare -A files=( ["Catalog"]="product" ["ProductAttribute"]="attribute")
$ for key in "${!files[@]}"; do echo "$key: ${files[$key]}"; done 
ProductAttribute: attribute
Catalog: product

如果要按特定顺序进行迭代,则要对此负责:

$ declare -A files=( ["Catalog"]="product" ["ProductAttribute"]="attribute")
$ keys=( Catalog ProductAttribute )
$ for key in "${keys[@]}"; do echo "$key: ${files[$key]}"; done 
Catalog: product
ProductAttribute: attribute

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章