Shellscript 컬이 오류를 발생시킵니다

Shellscript 컬이 오류를 발생시킵니다

컬을 사용하여 작은 쉘스크립트를 만들려고 합니다.

스크립트는 다음과 같습니다. (사용자 이름, URL 및 Co.를 숨겨야 합니다.)

for i in `cat cain.txt`
do
uuid=$(curl   -X POST -H "ACCEPT-LANGUAGE:en"   -H "Content-Type:application/json"   -H "Accept: application/json" -d  {"username":"TheUserName"}  HereTheUrl) 
> final.txt
fuid=$(echo "$uuid" | sed -nE 's/.*"serviceUuid":"(.*)","user.*/\1/p' >>  final.txt)
eval $fuid
echo $fuid

service_uuid=$(cat final.txt)
password=$(sed -n 1p cain.txt)
echo $service_uuid
echo $password

login=$(curl -X POST -H "ACCEPT-LANGUAGE:en"  -H "Content-Type: application/json;charset=utf-8"  -H "Accept-Encoding:gzip,deflate"   -H "Accept: application/json"  -H "Connection: keep-alive" -d  {"serviceUuid":"${service_uuid}","password":"${password}"}  HereTheUrl)

echo "$login"
if [ "$login" = "STATUS_OK" ]
then
echo "Password: $password"
break
else
echo "Trying: $password"
fi
done

이제 이것을 실행하면 다음 오류가 발생합니다.

