access built-in shell commands like 'history' or 'fc'

Discussion in 'Ruby' started by bwv549, Aug 9, 2006.

  1. bwv549

    bwv549 Guest

    How do I access built-in shell commands (like 'history' or 'fc') with a
    system call?

    Not what I was expecting:
    % ruby -e 'puts `history`'
    -e:1: command not found: history

    But this works fine:

    % ruby -e 'puts `pwd`'
    /home/john

    I've tried this:

    % ruby -e 'system "history >tmp.tmp"; puts IO.read("tmp.tmp")'

    And no errors are generated, but there is nothing in tmp.tmp, either.

    Thanks
    bwv549, Aug 9, 2006
    #1
    1. Advertising

  2. bwv549 wrote:
    > How do I access built-in shell commands (like 'history' or 'fc') with a
    > system call?
    >
    > Not what I was expecting:
    > % ruby -e 'puts `history`'
    > -e:1: command not found: history
    >
    > But this works fine:
    >
    > % ruby -e 'puts `pwd`'
    > /home/john
    >
    > I've tried this:
    >
    > % ruby -e 'system "history >tmp.tmp"; puts IO.read("tmp.tmp")'
    >
    > And no errors are generated, but there is nothing in tmp.tmp, either.
    >
    > Thanks

    Could it be that Ruby is using a different shell than you are used to?
    I think it uses /bin/sh by default (not sure).

    -Justin
    Justin Collins, Aug 9, 2006
    #2
    1. Advertising

  3. bwv549

    Guest

    On Thu, 10 Aug 2006, bwv549 wrote:

    > How do I access built-in shell commands (like 'history' or 'fc') with a
    > system call?
    >
    > Not what I was expecting:
    > % ruby -e 'puts `history`'
    > -e:1: command not found: history
    >
    > But this works fine:
    >
    > % ruby -e 'puts `pwd`'
    > /home/john
    >
    > I've tried this:
    >
    > % ruby -e 'system "history >tmp.tmp"; puts IO.read("tmp.tmp")'
    >
    > And no errors are generated, but there is nothing in tmp.tmp, either.
    >
    > Thanks




    system 'sh -c history'

    however there really won't be one since the invocation of sh immediately
    exits.

    you may find my session lib helpful

    require 'session' # gem install session

    sh = Session::SH.new

    stdout, stderr = sh.execute 'ls'
    p sh.status

    stdout, stderr = sh.execute 'history' # this will have 'ls' in it!
    p sh.status

    regards.


    -a
    --
    to foster inner awareness, introspection, and reasoning is more efficient than
    meditation and prayer.
    - h.h. the 14th dali lama
    , Aug 9, 2006
    #3
  4. bwv549

    bwv549 Guest

    > Could it be that Ruby is using a different shell than you are used to?
    > I think it uses /bin/sh by default (not sure).


    Perhaps that is part of it. However, on this suggestion, I entered the
    suggested shell (/bin/sh). However, the behavior hasn't changed,
    although the 'history' command works fine in this shell.

    Just for reference, this is what I get when checking what this shell
    is:

    % /bin/sh --version
    GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
    Copyright (C) 2005 Free Software Foundation, Inc.

    I'm on Ubuntu 6.X, just for reference.

    Thanks
    bwv549, Aug 9, 2006
    #4
  5. Why not access them by reading the file ~/.bash-history?

    Regards, Morton

    On Aug 9, 2006, at 3:30 PM, bwv549 wrote:

    > How do I access built-in shell commands (like 'history' or 'fc')
    > with a
    > system call?
    >
    > Not what I was expecting:
    > % ruby -e 'puts `history`'
    > -e:1: command not found: history
    >
    > But this works fine:
    >
    > % ruby -e 'puts `pwd`'
    > /home/john
    >
    > I've tried this:
    >
    > % ruby -e 'system "history >tmp.tmp"; puts IO.read("tmp.tmp")'
    >
    > And no errors are generated, but there is nothing in tmp.tmp, either.
    >
    > Thanks
    >
    >
    Morton Goldberg, Aug 10, 2006
    #5
  6. bwv549

    Guest

    On Thu, 10 Aug 2006, Morton Goldberg wrote:

    > On Aug 9, 2006, at 3:30 PM, bwv549 wrote:
    >
    >> How do I access built-in shell commands (like 'history' or 'fc') with a
    >> system call?
    >>
    >> Not what I was expecting:
    >> % ruby -e 'puts `history`'
    >> -e:1: command not found: history
    >>
    >> But this works fine:
    >>
    >> % ruby -e 'puts `pwd`'
    >> /home/john
    >>
    >> I've tried this:
    >>
    >> % ruby -e 'system "history >tmp.tmp"; puts IO.read("tmp.tmp")'
    >>
    >> And no errors are generated, but there is nothing in tmp.tmp, either.


    that's because each call starts a __new__ shell so you won't have any history!

    see my post about session.

    -a
    --
    to foster inner awareness, introspection, and reasoning is more efficient than
    meditation and prayer.
    - h.h. the 14th dali lama
    , Aug 10, 2006
    #6
  7. On 09.08.2006 21:51, bwv549 wrote:
    >> Could it be that Ruby is using a different shell than you are used to?
    >> I think it uses /bin/sh by default (not sure).

    >
    > Perhaps that is part of it. However, on this suggestion, I entered the
    > suggested shell (/bin/sh). However, the behavior hasn't changed,
    > although the 'history' command works fine in this shell.
    >
    > Just for reference, this is what I get when checking what this shell
    > is:
    >
    > % /bin/sh --version
    > GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
    > Copyright (C) 2005 Free Software Foundation, Inc.
    >
    > I'm on Ubuntu 6.X, just for reference.


    There is really no point in accessing the history of the shell because
    what you likely want is the history of the *invoking* shell, i.e. the
    one that started your ruby interpreter. AFAIK there is no way to access
    that history. If you want to work with that you can do this at the
    shell prompt.

    history | ruby -e '...'

    Your example with "pwd" worked because "pwd" is also an external program
    (try it out with "type -a pwd"). But you can access PWD much easier
    from inside Ruby:

    12:03:59 [~]: ruby -e 'puts Dir.pwd, ENV["PWD"]'
    /cygdrive/w
    /cygdrive/w

    Kind regards

    robert
    Robert Klemme, Aug 10, 2006
    #7
  8. bwv549

    x1 Guest

    # if HISTFILE is in rbconfig

    ruby -e 'IO.read(ENV["HISTFILE"]).split.each {|i| puts i}'
    # else
    ruby -e 'IO.read(ENV["HOME"] + "/.bash_history").split.each {|i| puts i}'


    On 8/10/06, Robert Klemme <> wrote:
    > On 09.08.2006 21:51, bwv549 wrote:
    > >> Could it be that Ruby is using a different shell than you are used to?
    > >> I think it uses /bin/sh by default (not sure).

    > >
    > > Perhaps that is part of it. However, on this suggestion, I entered the
    > > suggested shell (/bin/sh). However, the behavior hasn't changed,
    > > although the 'history' command works fine in this shell.
    > >
    > > Just for reference, this is what I get when checking what this shell
    > > is:
    > >
    > > % /bin/sh --version
    > > GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
    > > Copyright (C) 2005 Free Software Foundation, Inc.
    > >
    > > I'm on Ubuntu 6.X, just for reference.

    >
    > There is really no point in accessing the history of the shell because
    > what you likely want is the history of the *invoking* shell, i.e. the
    > one that started your ruby interpreter. AFAIK there is no way to access
    > that history. If you want to work with that you can do this at the
    > shell prompt.
    >
    > history | ruby -e '...'
    >
    > Your example with "pwd" worked because "pwd" is also an external program
    > (try it out with "type -a pwd"). But you can access PWD much easier
    > from inside Ruby:
    >
    > 12:03:59 [~]: ruby -e 'puts Dir.pwd, ENV["PWD"]'
    > /cygdrive/w
    > /cygdrive/w
    >
    > Kind regards
    >
    > robert
    >
    >
    x1, Aug 11, 2006
    #8
  9. bwv549

    Ken Bloom Guest

    On Wed, 09 Aug 2006 12:28:44 -0700, bwv549 wrote:

    > How do I access built-in shell commands (like 'history' or 'fc') with a
    > system call?
    >
    > Not what I was expecting:
    > % ruby -e 'puts `history`'
    > -e:1: command not found: history
    >
    > But this works fine:
    >
    > % ruby -e 'puts `pwd`'
    > /home/john
    >
    > I've tried this:
    >
    > % ruby -e 'system "history >tmp.tmp"; puts IO.read("tmp.tmp")'
    >
    > And no errors are generated, but there is nothing in tmp.tmp, either.
    >
    > Thanks


    I think under certain circumstances either ruby doesn't call the shell
    (and just calls the executable) or the shell doesn't recognize builtin
    commands when called as sh -c

    For example:
    irb(main):001:0> puts `cd scratch`
    (irb):1: command not found: cd scratch

    => nil
    irb(main):002:0> puts `pwd`
    /home/bloom
    => nil
    irb(main):003:0> puts `type pwd`
    (irb):3: command not found: type pwd

    => nil
    irb(main):004:0> puts `type pwd && echo`
    pwd is a shell builtin

    => nil
    irb(main):005:0> puts `which pwd`
    /bin/pwd
    => nil

    pwd works because it also happens to be a binary in /bin

    --Ken Bloom

    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Aug 11, 2006
    #9
  10. --0F1p//8PRICkK4MW
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline

    > I think under certain circumstances either ruby doesn't call the shell
    > (and just calls the executable) or the shell doesn't recognize builtin
    > commands when called as sh -c


    that's it.
    The most shells provide build-in commands only in the interactive mode.
    Some shells coIMost shells print something like "can't find...", some
    shells also print a littl help.

    example (on zsh):

    $ echo history | zsh
    history: not interactive shell

    regards,
    Matthias

    --0F1p//8PRICkK4MW
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (OpenBSD)

    iD8DBQFE3KgIbaBeCFlfXPMRAvAvAJ4rEBfdAcx+y7/nR0iAIYzdL7zL6QCfejM6
    Y0B1kciQ7usTNqNQZSlvMG0=
    =a2r4
    -----END PGP SIGNATURE-----

    --0F1p//8PRICkK4MW--
    Matthias Ludwig, Aug 11, 2006
    #10
    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. Adam
    Replies:
    0
    Views:
    326
  2. goldtech
    Replies:
    24
    Views:
    2,219
    DevPlayer
    Nov 28, 2011
  3. Sam Stephenson
    Replies:
    1
    Views:
    215
    Andrew Walrond
    Jun 18, 2005
  4. Replies:
    2
    Views:
    278
    nutso fasst
    Oct 17, 2006
  5. Niall
    Replies:
    3
    Views:
    156
    Niall
    Dec 6, 2006
Loading...

Share This Page