Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash:从ls命令格式化for循环内部的结果_Bash_For Loop_Ssh_Ls - Fatal编程技术网

Bash:从ls命令格式化for循环内部的结果

Bash:从ls命令格式化for循环内部的结果,bash,for-loop,ssh,ls,Bash,For Loop,Ssh,Ls,为什么echo“Line$Line”中的附加“Line”没有预先添加到for循环中的所有文件中? #!/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"@

为什么
echo“Line$Line”
中的附加“Line”没有预先添加到for循环中的所有文件中?

#!/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
脚本输出

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

for line in $result
$result
执行分词,以获取
$line
应设置为的每个元素。分词使用
$IFS
中的分隔符。在脚本前面,您将其设置为仅
。因此,此循环将迭代
$result
中以逗号分隔的数据。因为里面没有逗号,所以它只是一个元素

如果要按行拆分,请执行以下操作:

IFS="
"
for line in $result
IFS="
"
for line in $result