2016-08-19 71 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