How to not display output of a system call.

Discussion in 'Ruby' started by Jerry Mr, Oct 6, 2009.

  1. Jerry Mr

    Jerry Mr Guest

    Lets say I have a Windows command line program that runs the following:

    system("del *.txt")

    How do I NOT display the output from the del command?

    Thank you.
    --
    Posted via http://www.ruby-forum.com/.
     
    Jerry Mr, Oct 6, 2009
    #1
    1. Advertising

  2. Jerry Mr

    Jerry Mr Guest

    Jerry Mr, Oct 6, 2009
    #2
    1. Advertising

  3. On 10/06/2009 10:29 PM, Jerry Mr wrote:
    > Lets say I have a Windows command line program that runs the following:
    >
    > system("del *.txt")
    >
    > How do I NOT display the output from the del command?
    >
    > Thank you.


    In this case:

    Dir["*.txt"].each {|f| File.delete f}

    :)

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Oct 6, 2009
    #3
  4. On 10/06/2009 10:45 PM, Jerry Mr wrote:
    > Also, how do you make the program pause for a few seconds.


    Put it to SLEEP.

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Oct 6, 2009
    #4
  5. Jerry Mr

    Tim Hunter Guest

    Jerry Mr wrote:
    > Lets say I have a Windows command line program that runs the following:
    >
    > system("del *.txt")
    >
    > How do I NOT display the output from the del command?
    >
    > Thank you.


    `del *.txt`

    That's backticks, not apostrophes. Also, this won't prevent the display
    of output to stderr, only stdout.

    --
    RMagick is looking for a maintainer. Email me if you're interested.
     
    Tim Hunter, Oct 6, 2009
    #5
  6. Jerry Mr

    Tim Hunter Guest

    Jerry Mr wrote:
    > Also, how do you make the program pause for a few seconds.


    $ ri Kernel#sleep
    ----------------------------------------------------------- Kernel#sleep
    sleep([duration]) => fixnum

    From Ruby 1.9.1
    ------------------------------------------------------------------------
    Suspends the current thread for duration seconds (which may be any
    number, including a Float with fractional seconds). Returns the
    actual number of seconds slept (rounded), which may be less than
    that asked for if another thread calls Thread#run. Zero arguments
    causes sleep to sleep forever.

    Time.new #=> 2008-03-08 19:56:19 +0900
    sleep 1.2 #=> 1
    Time.new #=> 2008-03-08 19:56:20 +0900
    sleep 1.9 #=> 2
    Time.new #=> 2008-03-08 19:56:22 +0900


    --
    RMagick is looking for a maintainer. Email me if you're interested.
     
    Tim Hunter, Oct 6, 2009
    #6
  7. Jerry Mr

    Jerry Piazza Guest

    Tim Hunter wrote:
    >
    > `del *.txt`
    >
    > That's backticks, not apostrophes. Also, this won't prevent the display
    > of output to stderr, only stdout.


    Hrmm, backticks don't seem to work on windows boxes.

    Tries to point to a CMD variable instead.


    @Robert Klemme

    I agree that doing it entirely in ruby would be the better way to go
    about it.
    Just sometimes it is quicker to use a command that is already available
    (or run a utility I added to %path%) to speed things up at work.

    Plus, now that I am wondering, it will haunt me until I find the answer.
    --
    Posted via http://www.ruby-forum.com/.
     
    Jerry Piazza, Oct 6, 2009
    #7
  8. Jerry Mr

    Jerry Piazza Guest

    I am now trying to impliment this.

    I created this:
    def spinner spin_trigger
    while (spin_trigger == true)
    print "\\\r"
    sleep 1
    print "|\r"
    sleep 1
    print "/\r"
    sleep 1
    print "-\r"
    sleep 1
    end
    end

    spin = true
    Thread.new do
    spinner spin
    end
    files = Dir.glob("c:/**/*.txt")
    spin = false

    But all I get is a "\" stuck at the beginning of the line until the Dir
    command finishes.

    Maybe I am confused about the usage of Thread?
    --
    Posted via http://www.ruby-forum.com/.
     
    Jerry Piazza, Oct 7, 2009
    #8
  9. Jerry Mr

    7stud -- Guest

    Jerry Piazza wrote:
    > I am now trying to impliment this.
    >
    > I created this:
    > def spinner spin_trigger
    > while (spin_trigger == true)
    > print "\\\r"
    > sleep 1
    > print "|\r"
    > sleep 1
    > print "/\r"
    > sleep 1
    > print "-\r"
    > sleep 1
    > end
    > end
    >
    > spin = true
    > Thread.new do
    > spinner spin
    > end
    > files = Dir.glob("c:/**/*.txt")
    > spin = false
    >
    > But all I get is a "\" stuck at the beginning of the line until the Dir
    > command finishes.
    >
    > Maybe I am confused about the usage of Thread?


    I think you have some more basic issues to deal with first. What do you
    think the output of the following code will be:

    def test(x)
    while x == 20
    sleep 3
    puts x
    end
    end

    x = 10
    test(20)
    x = 30

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Oct 7, 2009
    #9
  10. On 07.10.2009 04:58, 7stud -- wrote:
    > Jerry Piazza wrote:
    >> I am now trying to impliment this.
    >>
    >> I created this:
    >> def spinner spin_trigger
    >> while (spin_trigger == true)


    Comparing with "true" or "false" to obtain a boolean value is a very bad
    idea - especially in Ruby which has two false values and unlimited true
    values.

    >> print "\\\r"
    >> sleep 1
    >> print "|\r"
    >> sleep 1
    >> print "/\r"
    >> sleep 1
    >> print "-\r"
    >> sleep 1
    >> end
    >> end
    >>
    >> spin = true
    >> Thread.new do
    >> spinner spin
    >> end
    >> files = Dir.glob("c:/**/*.txt")
    >> spin = false
    >>
    >> But all I get is a "\" stuck at the beginning of the line until the Dir
    >> command finishes.
    >>
    >> Maybe I am confused about the usage of Thread?

    >
    > I think you have some more basic issues to deal with first. What do you
    > think the output of the following code will be:
    >
    > def test(x)
    > while x == 20
    > sleep 3
    > puts x
    > end
    > end
    >
    > x = 10
    > test(20)
    > x = 30


    Absolutely!

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Oct 7, 2009
    #10
  11. Jerry Piazza wrote:
    > Lets say I have a Windows command line program that runs the following:
    >
    > system("del *.txt")
    >
    > How do I NOT display the output from the del command?


    You could try something like this:

    system("del *.txt >NUL")

    Otherwise try IO.popen, which can capture the output of the command, so
    you can throw it away.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Oct 8, 2009
    #11
    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. 131.96.106.128 [johnny]

    grab system() call output

    131.96.106.128 [johnny], Jul 24, 2003, in forum: C++
    Replies:
    1
    Views:
    460
    Victor Bazarov
    Jul 24, 2003
  2. v4vijayakumar

    Extracting output from system(3) call

    v4vijayakumar, May 13, 2006, in forum: C Programming
    Replies:
    18
    Views:
    444
    Kevin Bagust
    May 15, 2006
  3. Wes Gamble
    Replies:
    2
    Views:
    123
    Robert Klemme
    Jun 19, 2009
  4. sed_y
    Replies:
    0
    Views:
    1,062
    sed_y
    Feb 15, 2012
  5. Madhu Ramachandran

    output of system() call

    Madhu Ramachandran, Jan 4, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    101
    zentara
    Jan 5, 2006
Loading...

Share This Page