Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Using Shell linux commands in Ruby!

Reply
Thread Tools

Using Shell linux commands in Ruby!

 
 
Ch Asif
Guest
Posts: n/a
 
      10-03-2008
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
http://linux-certification.blogspot....s-in-ruby.html

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

shell> 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

shell> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Lex Williams
Guest
Posts: n/a
 
      10-03-2008
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 .
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Nit Khair
Guest
Posts: n/a
 
      10-03-2008
Ch Asif wrote:
>


Please also look at: %x{command}

such as :

dirlist=%x{ls -m}

etc.

Do note that you may have to chomp() the result in many cases - there
will be the extra char at end (newline).

btw, system() won't return the output.

There are other variations such as "exec" which will overlay your
current process ...

my favorite is %x{...}
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Reid Thompson
Guest
Posts: n/a
 
      10-03-2008
Ch Asif wrote:
> 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
> http://linux-certification.blogspot....s-in-ruby.html
>
> 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
>
> shell> 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
>
> shell> 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
>

google ruby + popen

 
Reply With Quote
 
Suraj Kurapati
Guest
Posts: n/a
 
      10-03-2008
Lex Williams wrote:
> Another way to execute a command would be through the system method.


See also IO#popen:

>> IO.popen('cat', 'w+') {|f| f << 'hello'; f.close_write; puts f.read }

hello
=> nil
>> puts $?.exitstatus

0
=> nil

> But then you couldn't get the command output that easily,
> since system returns an exit code .


Wrong. Obtaining the output of a command has nothing to do with the
exit code:

>> `false`

=> ""
>> puts $?.exitstatus

1
=> nil
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lex Williams
Guest
Posts: n/a
 
      10-03-2008
> Wrong. Obtaining the output of a command has nothing to do with the
> exit code:
>
>>> `false`

> => ""
>>> puts $?.exitstatus

> 1
> => nil


Interesting example of NOT using system .
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Michael Guterl
Guest
Posts: n/a
 
      10-03-2008
On Fri, Oct 3, 2008 at 6:48 AM, Lex Williams <(E-Mail Removed)> 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 .


Actually Kernel#system returns true or false

macbook:~ michaelguterl$ ri Kernel#system
---------------------------------------------------------- Kernel#system
system(cmd [, arg, ...]) => true or false
------------------------------------------------------------------------
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+.

 
Reply With Quote
 
Lex Williams
Guest
Posts: n/a
 
      10-03-2008
sorry , I thought ruby went along the same way as the other languages
about system.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using subprocess module to launch a shell shell script that itselfforks a process Samuel A. Falvo II Python 0 10-08-2008 12:43 AM
using net::ssh shell to sudo to another user and execute commands wbsurfver@yahoo.com Ruby 8 09-12-2008 10:02 AM
executing commands in the same shell in Windows using Perl Murugesh Perl Misc 5 12-07-2005 04:25 PM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments