gpt4 book ai didi

curl - 如何在 git bash 中执行预先设置的命令

转载 作者:行者123 更新时间:2023-12-03 22:08:23 29 4
gpt4 key购买 nike

我想在 git bash 上做一个 curl ,而不必输入它。即,我想通过在 Windows 中运行批处理脚本来执行“curl http://google.co.uk”。

这有可能吗?我正在尝试此操作,因为我正在尝试自动执行多个 curl 请求。

最佳答案

使用 git bash 进行测试的好主意!如果您想从一个脚本中多次调用 curl,我会使用 bash 脚本。

首先创建一个名为 doit.sh 的文件,并将您的 curl 命令放入其中:

#!/bin/env bash

curl http://google.co.uk
curl http://www.google.com
# More as needed...

保存文件,您应该能够通过在 Windows 资源管理器中双击它来运行它 - 或者甚至更好,通过在命令行上使用 ./doit.sh 运行它。

curl 对于这种测试来说是一个非常强大的工具,所以如果你愿意走 bash 脚本之路,你可以编写更复杂的测试 - 例如:

#!/bin/env bash

expected_resp_code="200"
echo "Making sure the response code is '$expected_resp_code'..."

actual_resp_code=$(curl --silent --head --write-out %{http_code} http://www.google.co.uk/ -o /dev/null)

if [[ ! "$actual_resp_code" = "$expected_resp_code" ]] ; then
echo "Expected '$expected_resp_code', but got code '$actual_resp_code'"
exit 1
fi

echo "Success!"
exit 0

在 git bash 中执行脚本可能如下所示:

you@yourmachine ~/Desktop
$ ./doit.sh
Making sure the response code is '200'...
Success!

如果您扩展到数十或数百个测试,您可能需要考虑转向另一种脚本语言,但对于一些一次性任务,从 bash 脚本调用 curl 可能会很适合您。

关于curl - 如何在 git bash 中执行预先设置的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151283/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com