2013-04-16 74 views
0

爲什麼echo "Line $line"內的附加'Line'不會被預置到for循環中的所有文件?
Bash:從ls命令內部爲循環格式化結果

#!/bin/bash 

INPUT=targets.csv 
IFS="," 

[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; } 
while read target user password path 
do 
    result=$(sshpass -p "$password" ssh -n "$user"@"$target" ls "$path"*file* 2>/dev/null) 

    if [ $? -ne 0 ] 
    then 
      echo "No Heap dumps detected." 
    else 
      echo "Found a Heap dump! Possible OOM issue detected" 
      for line in $result 
      do 
        echo "Line $line" 
      done 
    fi 

done < $INPUT 

.csv文件內容..

[email protected]:~/scripts$ cat targets.csv 
server.com,root,passw0rd,/root/ 

腳本輸出..

[email protected]:~/scripts$ ./checkForHeapdump.sh 
Found a Heap dump! Possible OOM issue detected 
Line file1.txt 
file2.txt 

回答

0

聲明:

for line in $result 

$result進行分詞得到è應該設置爲$line的ach元素。分詞使用$IFS中的分隔符。在腳本早期,您將其設置爲,。所以這個循環將遍歷$result中的逗號分隔數據。由於它中沒有任何逗號,它只是一個單獨的元素。

如果您想通過線來分割它,這樣做:

IFS=" 
" 
for line in $result 
+0

好一個Barmar! – bobbyrne01