如何将循环 curl 命令的输出写入文件

How to write output of looped curl command to file

我想在循环中每隔几秒 运行 一个 curl 命令,我希望将输出写入一个文件(每次 运行 时附加而不是覆盖)。我还必须从 CLI 运行

curl 命令如下

curl --location --request POST 'https://api.website.com/Auth/token'
--header 'Content-Type: application/x-www-form-urlencoded'
--header 'Cookie: blablabla'
--data-urlencode 'grant_type=password'
--data-urlencode 'username=username'
--data-urlencode 'password=password

我试过添加 while true;在开头做 sleep 2 && 并在结尾输出 > file.txt 但它不起作用。我敢肯定它可能很简单,但我对 bash 不是很好所以任何帮助都会很棒

你可以这样做:

while :; do
    curl --location --request POST 'https://api.website.com/Auth/token' \
        --header 'Content-Type: application/x-www-form-urlencoded' \
        --header 'Cookie: blablabla' \
        --data-urlencode 'grant_type=password' \
        --data-urlencode 'username=username' \
        --data-urlencode 'password=password'
    sleep 2 || break
done >> file.txt