2017-06-05 9 views
-1

ausführen ich wie folgt, wenn die Zeichenfolge gedruckt wirdRuby eine mehrzeilige cmd String

curl --tlsv1.2 -b /tmp/cookie.txt -c /tmp/cookie.txt --connect-timeout 60 -X POST 
      -H 'Content-Type: application/xml' 
      -H 'Accept: application/xml' 
      -d '<?xml version="1.0" encoding="UTF-8" ?> 
      <ns:login xmlns:ns="http://www.actility.com/smp/ws/admin"> 
      <login>hello</login><password>wPa4GwYbRTCw0Uy!</password></ns:login>' 
      --location https://myapi.application.com 

Wie führe ich es in Ruby w/o Modifizieren

cmd = %Q(curl --tlsv1.2 -b #{config['cookies']} -C#{config['cookies']} -- 
connect-timeout 60 -X POST 
      -H 'Content-Type: application/xml' 
      -H 'Accept: application/xml' 
      -d '#{xml_content}' 
      --location #{uri}) 
puts cmd 

Der Ausgang ist ein curl Befehl bauen hatte die cmd string zu einem einzigen cmd?

Antwort

1

Ich denke, dass Sie mit Ihrem cmd vor dem „\ n“ de „\“ sollte es sein sollte hinzufügen wie folgt scaped:

[1] pry(main)> cmd = %Q(curl -i 
[1] pry(main)* -H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' 
[1] pry(main)* -XGET 'http://httpbin.org/basic-auth/user/password') 
=> "curl -i\n" + "-H 'Authorization: Basic dXNlcjpwYXNzd29yZA=='\n" + "-XGET 'http://httpbin.org/basic-auth/user/password'" 
[2] pry(main)> exec(cmd) 
curl: no URL specified! 
curl: try 'curl --help' or 'curl --manual' for more information 
sh: line 1: -H: command not found 
sh: line 2: -XGET: command not found 

Sie das schreiben sollte:

[1] pry(main)> cmd = %Q(curl -i \\ 
[1] pry(main)* -H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' \\ 
[1] pry(main)* -XGET 'http://httpbin.org/basic-auth/user/password') 
=> "curl -i \\\n" + "-H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' \\\n" + "-XGET 'http://httpbin.org/basic-auth/user/password'" 
[2] pry(main)> exec(cmd) 
HTTP/1.1 200 OK 
Connection: keep-alive 
Server: meinheld/0.6.1 
Date: Mon, 05 Jun 2017 09:46:24 GMT 
Content-Type: application/json 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Credentials: true 
X-Powered-By: Flask 
X-Processed-Time: 0.000503063201904 
Content-Length: 47 
Via: 1.1 vegur 

{ 
    "authenticated": true, 
    "user": "user" 
} 
0

Sie haben bereits einen String erstellt, der den Befehl enthält, also müssen Sie ihn an eine der Shell-Ausführungsmethoden von Ruby übergeben. I

result = %x(#{cmd}) 

Hier ist eine ausführlichere Antwort würde vorschlagen, Sie nützlich finden könnten: https://stackoverflow.com/a/2400/2029766

+0

ich diesen Fehler bekam 'curl: keine URL angegeben! curl: try 'curl --help' oder 'curl --manual' für weitere Informationen sh: 2: -H: nicht gefunden sh: 3: -H: nicht gefunden sh: 4: -d: nicht gefunden sh: 7: --Ort: nicht gefunden' –

+0

Ich sehe das Problem. Ein Zeilenumbruch in einem String, der an '% x()' übergeben wird, ist genauso wie das Drücken von im Terminal. Um das Senden des Befehls zu verhindern, müssen Sie das Zeilenumbruchzeichen umgehen, indem Sie ein "\" voranstellen (wie von anquegi vorgeschlagen), oder Sie entfernen einfach die Zeilenumbrüche für die Ausführung. '% x (# {cmd.tr (" \ n ", '')})'. –