I have the following text that allows for having shell linux commands
executed and their results manipulated inside ruby code. If anyone can
tell me a better way of doing this, or he/she can mention the pros and
cons of this way of using shell linux commands inside ruby. I will be
very thankful to him/her. The following text is also available on the
link
The following is a step by step guide how can you write shell linux
commands inside ruby code and get their results in customized output
created by ruby.
First of all execute the following command on shell prompt
vi userinfo.rb
This will open up vi editor with a file named userinfo opened in it
1. Press i on your keyboard, this will switch the file userinfo into
insert mode
2. Write the following code in it
#Code Starts
def user
user = `users`
end
def groups
groups = `groups`
end
puts "You are #{user} and you belongs to the groups \n #{groups}" #Code Ends
3. Press ESC key on your keyboard to exit insert mode
4. Now write :wq and press enter key on your keyboard
This will throw you out to the shell prompt. On the shell prompt write
the following command
ruby userinfo.rb
You will the customized output like this
You are root and you belong to the groups
[A list of group will be displayed on this line]
Thats it to executing shell linux commands inside your linux code
pro would be that you can write your own version of a binary
con would be that a shell is spawned to execute the command
Another way to execute a command would be through the system method. But
then you couldn't get the command output that easily , since system
returns an exit code .
I have the following text that allows for having shell linux commands
executed and their results manipulated inside ruby code. If anyone can
tell me a better way of doing this, or he/she can mention the pros and
cons of this way of using shell linux commands inside ruby. I will be
very thankful to him/her. The following text is also available on the
link Linux Certification LPIC: Using Shell Commands in Ruby
The following is a step by step guide how can you write shell linux
commands inside ruby code and get their results in customized output
created by ruby.
First of all execute the following command on shell prompt
> vi userinfo.rb
This will open up vi editor with a file named userinfo opened in it
1. Press i on your keyboard, this will switch the file userinfo into
insert mode
2. Write the following code in it
#Code Starts
def user
user = `users`
end
def groups
groups = `groups`
end
puts "You are #{user} and you belongs to the groups \n #{groups}" #Code Ends
3. Press ESC key on your keyboard to exit insert mode
4. Now write :wq and press enter key on your keyboard
This will throw you out to the shell prompt. On the shell prompt write
the following command
> ruby userinfo.rb
You will the customized output like this
You are root and you belong to the groups
[A list of group will be displayed on this line]
Thats it to executing shell linux commands inside your linux code
macbook:~ michaelguterl$ ri Kernel#system
---------------------------------------------------------- Kernel#system
system(cmd [, arg, ...]) => true or false
···
On Fri, Oct 3, 2008 at 6:48 AM, Lex Williams <etaern@yahoo.com> wrote:
pros and cons ?
pro would be that you can write your own version of a binary
con would be that a shell is spawned to execute the command
Another way to execute a command would be through the system method. But
then you couldn't get the command output that easily , since system
returns an exit code .
------------------------------------------------------------------------
Executes _cmd_ in a subshell, returning +true+ if the command was
found and ran successfully, +false+ otherwise. An error status is
available in +$?+. The arguments are processed in the same way as
for +Kernel::exec+.