Topic: Execute bash script in controller - show response in view

I'm using %x[ ] to run basic linux commands like "top" to display in my rails app.

I need to call a bash script that returns a string and display the string.

I tried this:

 @result = %x[ bash.sh ]

...and got nowhere.

I did some Googling and found several examples of folks using PTY and expect...which is alien to me.

So I'm in need of some guidance.

I need to call a bash script in my controller that will return a string, save it as an instance variable and redirect to my view to display that string.

PLEASE help :-)

Last edited by Yak (2008-04-22 11:06:48)

Re: Execute bash script in controller - show response in view

Use backticks...

baci:~ mikel$ cat test.sh
#!/bin/bash
echo "Hello"
baci:~ mikel$ ./test.sh
Hello
baci:~ mikel$ irb
irb(main):001:0> result = `./test.sh`
=> "Hello\n"
irb(main):002:0> puts result
Hello
=> nil
irb(main):003:0>

Hope that helps!

Mikel

Re: Execute bash script in controller - show response in view

Here you can check out the most used linux commands .

Re: Execute bash script in controller - show response in view

I really like this subject. Could you tell me more … I would love to explore

A really nice idea. I appreciate it much !

Ayesha Khan

Last edited by arfidichap45 (2012-02-28 07:46:04)