0

I am newbie in Ruby, Rake and shell script. Here I created one simple task in Rake which parse YAML file and returns its response. Following is the task in my Rakefile. It works fine and puts prints the yaml value on console.

task :test, [:env] do |t, args|
  db_settings = YAML::load(File.open("test.yml"))["#{args[:env]}"]
  puts db_settings
end

Following is the result of the task.

{"url"=>"http://test.com", "end"=>"test"}
["test[local]"]

But when I want to call this task from shell script then it didn't return response to script. not sure ruby not able to send response or script itself doing something wrong.

Following is shell script that I am using to call this rake task. 
res= rake test[local]
echo $res

How can I get this parsed yaml from rake task to shell script?

Second question is, will this script be able to use this Parsed yaml. Rake task returns env specific json structure to script and the script supposed to get the element using key.

Can someone help on this to? is the current approach to use Yaml on script.

keepmoving
  • 1,813
  • 8
  • 34
  • 74
  • I believe you have a typo in your assignment. Maybe it should be `res=$(rake test[local])`, can you try that? The `$()` operator executes whatever is in the brackets in a sub-shell and assigns the result to the `res` variable. – D-FENS Dec 04 '21 at 14:53
  • yup, its printing result on shell console. but how can I consume yaml result in script. Do I need to write my own parser to extract element or bash has that capability too. – keepmoving Dec 04 '21 at 16:04
  • 1
    This is a separate question. You can find answers to the same question in [this thread](https://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script). – D-FENS Dec 04 '21 at 16:24

0 Answers0