【问题标题】:How to write output of looped curl command to file如何将循环 curl 命令的输出写入文件
【发布时间】:2022-01-07 16:19:24
【问题描述】:

我想在循环中每隔几秒运行一次 curl 命令,并且我希望将输出写入文件(每次运行时都不会覆盖)。我还必须从 CLI 运行它

Curl 命令如下所示

curl --location --request POST 'https://api.website.com/Auth/token'
--header '内容类型:应用程序/x-www-form-urlencoded'
--header 'Cookie: blablabla'
--data-urlencode 'grant_type=password'
--data-urlencode '用户名=用户名'
--data-urlencode '密码=密码

我尝试添加一个while true;在开头做 sleep 2 && 和 > file.txt 在最后输出,但它不起作用。我敢肯定它可能很简单,但我不太擅长 bash,所以任何帮助都会很棒

【问题讨论】:

    标签: bash curl stdout


    【解决方案1】:

    你可以这样做:

    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
    

    【讨论】:

    • 谢谢你试试
    • 有没有办法让每个响应也打印一个时间戳?
    • 您可以使用命令替换来捕获输出并使用类似echo "[$(date)] $output"的内容。
    猜你喜欢
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2012-02-18
    • 2021-02-26
    • 2020-06-17
    • 1970-01-01
    • 2023-02-08
    • 2018-06-18
    相关资源
    最近更新 更多