Bash 在 vim 中编写的脚本与在终端中的工作方式不同!为什么?

Bash script written in vim works differently than in the terminal! WHY?

这个想法是bash脚本,它使用“common_ports.txt”文件中的端口枚举网站的内部端口(SSRF)并输出每个端口的端口和“内容长度”因此。

即curl请求:

$ curl -Is "http://10.10.182.210:8000/attack?port=5000"
 HTTP/1.0 200 OK 
 Content-Type: text/html; charset=utf-8
 Content-Length: 1035
 Server: Werkzeug/0.14.1 Python/3.6.9
 Date: Sat, 16 Oct 2021 13:02:27 GMT

为了获取内容长度,我使用了 grep:

$ curl -Is "http://10.10.182.210:8000/attack?port=5000" | grep  "Content-Length"
  Content-Length: 1035

到目前为止一切正常。但是当我在 vim 中编写它以自动执行该过程时,我得到了奇怪的输出。

这是我的完整 bash 脚本:

#!/bin/bash
file="./common_ports.txt"


while IFS= read -r  line 
do
        response=$(curl -Is "http://10.10.182.210:8000/attack?port=$line")
         
        len=$(echo $response | grep  "Content-Length:")
        echo "$len"
done < "$file"

这是输出:

$ ./script.sh
 Date: Sat, 16 Oct 2021 13:10:35 GMT9-8
 Date: Sat, 16 Oct 2021 13:10:36 GMT9-8
^C

输出response变量的最后一行。谁能解释一下为什么?? 提前致谢!!!

您需要将 $response 括在双引号内。

len=$(echo "$response" | grep  "Content-Length:")