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

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

提问人:Elkhan 提问时间:10/16/2021 最后编辑:CyrusElkhan 更新时间:10/16/2021 访问量:50

问:

这个想法是 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

bash curl 脚本 引号

评论

1赞 romainl 10/16/2021
这与 Vim 无关。

答:

1赞 emptyhua 10/16/2021 #1

您需要将内部的双引号换行。$response

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