How to capture output of linux command

Discussion in 'Ruby' started by Damjan Rems, Apr 9, 2010.

  1. Damjan Rems

    Damjan Rems Guest

    You can run linux command:

    a = `ls /home`

    and receive output in variable a.

    But I would like /home to be variable. So I would invoke command like
    this:

    a = `"ls #{dir}"`

    This of course doesn't work, but I hope you get the point.

    a = system("ls #{dir}") works, but returns only true or false not the
    standard output of command.

    How can I get standard output of the command and use variable as
    parameter?


    by
    TheR
    --
    Posted via http://www.ruby-forum.com/.
     
    Damjan Rems, Apr 9, 2010
    #1
    1. Advertising

  2. Damjan Rems

    Ryan Davis Guest

    On Apr 9, 2010, at 04:33 , Damjan Rems wrote:

    > a = `"ls #{dir}"`
    >
    > This of course doesn't work, but I hope you get the point.


    remove the double quotes
     
    Ryan Davis, Apr 9, 2010
    #2
    1. Advertising

  3. Damjan Rems wrote:
    >
    > You can run linux command:
    >
    > a = `ls /home`
    >
    > and receive output in variable a.
    >
    > But I would like /home to be variable. So I would invoke command like
    > this:
    >
    > a = `"ls #{dir}"`


    As Ryan said, you need to remove the double quotes. You're asking the
    shell to execute the "ls whatever" command. There's no such command.

    BTW, you actually want double quotes there, but put them around the
    parameter:

    a = `ls "#{dir}"`

    The purpose of the double quotes here is to protect against the case
    where the 'dir' variable contain spaces (which are meaningful to the
    shell --they separate tokens).
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Apr 9, 2010
    #3
  4. Damjan Rems

    Marc Weber Guest

    Marc Weber, Apr 9, 2010
    #4
  5. Damjan Rems

    Damjan Rems Guest

    Marc Weber wrote:
    > Learn about Dir object. It has methods such as glob which should give
    > you directory contents. Then you don't have to care about stdout, nor
    > quoting.
    >
    > http://whynotwiki.com/Comparison_of_Escape_class_and_String.shell_escape
    >
    > Have a look at the first example here
    >
    > Marc


    Thanks guys.

    I used ls just as an example. I am actually calling smartctl command to
    find out SMART status of all disks on my computer.

    by
    TheR
    --
    Posted via http://www.ruby-forum.com/.
     
    Damjan Rems, Apr 9, 2010
    #5
  6. Damjan Rems

    Marc Weber Guest

    Excerpts from Albert Schlef's message of Fri Apr 09 13:46:34 +0200 2010:
    > Damjan Rems wrote:
    > >
    > > You can run linux command:
    > >
    > > a = `ls /home`
    > >
    > > and receive output in variable a.
    > >
    > > But I would like /home to be variable. So I would invoke command like
    > > this:
    > >
    > > a = `"ls #{dir}"`

    >
    > As Ryan said, you need to remove the double quotes. You're asking the
    > shell to execute the "ls whatever" command. There's no such command.
    >
    > BTW, you actually want double quotes there, but put them around the
    > parameter:
    >
    > a = `ls "#{dir}"`
    >
    > The purpose of the double quotes here is to protect against the case
    > where the 'dir' variable contain spaces (which are meaningful to the
    > shell --they separate tokens).


    If we got hat route you want ls -- ${dir} to protect against directories
    called "-a" or such .. And keep in mind that directories may contain
    slashes and such stuff on linux. eg try mkdir a\\b
    thus using quotes is not enough. It may work for most cases you use
    though. Anyway I'd do it right - you never know.

    Marc Weber
     
    Marc Weber, Apr 9, 2010
    #6
  7. Damjan Rems

    Schneider Guest

    On Apr 9, 8:46 am, Albert Schlef <> wrote:
    > Damjan Rems wrote:
    >
    > > You can run linux command:

    >
    > > a = `ls /home`

    >
    > > and receive output in variable a.

    >
    > > But I would like /home to be variable. So I would invoke command like
    > > this:

    >
    > > a = `"ls #{dir}"`

    >
    > As Ryan said, you need to remove the double quotes. You're asking the
    > shell to execute the "ls whatever" command. There's no such command.
    >
    > BTW, you actually want double quotes there, but put them around the
    > parameter:
    >
    >   a = `ls "#{dir}"`
    >
    > The purpose of the double quotes here is to protect against the case
    > where the 'dir' variable contain spaces (which are meaningful to the
    > shell --they separate tokens).
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Keep in mind that `ls #{dir}` (without the quotes) is also DANGEROUS.
    What if you (or someone) set dir = "~; rm -rf ~"? BAD.

    So, using quotes is also safer.
     
    Schneider, Apr 9, 2010
    #7
  8. Schneider wrote:
    > On Apr 9, 8:46�am, Albert Schlef <> wrote:
    >>
    >> The purpose of the double quotes here is to protect against the case
    >> where the 'dir' variable contain spaces (which are meaningful to the
    >> shell --they separate tokens).
    >> --
    >> Posted viahttp://www.ruby-forum.com/.

    >
    > Keep in mind that `ls #{dir}` (without the quotes) is also DANGEROUS.
    > What if you (or someone) set dir = "~; rm -rf ~"? BAD.
    >
    > So, using quotes is also safer.


    No, using quotes isn't safer:

    dir = '123" ; rm -rf ~ ; echo "'
    `ls "#{dir}"`

    Results it doing ls "123" ; rm -rf ~ ; echo ""
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Apr 12, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ernesto
    Replies:
    1
    Views:
    781
    Ernesto
    Feb 1, 2006
  2. Peter A. Schott
    Replies:
    4
    Views:
    598
    Peter A. Schott
    Feb 7, 2006
  3. Dib Urim
    Replies:
    4
    Views:
    198
    Ben Liddicott
    Nov 23, 2003
  4. Dib Urim
    Replies:
    4
    Views:
    204
    Michele Dondi
    Dec 3, 2003
  5. championsleeper
    Replies:
    8
    Views:
    595
Loading...

Share This Page