我试图多次运行bash命令,使用文本文件中的行作为参数:
while read -r line;
do
curl -X POST
-u JLCarveth
-H "Content-Type: application/json"
-H "Authorization: token [...]"
-H "Accept: application/json"
https://myurl.com/api/v1/user/repos
-d '{"name":"$line"}';
printf "Done $linen";
done < repo_names.txt
问题是,API没有接收name
参数。我如何正确地将line
值替换为JSON格式?
使用jq
等工具确保生成正确的JSON,无论line
的值是多少
while read -r line;
do
curl -X POST
-u JLCarveth
-H "Content-Type: application/json"
-H "Authorization: token [...]"
-H "Accept: application/json"
https://myurl.com/api/v1/user/repos
-d "$(jq -n --arg n "$line" '{name: $n}')"
printf 'Done %sn' "$line"
done < repo_names.txt