Guess what this will do...

Discussion in 'Ruby' started by John Carter, Jun 27, 2007.

  1. John Carter

    John Carter Guest

    Without cheating (trying to run it) what will this pair of files do under Linux....

    Here is a little script we want to be interpreted by the "flashbang" interpreter...
    ===/home/johnc/foo/hashbang===========================================
    #!/home/johnc/foo/flashbang


    puts ARGV[0]


    ======================================================================

    So we expect
    ./hashbang "Hello World"
    to be formally equivalent to running...

    /home/johnc/foo/flashbang ./hashbang "Hello World"

    Now assume....

    ===/home/johnc/foo/flashbang==========================================
    #!/bin/sh

    echo flashbanger
    echo $0
    echo "$@"

    /usr/bin/ruby "$@"
    ======================================================================

    Let's try...
    /home/johnc/foo/flashbang -e 'puts "Hello World"'

    On my system the result is....
    flashbanger
    /home/johnc/foo/flashbang
    puts "Hello World"
    Hello World

    No surprise...

    Now what do you think happens if you say...?

    ./hashbang "Hello World"

    Now what do you think happens if you say....?

    /home/johnc/foo/flashbang ./hashbang "Hello World"


    Scroll down for the answers...
     
    John Carter, Jun 27, 2007
    #1
    1. Advertising

  2. --gmhhrsDozM2n+uz5
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On Wed, Jun 27, 2007 at 08:28:07AM +0900, John Carter wrote:
    > Without cheating (trying to run it) what will this pair of files do under=

    Linux....
    >=20


    [lots of stuff snipped]

    > WHAT THE HELL!? Why?
    >=20
    > Well, deep within ruby/ruby.c is a chunk of code that looks for a #!
    > on the first line. If it contains the word "ruby" anywhere, it ignores
    > it. If it doesn't it says, Oh dear. This one wasn't actually for me. I
    > better be gnice and exec that interpreter and feed it this script...
    >=20


    That's a Perlism. man perlrun says:
    If the #! line does not contain the word "perl", the program named
    after the #! is executed instead of the Perl interpreter. This is
    slightly bizarre, but it helps people on machines that don't do #!,
    because they can tell a program that their SHELL is /usr/bin/perl,
    and Perl will then dispatch the program to the correct interpreter
    for them.

    I'm a bit surprised that ruby mimicks this behavior! Does it even
    _build_ on systems that don't do ``#!''?


    > All in all, you can tell I have a had rather exotic and frustrating
    > day or three.
    >=20
    > John Carter Phone : (64)(3) 358 6639
    > Tait Electronics Fax : (64)(3) 359 4632
    > PO Box 1645 Christchurch Email :
    > New Zealand
    >=20
    >=20


    --=20
    I saw `cout' being shifted "Hello world" times to the left and stopped right
    there.
    -- Steve Gonedes=20

    --gmhhrsDozM2n+uz5
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)

    iD8DBQFGgqQrKaiGM/xGKzQRAgj0AKDQPXJZg1BLJ51SvpIMat4tOsTORACfegi2
    sACf21Slo3ZC+gekA+xQSvY=
    =sFKG
    -----END PGP SIGNATURE-----

    --gmhhrsDozM2n+uz5--
     
    Anthony Martinez, Jun 27, 2007
    #2
    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. Replies:
    5
    Views:
    5,612
  2. Jim in Arizona

    Guess What ...

    Jim in Arizona, Jan 31, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    477
    Juan T. Llibre
    Jan 31, 2006
  3. Ashanen

    I guess what I'm asking is....

    Ashanen, Jan 2, 2004, in forum: Java
    Replies:
    6
    Views:
    422
    Ashanen
    Jan 3, 2004
  4. JavaJunkie
    Replies:
    12
    Views:
    914
    JavaJunkie
    Feb 4, 2004
  5. Michael Wurm
    Replies:
    12
    Views:
    605
    Roedy Green
    Jul 23, 2004
Loading...

Share This Page