1

I would like to know how to run ruby commands in ruby on rails console inside a shell script ? To be clear: I have a shell script which will connect to ruby on rails console, but unable to run commands inside the rails console from shell script. Please see below code snippet.

I am successfully able to enter into rails console from shell script, but unable to provide ruby commands inside shell script itself.

This sample script is allowing me to enter into rails console:

#!/bin/bash
/usr/bin/helpkit-docker.sh --rails-console

But, when i extend the above script to run commands inside rails console using EOF, it throws error:

#!/bin/bash
/usr/bin/helpkit-docker.sh --rails-console <<EOF
Time.now.strftime("%d/%m/%Y %H:%M") 
EOF

Error: the input device is not a TTY

I expect the time to be fetched from rails console and get displayed in shell terminal

1
  • What is the contents of helpkit-docker.sh? I tried googling it but couldn't find it. I suppose the question is no longer relevant, but I've got a similar problem and this might help me. Commented Sep 13, 2023 at 7:05

1 Answer 1

2

I believe the best way is create a ruby script that run the ruby code that you want and then in your shell script you call it using rails runner, like this:

#!/bin/bash

rails runner script/myscript.rb

and in your script

puts Time.now.strftime("%d/%m/%Y %H:%M")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply, @Jorge David the below code snip, /usr/bin/helpkit-docker.sh --rails-console actually switches user as deploy and in docker it opens up rails console Actual code: docker exec --user deploy -it docker-name /bin/bash --login -c 'id; cd /data/helpkit/current; bundle exec rails console' So, how do i do this in shell script and my requriement is after accessing the rails console via shell script, i would like to run some ruby commands.. As per your suggession, i can club all my commands in single ruby script but how do i execute this.. Please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.