以下适用于创建单个用户:
curl -X POST --anyauth -u admin:admin --header "Content-Type:application/json" \
-d '{"user-name":"joe",
"password": "cool",
"role": [ "rest-reader", "rest-writer" ]
}' \
http://localhost:8002/manage/v2/users
但在 for 循环内创建多个用户(一次一个)时失败
for i in john frank bob
do
curl -X POST --anyauth -u admin:admin --header "Content-Type:application/json" \
-d '{"user-name":"$i",
"password": "$i",
"role": [ "rest-reader", "rest-writer" ]
}' \
http://localhost:8002/manage/v2/users
done
我哪里做错了?
答案1
您的数据字符串用单引号引起来,但变量不会在单引号内扩展。
您可以使用 a 关闭开头的单引号'
,添加双引号变量"$i"
并使用 再次打开单引号字符串'
:
for i in john frank bob
do
curl -X POST --anyauth -u admin:admin --header "Content-Type:application/json" \
-d '{"user-name":"'"$i"'",
"password": "'"$i"'",
"role": [ "rest-reader", "rest-writer" ]
}' \
http://localhost:8002/manage/v2/users
done
答案2
尝试删除 $i 周围的引号
所以,基本上:
'{"username":"'$i'"}' 试试这个:
for i in john frank bob
do
curl -X POST --anyauth -u admin:admin --header "Content-Type:application/json" \
-d '{"user-name":"'$i'",
"password": "'$i'",
"role": [ "rest-reader", "rest-writer" ]
}' \
http://localhost:8002/manage/v2/users
done