나는 bash 쉘을 사용하고 있습니다. 다음 변수를 정의했습니다 ...
localhost:tmp davea$ echo $json
{"id": "abc", "name": "dave"}
이 var의 값을 Ruby 명령어에 삽입하고 싶어서 이렇게 해봤는데...
localhost:tmp davea$ ruby -rjson -e 'j = JSON.parse($json); puts j["Instances"][0]["ImageId"]'
Ignoring bindex-0.5.0 because its extensions are not built. Try: gem pristine bindex --version 0.5.0
Ignoring byebug-11.0.1 because its extensions are not built. Try: gem pristine byebug --version 11.0.1
Ignoring byebug-10.0.2 because its extensions are not built. Try: gem pristine byebug --version 10.0.2
Ignoring byebug-10.0.1 because its extensions are not built. Try: gem pristine byebug --version 10.0.1
Ignoring byebug-10.0.0 because its extensions are not built. Try: gem pristine byebug --version 10.0.0
Ignoring childprocess-1.0.1 because its extensions are not built. Try: gem pristine childprocess --version 1.0.1
Ignoring debase-0.2.4 because its extensions are not built. Try: gem pristine debase --version 0.2.4
Ignoring eventmachine-1.2.5 because its extensions are not built. Try: gem pristine eventmachine --version 1.2.5
Ignoring ffi-1.11.1 because its extensions are not built. Try: gem pristine ffi --version 1.11.1
Ignoring ffi-1.9.25 because its extensions are not built. Try: gem pristine ffi --version 1.9.25
Ignoring ffi-1.9.23 because its extensions are not built. Try: gem pristine ffi --version 1.9.23
Ignoring ffi-1.9.18 because its extensions are not built. Try: gem pristine ffi --version 1.9.18
Ignoring hiredis-0.6.1 because its extensions are not built. Try: gem pristine hiredis --version 0.6.1
Ignoring json-2.1.0 because its extensions are not built. Try: gem pristine json --version 2.1.0
Ignoring nio4r-2.4.0 because its extensions are not built. Try: gem pristine nio4r --version 2.4.0
Ignoring nio4r-2.3.1 because its extensions are not built. Try: gem pristine nio4r --version 2.3.1
Ignoring nio4r-2.3.0 because its extensions are not built. Try: gem pristine nio4r --version 2.3.0
Ignoring nio4r-2.2.0 because its extensions are not built. Try: gem pristine nio4r --version 2.2.0
Ignoring nokogiri-1.10.4 because its extensions are not built. Try: gem pristine nokogiri --version 1.10.4
Ignoring nokogiri-1.8.2 because its extensions are not built. Try: gem pristine nokogiri --version 1.8.2
Ignoring nokogiri-1.8.1 because its extensions are not built. Try: gem pristine nokogiri --version 1.8.1
Ignoring pg-1.1.4 because its extensions are not built. Try: gem pristine pg --version 1.1.4
Ignoring pg-0.21.0 because its extensions are not built. Try: gem pristine pg --version 0.21.0
Ignoring puma-3.12.1 because its extensions are not built. Try: gem pristine puma --version 3.12.1
Ignoring puma-3.11.4 because its extensions are not built. Try: gem pristine puma --version 3.11.4
Ignoring puma-3.11.3 because its extensions are not built. Try: gem pristine puma --version 3.11.3
Ignoring puma-3.11.2 because its extensions are not built. Try: gem pristine puma --version 3.11.2
Ignoring rmagick-2.16.0 because its extensions are not built. Try: gem pristine rmagick --version 2.16.0
Ignoring ruby-debug-ide-0.7.0 because its extensions are not built. Try: gem pristine ruby-debug-ide --version 0.7.0
Ignoring ruby-filemagic-0.7.2 because its extensions are not built. Try: gem pristine ruby-filemagic --version 0.7.2
Ignoring sqlite3-1.4.1 because its extensions are not built. Try: gem pristine sqlite3 --version 1.4.1
Ignoring sqlite3-1.3.13 because its extensions are not built. Try: gem pristine sqlite3 --version 1.3.13
Ignoring websocket-driver-0.6.5 because its extensions are not built. Try: gem pristine websocket-driver --version 0.6.5
Ignoring websocket-native-1.0.0 because its extensions are not built. Try: gem pristine websocket-native --version 1.0.0
/Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/pure/parser.rb:135:in `convert_encoding': nil is not like a string (TypeError)
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/pure/parser.rb:78:in `initialize'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/common.rb:156:in `new'
from /Users/davea/.rvm/gems/ruby-2.4.0/gems/json-2.1.0/lib/json/common.rb:156:in `parse'
from -e:1:in `<main>'
내 $json이 번역되지 않는 것 같습니다. 위 명령에 삽입하는 올바른 방법은 무엇입니까?
답변1
작은따옴표를 사용합니다. Bash에서는 작은따옴표 안의 변수가 확장되지 않습니다.
ruby -rjson -e "j = JSON.parse($json); puts j[\"Instances\"][0][\"ImageId\"]"