0

I'm using bash shell. I have defined the following variable ...

localhost:tmp davea$ echo $json
{"id": "abc", "name": "dave"}

I would like to insert the value of this var into a ruby command, so I tried this ...

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>'

It would appear my $json is not being translated. What's the correct way to insert it into the above command?

1

You use singe quotes. In bash, variables within single quotes do not get expanded.

ruby -rjson -e "j = JSON.parse($json); puts j[\"Instances\"][0][\"ImageId\"]"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.