for 루프 내부에 POST 컬링

for 루프 내부에 POST 컬링

단일 사용자 생성에는 다음이 적용됩니다.

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

관련 정보