mixing string formatting with system exec

Discussion in 'Ruby' started by Nate St.Germain, Sep 26, 2010.

  1. noob question…

    why does this line produce the following result?

    %x("%s %s" % ['/usr/bin/touch', '/tmp/test'])

    error:
    sh: line 0: fg: no job control

    i don't necessarily need to use string formatting or substitution here,
    just curious about the result.

    thanks.
    --
    Posted via http://www.ruby-forum.com/.
    Nate St.Germain, Sep 26, 2010
    #1
    1. Advertising

  2. On 26.09.2010 21:26, Nate St.Germain wrote:
    > noob question…
    >
    > why does this line produce the following result?
    >
    > %x("%s %s" % ['/usr/bin/touch', '/tmp/test'])
    >
    > error:
    > sh: line 0: fg: no job control
    >
    > i don't necessarily need to use string formatting or substitution here,
    > just curious about the result.
    >
    > thanks.


    %x is backticks and the contents between brackets are passed unmodified
    to the shell:

    Robert@babelfish ~
    $ ruby19 -e 'p %x[ls a]'
    ls: cannot access a: No such file or directory
    ""

    Robert@babelfish ~
    $ ruby19 -e 'p %x["ls a"]'
    sh: ls a: command not found
    ""

    You are essentially doing the same as this:

    irb(main):002:0> system '"%s %s" % [\'/usr/bin/touch\', \'/tmp/test\']'
    sh: line 0: fg: no job control
    => false

    You get the same if you call bash as sh directly:

    Robert@babelfish ~
    $ /bin/sh -c '"%s %s"'
    /bin/sh: line 0: fg: no job control


    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Sep 26, 2010
    #2
    1. Advertising

  3. Robert Klemme wrote:

    > %x is backticks and the contents between brackets are passed unmodified
    > to the shell:



    ahh, got it. thanks, robert.
    --
    Posted via http://www.ruby-forum.com/.
    Nate St.Germain, Sep 27, 2010
    #3
    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. Hal Vaughan
    Replies:
    11
    Views:
    1,105
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,076
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    460
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    168
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    643
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page