Question

How come the additional 'Line' insideecho "Line $line" is not prepended to all files inside the for loop?

#!/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 file contents ..

rob@laptop:~/scripts$ cat targets.csv 
server.com,root,passw0rd,/root/

script output ..

rob@laptop:~/scripts$ ./checkForHeapdump.sh 
Found a Heap dump! Possible OOM issue detected
Line file1.txt
file2.txt
Was it helpful?

Solution

The statement:

for line in $result

performs word splitting on $result to get each element that $line should be set to. Word splitting uses the delimiters in $IFS. Earlier in the script you set this to just ,. So this loop will iterate over comma-separated data in $result. Since there aren't any commas in it, it's just a single element.

If you want to split it by lines, do:

IFS="
"
for line in $result
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top