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.

Dru
  • 9,632
  • 13
  • 49
  • 68
  • Are you looking for something like `eval` or do you actually need a new process? – Yet Another Geek May 02 '12 at 19:18
  • I'm not sure what you're asking. Judging by the [irb man page](http://linux.die.net/man/1/irb), you might be looking for something like [expect](http://linux.die.net/man/1/expect). – Tim Pote May 02 '12 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!"] – Dru May 02 '12 at 19:23
  • 1
    why you have to do it in `irb` ? It is supposed to be interacive. Why not run `ruby` directly ? `ruby – dpp May 02 '12 at 19:30
  • @dpp I'll give that a try now and pass my variables from the shell script to the ruby script. – Dru May 02 '12 at 19:48
  • 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. – dpp May 02 '12 at 19:49
  • @dpp Thanks, it's great to know about `ruby -e` – Dru May 02 '12 at 21:19

2 Answers2

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.

Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
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
Community
  • 1
  • 1
Ecuageo
  • 26
  • 5
  • 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. – Tim Pote May 02 '12 at 19:35
  • 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.) – Dru May 02 '12 at 19:41
  • 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 May 02 '12 at 19:49
  • @Ecuageo Post it in your answer. – Tim Pote May 02 '12 at 19:50
  • He doesn't want to enter that stuff by hand, he want's that input automated. – Tim Pote May 02 '12 at 19:54
  • fair enough. It seems like calling irb to execute static code is the wrong approach. – Ecuageo May 02 '12 at 19:56