2016-08-19 88 views
0

我是shell脚本编程新手。我尝试了下面的代码,首先检查指定目录中是否存在任何XML文件。如果找到了,那么我需要将XML文件存储到数组中以处理数据。但下面的行不起作用。我究竟做错了什么?请建议正确的方法。从目录访问xml文件

if [ -f ${Input_Path}/ABC/*.XML ] 
then 
    arr=(${Input_Path}/ABC/*.XML) 
    for i in "${arr[@]}"; 
     do 
      ....... 
     done 
+0

数组很可能不是一个好地方来存储你不指定SHELL你用什么 –

+0

庆典@ StefanHegny – siva

+0

HTTPS的XML。(庆典):/ /stackoverflow.com/questions/29350318/reading-an-array-from-a-file-in-bash-not-found-errors-using-cat –

回答

0

您可以如下使用;

#!/bin/bash 
Input_Path=$1 
myarray=() 

while IFS= read -rd '' files; do 
myarray+=("$files") 
#do something; 
done < <(find ${Input_Path} -type f -name '*.xml' -print0) 

printf '%s\n' "${myarray[@]}" 

运行如下;

./script <yourInputPath> 

例如:

[email protected]:/tmp/1$ ./test.sh /tmp/1 
/tmp/1/2.xml 
/tmp/1/4.xml 
/tmp/1/3.xml 
/tmp/1/1.xml