0

I am trying to enter irb from a shell script, do something, and then return to the shell script.

How can I do this?

#!/bin/bash
#example_app

# Some shell instructions here
irb
four = 2 + 2
exit
echo "This text is not reached and the process hangs open"

NOTE: I should not try to load irb to enter static code, I agree with you guys. I didn't think about what I was doing. I was interacting with an AWS library and I tried to carry that same philosophy into automating this without thinking twice about it.

7
  • Are you looking for something like eval or do you actually need a new process? Commented May 2, 2012 at 19:18
  • I'm not sure what you're asking. Judging by the irb man page, you might be looking for something like expect. Commented May 2, 2012 at 19:21
  • I am trying to execute a series of ruby commands from a shell script that work perfectly from ruby console if I manually enter irb. An extremely simplified explanation of what I need is [a shell script that can enter irb, do something, and return to the shell script to echo "Done!"] Commented May 2, 2012 at 19:23
  • 1
    why you have to do it in irb ? It is supposed to be interacive. Why not run ruby directly ? ruby <script file> Commented May 2, 2012 at 19:30
  • 1
    ruby also has a -e option, you could also do ` ruby -e 'four = 2 + 2' -e 'puts four' ` from your bash shell if your code is only few lines. Commented May 2, 2012 at 19:49

2 Answers 2

2

You can pass a here document to irb:

irb <<SCRIPT
  four = 2 + 2
  puts four
SCRIPT

However, I don't think that will accomplish what you are trying to do. Here is the output:

Switch to inspect mode.
  four = 2 + 2
4
  puts four
4
nil

It is similar to bash -x. Why not simply use plain old ruby for this?

ruby <<SCRIPT
  four = 2 + 2
  puts four
SCRIPT
4

There is no need to spawn an interactive shell if you aren't going to interact with it.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you want the answer explained from fork and exec in bash essentially:

function_to_fork() { irb }

function_to_fork &

that may not work if you want some interaction there to influence the parent process or for the parent process to wait.

My comment below didn't format very well:

With the following program I was able to achieve:

#!/bin/bash
# irbb.sh
irb
echo "here"

$ ./irbb.sh 
>> four = 2 + 2
=> 4
>> exit
here

6 Comments

I don't think this answers the question at all. The OP essentially wants to run some commands in another shell, then return to his script in sequence, not in parallel.
I tried what you suggested. The process does not hang open as before, but shell commands after the fork are not getting executed and the code inside the fork doesn't seem to be getting executed. (The actual ruby code, involves storing files. I've tested this manually and it works. That's how I'm able to isolate it to this shell script.)
With the following program I was able to achieve: #!/bin/bash # irbb.sh irb echo "here" $ ./irbb.sh >> four = 2 + 2 => 4 >> exit here
@Ecuageo Post it in your answer.
He doesn't want to enter that stuff by hand, he want's that input automated.
|

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.