2013-04-16 44 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