{"status":{"code":"SERVER_ERROR","message":"java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"},"body":{"data":{"detailMessage":"java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $","cause":{"detailMessage":"Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $","stackTrace":[{"declaringClass":"com.google.gson.stream.JsonReader","methodName":"beginObject","fileName":"JsonReader.java","lineNumber":385},{"declaringClass":"com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter","methodName":"read","fileName":"ReflectiveTypeAdapterFactory.java","lineNumber":213},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":887},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":852},{"declaringClass":"com.teamdae.common.arch.jax.handler.GsonMessageBodyHandler","methodName":"readFrom","fileName":"GsonMessageBodyHandler.java","lineNumber":91},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBodyReader","fileName":"JAXRSUtils.java","lineNumber":1342},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBody","fileName":"JAXRSUtils.java","lineNumber":1293},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameter","fileName":"JAXRSUtils.java","lineNumber":826},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameters","fileName":"JAXRSUtils.java","lineNumber":789},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"processRequest","fileName":"JAXRSInInterceptor.java","lineNumber":212},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"handleMessage","fileName":"JAXRSInInterceptor.java","lineNumber":77},{"declaringClass":"org.apache.cxf.phase.PhaseInterceptorChain","methodName":"doIntercept","fileName":"PhaseInterceptorChain.java","lineNumber":308},{"declaringClass":"org.apache.cxf.transport.ChainInitiationObserver","methodName":"onMessage","fileName":"ChainInitiationObserver.java","lineNumber":121},{"declaringClass":"org.apache.cxf.transport.http.AbstractHTTPDestination","methodName":"invoke","fileName":"AbstractHTTPDestination.java","lineNumber":254},{"declaringClass":"org.apache.openejb.server.cxf.rs.CxfRsHttpListener","methodName":"doInvoke","fileName":"CxfRsHttpListener.java","lineNumber":251},{"declaringClass":"org.apache.tomee.webservices.CXFJAXRSFilter","methodName":"doFilter","fileName":"CXFJAXRSFilter.java","lineNumber":94},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.tomcat.websocket.server.WsFilter","methodName":"doFilter","fileName":"WsFilter.java","lineNumber":52},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.filters.HttpHeaderSecurityFilter","methodName":"doFilter","fileName":"HttpHeaderSecurityFilter.java","lineNumber":120},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.core.StandardWrapperValve","methodName":"invoke","fileName":"StandardWrapperValve.java","lineNumber":198},{"declaringClass":"org.apache.catalina.core.StandardContextValve","methodName":"invoke","fileName":"StandardContextValve.java","lineNumber":108},{"declaringClass":"org.apache.tomee.catalina.OpenEJBValve","methodName":"invoke","fileName":"OpenEJBValve.java","lineNumber":44},{"declaringClass":"org.apache.catalina.authenticator.AuthenticatorBase","methodName":"invoke","fileName":"AuthenticatorBase.java","lineNumber":522},{"declaringClass":"org.apache.catalina.core.StandardHostValve","methodName":"invoke","fileName":"StandardHostValve.java","lineNumber":140},{"declaringClass":"org.apache.catalina.valves.ErrorReportValve","methodName":"invoke","fileName":"ErrorReportValve.java","lineNumber":79},{"declaringClass":"org.apache.catalina.valves.AbstractAccessLogValve","methodName":"invoke","fileName":"AbstractAccessLogValve.java","lineNumber":620},{"declaringClass":"org.apache.catalina.core.StandardEngineValve","methodName":"invoke","fileName":"StandardEngineValve.java","lineNumber":87},{"declaringClass":"org.apache.catalina.connector.CoyoteAdapter","methodName":"service","fileName":"CoyoteAdapter.java","lineNumber":349},{"declaringClass":"org.apache.coyote.ajp.AjpProcessor","methodName":"service","fileName":"AjpProcessor.java","lineNumber":780},{"declaringClass":"org.apache.coyote.AbstractProcessorLight","methodName":"process","fileName":"AbstractProcessorLight.java","lineNumber":66},{"declaringClass":"org.apache.coyote.AbstractProtocol$ConnectionHandler","methodName":"process","fileName":"AbstractProtocol.java","lineNumber":785},{"declaringClass":"org.apache.tomcat.util.net.NioEndpoint$SocketProcessor","methodName":"doRun","fileName":"NioEndpoint.java","lineNumber":1425},{"declaringClass":"org.apache.tomcat.util.net.SocketProcessorBase","methodName":"run","fileName":"SocketProcessorBase.java","lineNumber":52},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor","methodName":"runWorker","fileName":"ThreadPoolExecutor.java","lineNumber":1142},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor$Worker","methodName":"run","fileName":"ThreadPoolExecutor.java","lineNumber":617},{"declaringClass":"org.apache.tomcat.util.threads.TaskThread$WrappingRunnable","methodName":"run","fileName":"TaskThread.java","lineNumber":61},{"declaringClass":"java.lang.Thread","methodName":"run","fileName":"Thread.java","lineNumber":745}],"suppressedExceptions":[]},"stackTrace":[{"declaringClass":"com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter","methodName":"read","fileName":"ReflectiveTypeAdapterFactory.java","lineNumber":224},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":887},{"declaringClass":"com.google.gson.Gson","methodName":"fromJson","fileName":"Gson.java","lineNumber":852},{"declaringClass":"com.teamdae.common.arch.jax.handler.GsonMessageBodyHandler","methodName":"readFrom","fileName":"GsonMessageBodyHandler.java","lineNumber":91},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBodyReader","fileName":"JAXRSUtils.java","lineNumber":1342},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"readFromMessageBody","fileName":"JAXRSUtils.java","lineNumber":1293},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameter","fileName":"JAXRSUtils.java","lineNumber":826},{"declaringClass":"org.apache.cxf.jaxrs.utils.JAXRSUtils","methodName":"processParameters","fileName":"JAXRSUtils.java","lineNumber":789},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"processRequest","fileName":"JAXRSInInterceptor.java","lineNumber":212},{"declaringClass":"org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor","methodName":"handleMessage","fileName":"JAXRSInInterceptor.java","lineNumber":77},{"declaringClass":"org.apache.cxf.phase.PhaseInterceptorChain","methodName":"doIntercept","fileName":"PhaseInterceptorChain.java","lineNumber":308},{"declaringClass":"org.apache.cxf.transport.ChainInitiationObserver","methodName":"onMessage","fileName":"ChainInitiationObserver.java","lineNumber":121},{"declaringClass":"org.apache.cxf.transport.http.AbstractHTTPDestination","methodName":"invoke","fileName":"AbstractHTTPDestination.java","lineNumber":254},{"declaringClass":"org.apache.openejb.server.cxf.rs.CxfRsHttpListener","methodName":"doInvoke","fileName":"CxfRsHttpListener.java","lineNumber":251},{"declaringClass":"org.apache.tomee.webservices.CXFJAXRSFilter","methodName":"doFilter","fileName":"CXFJAXRSFilter.java","lineNumber":94},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.tomcat.websocket.server.WsFilter","methodName":"doFilter","fileName":"WsFilter.java","lineNumber":52},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.filters.HttpHeaderSecurityFilter","methodName":"doFilter","fileName":"HttpHeaderSecurityFilter.java","lineNumber":120},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"internalDoFilter","fileName":"ApplicationFilterChain.java","lineNumber":192},{"declaringClass":"org.apache.catalina.core.ApplicationFilterChain","methodName":"doFilter","fileName":"ApplicationFilterChain.java","lineNumber":165},{"declaringClass":"org.apache.catalina.core.StandardWrapperValve","methodName":"invoke","fileName":"StandardWrapperValve.java","lineNumber":198},{"declaringClass":"org.apache.catalina.core.StandardContextValve","methodName":"invoke","fileName":"StandardContextValve.java","lineNumber":108},{"declaringClass":"org.apache.tomee.catalina.OpenEJBValve","methodName":"invoke","fileName":"OpenEJBValve.java","lineNumber":44},{"declaringClass":"org.apache.catalina.authenticator.AuthenticatorBase","methodName":"invoke","fileName":"AuthenticatorBase.java","lineNumber":522},{"declaringClass":"org.apache.catalina.core.StandardHostValve","methodName":"invoke","fileName":"StandardHostValve.java","lineNumber":140},{"declaringClass":"org.apache.catalina.valves.ErrorReportValve","methodName":"invoke","fileName":"ErrorReportValve.java","lineNumber":79},{"declaringClass":"org.apache.catalina.valves.AbstractAccessLogValve","methodName":"invoke","fileName":"AbstractAccessLogValve.java","lineNumber":620},{"declaringClass":"org.apache.catalina.core.StandardEngineValve","methodName":"invoke","fileName":"StandardEngineValve.java","lineNumber":87},{"declaringClass":"org.apache.catalina.connector.CoyoteAdapter","methodName":"service","fileName":"CoyoteAdapter.java","lineNumber":349},{"declaringClass":"org.apache.coyote.ajp.AjpProcessor","methodName":"service","fileName":"AjpProcessor.java","lineNumber":780},{"declaringClass":"org.apache.coyote.AbstractProcessorLight","methodName":"process","fileName":"AbstractProcessorLight.java","lineNumber":66},{"declaringClass":"org.apache.coyote.AbstractProtocol$ConnectionHandler","methodName":"process","fileName":"AbstractProtocol.java","lineNumber":785},{"declaringClass":"org.apache.tomcat.util.net.NioEndpoint$SocketProcessor","methodName":"doRun","fileName":"NioEndpoint.java","lineNumber":1425},{"declaringClass":"org.apache.tomcat.util.net.SocketProcessorBase","methodName":"run","fileName":"SocketProcessorBase.java","lineNumber":52},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor","methodName":"runWorker","fileName":"ThreadPoolExecutor.java","lineNumber":1142},{"declaringClass":"java.util.concurrent.ThreadPoolExecutor$Worker","methodName":"run","fileName":"ThreadPoolExecutor.java","lineNumber":617},{"declaringClass":"org.apache.tomcat.util.threads.TaskThread$WrappingRunnable","methodName":"run","fileName":"TaskThread.java","lineNumber":61},{"declaringClass":"java.lang.Thread","methodName":"run","fileName":"Thread.java","lineNumber":745}],"suppressedExceptions":[]}}}

답변1

내 생각엔 데이터(페이로드)를 잘 참조하지 않았기 때문인 것 같습니다.

>echo {"username":"TheUserName"} 
{username:TheUserName}

큰따옴표는 쉘에서 처리한 후에 사라집니다.

AFAIK JSON은 인용되지 않은 문자열을 허용하지 않습니다.

이 문제를 해결하려면 다음과 같이 데이터 주위에 작은따옴표 쌍을 추가하면 됩니다.

>echo '{"username":"TheUserName"}'
{"username":"TheUserName"}

쉘 확장(변수 대체)이 데이터에서 작동하도록 하려면 큰따옴표를 사용하고 내부 따옴표를 이스케이프하십시오.

>echo "{\"username\":\"$username\"}"
{"username":"myuser"}

쉘 매개변수를 올바르게 인용하는 더 많은 방법이 있다는 점에 유의하십시오(이 사이트에 이미 이에 대한 좋은 답변이 많이 있다고 생각합니다). 원하는 경우 데이터를 파일에 넣을 수도 있습니다.

관련 정보