Can you run a command line script with arguments, without typing'ruby' first?

Discussion in 'Ruby' started by Jayson Williams, Nov 24, 2008.

  1. Hi All,

    I would like to do this -> my_script arg1, arg2
    This doesn't seem to work when the script has arguments.

    But if I type -> ruby my_script arg1,arg2
    It works fine.

    Is there a way I can run the my_script without typing ruby each time?
    Thanks
    Jayson Williams, Nov 24, 2008
    #1
    1. Advertising

  2. Jayson Williams

    Diogo Lisboa Guest

    chmod a+x my_script (restrict permissions if you want)
    /my_script args

    or put my_script in your PATH, and just type `my_script args'

    On Mon, Nov 24, 2008 at 4:47 PM, Jayson Williams
    <> wrote:
    > Hi All,
    >
    > I would like to do this -> my_script arg1, arg2
    > This doesn't seem to work when the script has arguments.
    >
    > But if I type -> ruby my_script arg1,arg2
    > It works fine.
    >
    > Is there a way I can run the my_script without typing ruby each time?
    > Thanks
    >
    >




    --
    Diogo
    Diogo Lisboa, Nov 24, 2008
    #2
    1. Advertising

  3. Jayson Williams

    Tim Pease Guest

    On Nov 24, 2008, at 11:47 AM, Jayson Williams wrote:

    > Hi All,
    >
    > I would like to do this -> my_script arg1, arg2
    > This doesn't seem to work when the script has arguments.
    >
    > But if I type -> ruby my_script arg1,arg2
    > It works fine.
    >
    > Is there a way I can run the my_script without typing ruby each time?


    on any *nix platform, make the script executable and put a hash-bang
    line at the top

    chmod 755 my_script
    vim my_script
    i#!/usr/bin/env ruby<Return><Esc>:wq
    my_script arg1 arg2


    Blessings,
    TwP
    Tim Pease, Nov 24, 2008
    #3
  4. Jayson Williams

    Diogo Lisboa Guest

    > #!/usr/bin/env ruby

    Yes, I forgot the shebang.

    Diogo
    Diogo Lisboa, Nov 24, 2008
    #4
  5. I am using win os, so the shabang thing isn't an option for me. I put
    the script in my ruby bin path, and I can access the script from
    anywhere now, but I still have the same problem with the script not
    running properly unless i type ruby first. It is as if ruby does not
    attempt to read in arguments unless you explicitly pass the script
    through ruby. If I call the script with the arguments without putting
    'ruby' first, the args don't seem to get read.


    On Mon, Nov 24, 2008 at 2:05 PM, Diogo Lisboa <> wrote:
    > chmod a+x my_script (restrict permissions if you want)
    > ./my_script args
    >
    > or put my_script in your PATH, and just type `my_script args'
    >
    > On Mon, Nov 24, 2008 at 4:47 PM, Jayson Williams
    > <> wrote:
    >> Hi All,
    >>
    >> I would like to do this -> my_script arg1, arg2
    >> This doesn't seem to work when the script has arguments.
    >>
    >> But if I type -> ruby my_script arg1,arg2
    >> It works fine.
    >>
    >> Is there a way I can run the my_script without typing ruby each time?
    >> Thanks
    >>
    >>

    >
    >
    >
    > --
    > Diogo
    >
    >
    Jayson Williams, Nov 24, 2008
    #5
  6. Jayson Williams

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Nov 24, 2008 at 2:12 PM, Jayson Williams
    <>wrote:

    > I am using win os, so the shabang thing isn't an option for me. I put
    > the script in my ruby bin path, and I can access the script from
    > anywhere now, but I still have the same problem with the script not
    > running properly unless i type ruby first. It is as if ruby does not
    > attempt to read in arguments unless you explicitly pass the script
    > through ruby. If I call the script with the arguments without putting
    > 'ruby' first, the args don't seem to get read.
    >
    >
    > On Mon, Nov 24, 2008 at 2:05 PM, Diogo Lisboa <>
    > wrote:
    > > chmod a+x my_script (restrict permissions if you want)
    > > ./my_script args
    > >
    > > or put my_script in your PATH, and just type `my_script args'
    > >
    > > On Mon, Nov 24, 2008 at 4:47 PM, Jayson Williams
    > > <> wrote:
    > >> Hi All,
    > >>
    > >> I would like to do this -> my_script arg1, arg2
    > >> This doesn't seem to work when the script has arguments.
    > >>
    > >> But if I type -> ruby my_script arg1,arg2
    > >> It works fine.
    > >>
    > >> Is there a way I can run the my_script without typing ruby each time?
    > >> Thanks
    > >>
    > >>

    > >
    > >
    > >
    > > --
    > > Diogo
    > >
    > >

    >
    >

    Yep, in Windows you have to invoke ruby first on the command line. The
    shebang in the Unix world is kind of a short hand for doing this.

    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
    Glen Holcomb, Nov 24, 2008
    #6
  7. Jayson Williams

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Nov 24, 2008 at 2:21 PM, Glen Holcomb <> wrote:

    >
    >
    > On Mon, Nov 24, 2008 at 2:12 PM, Jayson Williams <
    > > wrote:
    >
    >> I am using win os, so the shabang thing isn't an option for me. I put
    >> the script in my ruby bin path, and I can access the script from
    >> anywhere now, but I still have the same problem with the script not
    >> running properly unless i type ruby first. It is as if ruby does not
    >> attempt to read in arguments unless you explicitly pass the script
    >> through ruby. If I call the script with the arguments without putting
    >> 'ruby' first, the args don't seem to get read.
    >>
    >>
    >> On Mon, Nov 24, 2008 at 2:05 PM, Diogo Lisboa <>
    >> wrote:
    >> > chmod a+x my_script (restrict permissions if you want)
    >> > ./my_script args
    >> >
    >> > or put my_script in your PATH, and just type `my_script args'
    >> >
    >> > On Mon, Nov 24, 2008 at 4:47 PM, Jayson Williams
    >> > <> wrote:
    >> >> Hi All,
    >> >>
    >> >> I would like to do this -> my_script arg1, arg2
    >> >> This doesn't seem to work when the script has arguments.
    >> >>
    >> >> But if I type -> ruby my_script arg1,arg2
    >> >> It works fine.
    >> >>
    >> >> Is there a way I can run the my_script without typing ruby each time?
    >> >> Thanks
    >> >>
    >> >>
    >> >
    >> >
    >> >
    >> > --
    >> > Diogo
    >> >
    >> >

    >>
    >>

    > Yep, in Windows you have to invoke ruby first on the command line. The
    > shebang in the Unix world is kind of a short hand for doing this.
    >
    > --
    > "Hey brother Christian with your high and mighty errand, Your actions speak
    > so loud, I can't hear a word you're saying."
    >
    > -Greg Graffin (Bad Religion)
    >


    If you absolutely must get rid of the need to type ruby first you could use
    rubyscript2exe to build a windows exe file out of your script.

    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
    Glen Holcomb, Nov 24, 2008
    #7
  8. Jayson Williams

    Kyle Schmitt Guest

    On Mon, Nov 24, 2008 at 3:12 PM, Jayson Williams
    <> wrote:
    > I am using win os, so the shabang thing isn't an option for me. I put
    > the script in my ruby bin path, and I can access the script from
    > anywhere now, but I still have the same problem with the script not
    > running properly unless i type ruby first. It is as if ruby does not
    > attempt to read in arguments unless you explicitly pass the script
    > through ruby. If I call the script with the arguments without putting
    > 'ruby' first, the args don't seem to get read.


    Jayson, if your not on a real OS, you need to associate the extension
    rb with the interpreter. It's been a little while since I've done
    this in windows, but it's actually pretty straightforward. You may
    have to google for how to associate the script with the interpreter
    though, as I don't recall the _exact_ steps.

    --Kyle
    Kyle Schmitt, Nov 24, 2008
    #8
  9. Jayson Williams

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Nov 24, 2008 at 2:26 PM, Kyle Schmitt <>wrote:

    > On Mon, Nov 24, 2008 at 3:12 PM, Jayson Williams
    > <> wrote:
    > > I am using win os, so the shabang thing isn't an option for me. I put
    > > the script in my ruby bin path, and I can access the script from
    > > anywhere now, but I still have the same problem with the script not
    > > running properly unless i type ruby first. It is as if ruby does not
    > > attempt to read in arguments unless you explicitly pass the script
    > > through ruby. If I call the script with the arguments without putting
    > > 'ruby' first, the args don't seem to get read.

    >
    > Jayson, if your not on a real OS, you need to associate the extension
    > .rb with the interpreter. It's been a little while since I've done
    > this in windows, but it's actually pretty straightforward. You may
    > have to google for how to associate the script with the interpreter
    > though, as I don't recall the _exact_ steps.
    >
    > --Kyle
    >
    >

    And here I am forgetting the simple steps, doh!

    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
    Glen Holcomb, Nov 24, 2008
    #9
  10. The associations are correct. I reset them just to be sure though. I
    can execute the script without ruby, and it runs, but without ruby
    first, the args are not getting read.

    The rubyscript2exe suggestion worked. Thanks Glen

    Thanks everyone for the feedback

    On Mon, Nov 24, 2008 at 4:18 PM, Glen Holcomb <> wrote:
    > On Mon, Nov 24, 2008 at 2:21 PM, Glen Holcomb <> wrote:
    >
    >>
    >>
    >> On Mon, Nov 24, 2008 at 2:12 PM, Jayson Williams <
    >> > wrote:
    >>
    >>> I am using win os, so the shabang thing isn't an option for me. I put
    >>> the script in my ruby bin path, and I can access the script from
    >>> anywhere now, but I still have the same problem with the script not
    >>> running properly unless i type ruby first. It is as if ruby does not
    >>> attempt to read in arguments unless you explicitly pass the script
    >>> through ruby. If I call the script with the arguments without putting
    >>> 'ruby' first, the args don't seem to get read.
    >>>
    >>>
    >>> On Mon, Nov 24, 2008 at 2:05 PM, Diogo Lisboa <>
    >>> wrote:
    >>> > chmod a+x my_script (restrict permissions if you want)
    >>> > ./my_script args
    >>> >
    >>> > or put my_script in your PATH, and just type `my_script args'
    >>> >
    >>> > On Mon, Nov 24, 2008 at 4:47 PM, Jayson Williams
    >>> > <> wrote:
    >>> >> Hi All,
    >>> >>
    >>> >> I would like to do this -> my_script arg1, arg2
    >>> >> This doesn't seem to work when the script has arguments.
    >>> >>
    >>> >> But if I type -> ruby my_script arg1,arg2
    >>> >> It works fine.
    >>> >>
    >>> >> Is there a way I can run the my_script without typing ruby each time?
    >>> >> Thanks
    >>> >>
    >>> >>
    >>> >
    >>> >
    >>> >
    >>> > --
    >>> > Diogo
    >>> >
    >>> >
    >>>
    >>>

    >> Yep, in Windows you have to invoke ruby first on the command line. The
    >> shebang in the Unix world is kind of a short hand for doing this.
    >>
    >> --
    >> "Hey brother Christian with your high and mighty errand, Your actions speak
    >> so loud, I can't hear a word you're saying."
    >>
    >> -Greg Graffin (Bad Religion)
    >>

    >
    > If you absolutely must get rid of the need to type ruby first you could use
    > rubyscript2exe to build a windows exe file out of your script.
    >
    > --
    > "Hey brother Christian with your high and mighty errand, Your actions speak
    > so loud, I can't hear a word you're saying."
    >
    > -Greg Graffin (Bad Religion)
    >
    Jayson Williams, Nov 24, 2008
    #10
  11. Re: Can you run a command line script with arguments, withou

    Jayson Williams wrote:
    > I am using win os, so the shabang thing isn't an option for me. I put
    > the script in my ruby bin path, and I can access the script from
    > anywhere now, but I still have the same problem with the script not
    > running properly unless i type ruby first. It is as if ruby does not
    > attempt to read in arguments unless you explicitly pass the script
    > through ruby. If I call the script with the arguments without putting
    > 'ruby' first, the args don't seem to get read.


    With my one-click-install it works just fine. This is my test1.rb:

    puts ARGV

    #when called like this:

    #test1.rb first second

    #it produces

    #first
    #second

    hth,

    Siep



    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Nov 24, 2008
    #11
  12. Jayson Williams

    Kyle Schmitt Guest

    On Mon, Nov 24, 2008 at 3:39 PM, Jayson Williams
    <> wrote:
    > The associations are correct. I reset them just to be sure though. I
    > can execute the script without ruby, and it runs, but without ruby
    > first, the args are not getting read.
    >
    > The rubyscript2exe suggestion worked. Thanks Glen
    >
    > Thanks everyone for the feedback

    Jayson, hum. I guess that makes sense. Now that I'm thinking back I
    never used command line options on my windows-ruby stuff, and I do
    recall having to go through extra (extra ugly too) hoops to get some
    of my vbscripts to read options from the command line properly. The
    rubyscript2exe is probably the best way to go anyway :)
    Kyle Schmitt, Nov 24, 2008
    #12
  13. Re: Can you run a command line script with arguments, withouttyping 'ruby' first?

    Jayson Williams wrote:
    > The associations are correct. I reset them just to be sure though. I
    > can execute the script without ruby, and it runs, but without ruby
    > first, the args are not getting read.


    Are command-line arguments included in the file association?
    There should be something like "...\ruby.exe" "$*" or
    "...\ruby.exe" "%1" "%2" "%3" "%4" "%5" (up to %9, this form is
    restricted to 9 arguments).

    Daniel
    Daniel Schömer, Nov 24, 2008
    #13
  14. Jayson Williams

    Pit Capitain Guest

    2008/11/24 Kyle Schmitt <>:
    > Jayson, hum. I guess that makes sense. Now that I'm thinking back I
    > never used command line options on my windows-ruby stuff, (...)


    FWIW, I never had problems calling Ruby scripts with command line
    args, and I just enter the script name without "ruby". Currently I'm
    using the OCI on Windows 2000...

    Regards,
    Pit
    Pit Capitain, Nov 24, 2008
    #14
  15. Jayson Williams

    Jorrel Guest

    How about creating a .bat file with:

    ruby my_script %*

    At least, I think it's %*.

    On Tue, Nov 25, 2008 at 5:55 AM, Kyle Schmitt <> wrote:
    > On Mon, Nov 24, 2008 at 3:39 PM, Jayson Williams
    > <> wrote:
    >> The associations are correct. I reset them just to be sure though. I
    >> can execute the script without ruby, and it runs, but without ruby
    >> first, the args are not getting read.
    >>
    >> The rubyscript2exe suggestion worked. Thanks Glen
    >>
    >> Thanks everyone for the feedback

    > Jayson, hum. I guess that makes sense. Now that I'm thinking back I
    > never used command line options on my windows-ruby stuff, and I do
    > recall having to go through extra (extra ugly too) hoops to get some
    > of my vbscripts to read options from the command line properly. The
    > rubyscript2exe is probably the best way to go anyway :)
    >
    >




    --
    jorrel
    Jorrel, Nov 24, 2008
    #15
  16. Glen Holcomb wrote:
    >
    > If you absolutely must get rid of the need to type ruby first you could use
    > rubyscript2exe to build a windows exe file out of your script.
    >
    >


    You can associate .rb files with the Ruby Interpreter - that should do
    it. That's how One Click Install sets it up so that if you double click
    on it, it will just run the script rather than open it in an editor.

    Cheers,
    Mohit.
    11/25/2008 | 1:10 PM.
    Mohit Sindhwani, Nov 25, 2008
    #16
  17. Pit Capitain wrote:
    > 2008/11/24 Kyle Schmitt <>:
    >
    >> Jayson, hum. I guess that makes sense. Now that I'm thinking back I
    >> never used command line options on my windows-ruby stuff, (...)
    >>

    >
    > FWIW, I never had problems calling Ruby scripts with command line
    > args, and I just enter the script name without "ruby". Currently I'm
    > using the OCI on Windows 2000...
    >

    I think the steps should be something like:
    1. Add Ruby Interpreter path to your path to make sure that you can run
    Ruby from anywhere
    2. In Explorer, go to a folder that has a Ruby script ending with an
    extension of .rb
    3. Press shift and right click on the .rb file there
    4. In the context menu, it should give an option called 'Open with' - if
    that has the Ruby interpreter in it, click on it. If that works, great!
    5. Alternatively, click on 'Choose Program' - navigate to your ruby
    install and select ruby.exe
    6. Click on 'Always use the selected program' - say OK, etc.
    7. Go to a command prompt and try to run your ruby script without ruby
    in the name and with the parameters
    command>myscript.rb param1
    It should work.

    Note that the associations need to be set up in the GUI and the script
    must be run from the command line with the parameters. It has worked
    like that for me for months. Do post if there's a problem.

    I think using Rubyscript2exe for this is a bit of a troublesome way -
    since you may want to change your script, etc. and having to repackage
    it all the time just to try something simple is a pain!

    Cheers,
    Mohit.
    11/25/2008 | 1:18 PM.
    Mohit Sindhwani, Nov 25, 2008
    #17
  18. Mohit,

    I thought this would work also, but for some reason it is not. I
    currently can run any ruby script from anywhere. The only issue I have
    is when its a script run from the command line that needs args. The
    script still runs, but I can tell that the args are not being read. If
    I run the same script with ruby, it works fine. The only things I can
    think of that might be causing this are:
    1) ruby may think that the arguments are meant for the interpreter
    instead of my script. So they are being consumed by the interpreter
    and my script never sees them. Is there a way to tell ruby the the
    args are not for the interpreter?
    2) I am using the global array ARGV[x] to read in command line
    arguments. Is there a better method.

    The rubyscript2exe method is working, but is a bit cumbersome when I
    have to make small changes. Being able to run the script with args
    directly would be best for me.

    Thanks Mohit!

    On Tue, Nov 25, 2008 at 12:14 AM, Mohit Sindhwani <> wrote:
    > Pit Capitain wrote:
    >>
    >> 2008/11/24 Kyle Schmitt <>:
    >>
    >>>
    >>> Jayson, hum. I guess that makes sense. Now that I'm thinking back I
    >>> never used command line options on my windows-ruby stuff, (...)
    >>>

    >>
    >> FWIW, I never had problems calling Ruby scripts with command line
    >> args, and I just enter the script name without "ruby". Currently I'm
    >> using the OCI on Windows 2000...
    >>

    >
    > I think the steps should be something like:
    > 1. Add Ruby Interpreter path to your path to make sure that you can run Ruby
    > from anywhere
    > 2. In Explorer, go to a folder that has a Ruby script ending with an
    > extension of .rb
    > 3. Press shift and right click on the .rb file there
    > 4. In the context menu, it should give an option called 'Open with' - if
    > that has the Ruby interpreter in it, click on it. If that works, great!
    > 5. Alternatively, click on 'Choose Program' - navigate to your ruby install
    > and select ruby.exe
    > 6. Click on 'Always use the selected program' - say OK, etc.
    > 7. Go to a command prompt and try to run your ruby script without ruby in
    > the name and with the parameters
    > command>myscript.rb param1
    > It should work.
    >
    > Note that the associations need to be set up in the GUI and the script must
    > be run from the command line with the parameters. It has worked like that
    > for me for months. Do post if there's a problem.
    >
    > I think using Rubyscript2exe for this is a bit of a troublesome way - since
    > you may want to change your script, etc. and having to repackage it all the
    > time just to try something simple is a pain!
    >
    > Cheers,
    > Mohit.
    > 11/25/2008 | 1:18 PM.
    >
    >
    >
    >
    >
    Jayson Williams, Nov 25, 2008
    #18
  19. Re: Can you run a command line script with arguments, withou

    Doesn't seem to work that way for me. I created a file called args.rb
    that looks like this

    puts ARGV[0]
    #end

    This is what I get
    >args.rb hello
    >nil


    >ruby args.rb hello
    >hello


    I think in the first example ruby is using my arguments for the
    interpreter. Any ideas how I can tell ruby to use the arguments for
    the script and not the interpreter?

    Jayson
    On Mon, Nov 24, 2008 at 4:53 PM, Siep Korteling <> wrote:
    > Jayson Williams wrote:
    >> I am using win os, so the shabang thing isn't an option for me. I put
    >> the script in my ruby bin path, and I can access the script from
    >> anywhere now, but I still have the same problem with the script not
    >> running properly unless i type ruby first. It is as if ruby does not
    >> attempt to read in arguments unless you explicitly pass the script
    >> through ruby. If I call the script with the arguments without putting
    >> 'ruby' first, the args don't seem to get read.

    >
    > With my one-click-install it works just fine. This is my test1.rb:
    >
    > puts ARGV
    >
    > #when called like this:
    >
    > #test1.rb first second
    >
    > #it produces
    >
    > #first
    > #second
    >
    > hth,
    >
    > Siep
    >
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Jayson Williams, Nov 25, 2008
    #19
  20. Daniel,
    I don't believe these arguments are included in the association. I am
    not sure how to add them.

    On Tue, Nov 25, 2008 at 12:00 PM, Daniel Sch=F6mer
    <> wrote:
    > Jayson Williams wrote:
    >> The associations are correct. I reset them just to be sure though. I
    >> can execute the script without ruby, and it runs, but without ruby
    >> first, the args are not getting read.

    >
    > Are command-line arguments included in the file association?
    > There should be something like "...\ruby.exe" "$*" or
    > "...\ruby.exe" "%1" "%2" "%3" "%4" "%5" (up to %9, this form is
    > restricted to 9 arguments).
    >
    > Daniel
    >
    >
    Jayson Williams, Nov 25, 2008
    #20
    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. tony lincoln
    Replies:
    1
    Views:
    4,295
    =?ISO-8859-1?Q?Fr=E9d=E9ric_Minne?=
    Jul 15, 2003
  2. Replies:
    4
    Views:
    1,708
    Terry Hancock
    Apr 23, 2005
  3. JimLad
    Replies:
    0
    Views:
    497
    JimLad
    Jan 26, 2010
  4. Swati Sharma
    Replies:
    5
    Views:
    481
    Charles Oliver Nutter
    Apr 7, 2008
  5. 3lionz Wexler
    Replies:
    2
    Views:
    655
    Suraj Kurapati
    Aug 24, 2010
Loading...

Share This Page