You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本while循环中如何读取多变量适配Curl请求

解决方法

核心修改点

  1. 调整while read语句,使其同时读取文件的两列数据到指定变量
  2. 修正curl参数中变量无法解析的问题

修改后的完整脚本

while read p q; do
curl -X POST \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --header 'HTTP_X_MERCHANT_CODE: STA' \
  --header 'AK-Client-IP: 135.71.173.56' \
  --header 'Authorization: Basic qwrewrereererweer' \
  -d "{
    \"request_details\": [
        {
            \"id\": \"$p\",
            \"id_id\": \"$q\",
            \"reason\": \"Pickup reattempts exhausted\"
        }
    ]
}" api.stack.com/ask
done < file.txt

关键说明

  • while read p q:默认以空白字符(空格、制表符)分隔每行内容,将第一列赋值给p,第二列赋值给q;如果你的文件用其他字符(比如逗号)分隔列,可以改成IFS=, read p q来指定分隔符。
  • 把原curl中包裹JSON的单引号改为双引号,同时转义JSON内部的双引号(\"),这样shell才能正确解析$p和$q变量,替换为file.txt中的对应值。

内容的提问来源于stack exchange,提问作者Bikrant Kumar Sahoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 05:35:13