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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-24 19:52
(Received via mailing list)
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
5ff576dcf253c4469b51e7245c22808a?d=identicon&s=25 Diogo Lisboa (Guest)
on 2008-11-24 20:11
(Received via mailing list)
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
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2008-11-24 20:12
(Received via mailing list)
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
5ff576dcf253c4469b51e7245c22808a?d=identicon&s=25 Diogo Lisboa (Guest)
on 2008-11-24 20:16
(Received via mailing list)
> #!/usr/bin/env ruby

Yes, I forgot the shebang.

Diogo
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-24 22:17
(Received via mailing list)
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.
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 Glen Holcomb (Guest)
on 2008-11-24 22:21
(Received via mailing list)
On Mon, Nov 24, 2008 at 2:12 PM, Jayson Williams
<williams.jayson@gmail.com>wrote:

> wrote:
> >> This doesn't seem to work when the script has arguments.
> >
> > --
> > 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)
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 Glen Holcomb (Guest)
on 2008-11-24 22:24
(Received via mailing list)
On Mon, Nov 24, 2008 at 2:21 PM, Glen Holcomb <damnbigman@gmail.com>
wrote:

>> through ruby. If I call the script with the arguments without putting
>> > On Mon, Nov 24, 2008 at 4:47 PM, Jayson Williams
>> >> Thanks
>>
> 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)
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2008-11-24 22:31
(Received via mailing list)
On Mon, Nov 24, 2008 at 3:12 PM, Jayson Williams
<williams.jayson@gmail.com> 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
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 Glen Holcomb (Guest)
on 2008-11-24 22:43
(Received via mailing list)
On Mon, Nov 24, 2008 at 2:26 PM, Kyle Schmitt
<kyleaschmitt@gmail.com>wrote:

> 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)
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-24 22:44
(Received via mailing list)
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
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2008-11-24 22:58
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
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2008-11-24 23:00
(Received via mailing list)
On Mon, Nov 24, 2008 at 3:39 PM, Jayson Williams
<williams.jayson@gmail.com> 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 :)
50b2daf0e7666574579b9edaf8f2b69a?d=identicon&s=25 Pit Capitain (Guest)
on 2008-11-24 23:47
(Received via mailing list)
2008/11/24 Kyle Schmitt <kyleaschmitt@gmail.com>:
> 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
D7e32a4f229341c7489fe9a1cc060f91?d=identicon&s=25 Jorrel (Guest)
on 2008-11-25 01:02
(Received via mailing list)
How about creating a .bat file with:

ruby my_script %*

At least, I think it's %*.
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2008-11-25 06:12
(Received via mailing list)
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.
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2008-11-25 06:19
(Received via mailing list)
Pit Capitain wrote:
> 2008/11/24 Kyle Schmitt <kyleaschmitt@gmail.com>:
>
>> 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.
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-25 18:00
(Received via mailing list)
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!
Ed9979fe7dd7bcc809cfa79faacaf59e?d=identicon&s=25 Daniel Schömer (Guest)
on 2008-11-25 18:05
(Received via mailing list)
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
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-25 18:10
(Received via mailing list)
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
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-25 18:22
(Received via mailing list)
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ömer
Ff9e18f0699bf079f1fc91c8d4506438?d=identicon&s=25 James Britt (Guest)
on 2008-11-25 18:25
(Received via mailing list)
Kyle Schmitt wrote:

> 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.


That  *used* to happen when installing the 1-click[sic] version.

(It's in the PATHEXT system environment variable.  I think there has to
be something n the registry as well, but you could get that by
right-clicking a .rb file and selecting the app to always open files of
that type.)


Anyways, on my Vista box, it all works fine.  I type the name of a
script, no file extension, passing args, and all is super cool awesome.

I've never had to explicitly invoke ruby, even on XP. It's one case
where Windows did the right thing reliably. :)





--
James Britt

www.happycamperstudios.com   - Wicked Cool Coding
www.jamesbritt.com           - Playing with Better Toys
www.ruby-doc.org             - Ruby Help & Documentation
www.rubystuff.com            - The Ruby Store for Ruby Stuff
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2008-11-25 18:52
(Received via mailing list)
Daniel Schömer wrote:
> restricted to 9 arguments).
>
> Daniel
>

OK!  I have it here... I just checked through the Windows XP stuff on my
PC...
I think this is what you need to do.

1. Open a folder.
2. Select Tools > Folder Options
3. Tab: File Types
4. Scroll down till you find RB in the list
5. In the box that says 'Details for ...' click on [Advanced]
6. There is a box called 'Actions'
7. Select the open action - create a new one called open if it isn't
there
8. In the pop-up window, these are my settings:
  Action: open
  Application used to perform this action: "c:\ruby\bin\ruby.exe" "%1"
%*
  [x] use DDE
  Application: ruby
  Topic: System
  (other fields blank)
 [OK]

Try?

Cheers,
Mohit.
11/26/2008 | 1:50 AM.
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-25 19:18
(Received via mailing list)
WoooHooooo!!! That did it. The arguments were there except the last
%* was in quotes. Took the quotes off and presto. I can now do

>my_script args

and get the correct output.

 Thanks everyone!

Jayson
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2008-11-25 19:20
(Received via mailing list)
Jayson Williams wrote:
> WoooHooooo!!! That did it. The arguments were there except the last
> %* was in quotes. Took the quotes off and presto. I can now do
>
>
You're most welcome :)
Ed9979fe7dd7bcc809cfa79faacaf59e?d=identicon&s=25 Daniel Schömer (Guest)
on 2008-11-25 20:00
(Received via mailing list)
Jayson Williams wrote:
> I don't believe these arguments are included in the association. I am
> not sure how to add them.

I don't remember the exact menu/option names, but this should be
the way to check:

- Windows Explorer, Menu 'Extras', 'Folder Options'.  Or Control
  Panel, 'Folder Options'
- Tab 'Filetypes'
- Scroll to '.rb' and select
- Button 'Advanced' or 'Extended' on the bottom right
- Now there should be a textfield containing '...\ruby.exe'. Try
  to add "%*" or "%1" "%2" ... (try with and without quotes)
  after ruby.exe

(%1 gets replaced by the first argument, %2 by the second,
limited to 9 arguments. %* should be replaced by all arguments)

Daniel
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-28 20:02
(Received via mailing list)
Frustratingly, I got this working fine at work. I can type
 my_script args
and I get the proper results. Now my propblem is my home pc. I have
tried all the fixes above, but I still have to type

my_script.rb args
unless I inclued the .rb extension, the script will not run.
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2008-11-29 09:14
(Received via mailing list)
Jayson Williams wrote:
> Frustratingly, I got this working fine at work. I can type
>  my_script args
> and I get the proper results. Now my propblem is my home pc. I have
> tried all the fixes above, but I still have to type
>
> my_script.rb args
> unless I inclued the .rb extension, the script will not run.
>
In general, you should need to type in the extension as part of the
program name cos the extension is what tells Windows what to do with
it.  The exception is a list of extensions that Windows treats as
executables.  These are usually .exe, .com, . bat, .cmd etc - in these
cases, typing the extension is optional.

This is controlled in a variable called PATHEXT in the environment.  In
a command window, I did:
command>set

This gives me a list of extensions that are already there:
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.RB;.RBW

(note .rb and .rbw at the end)

Add that to your PATHEXT and try?

Cheers,
Mohit.
11/29/2008 | 4:13 PM.
7f40fd88cc3915154810df02406755e5?d=identicon&s=25 Jayson Williams (jayson)
on 2008-11-30 06:32
(Received via mailing list)
Mohit,

YOU ROCK !
I am embarrassed to have been goofing with computers for so many years
without knowing these basics. Anyway everything is working the way I
want, and I have learned some very useful basics.
Thanks!

Jayson
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2008-11-30 06:43
(Received via mailing list)
Jayson Williams wrote:
> Mohit,
>
> YOU ROCK !
> I am embarrassed to have been goofing with computers for so many years
> without knowing these basics. Anyway everything is working the way I
> want, and I have learned some very useful basics.
> Thanks!
>
me too :)
I had these items in peripheral vision, but your queries made me chase
them down :P

Cheers,
Mohit.
11/30/2008 | 1:42 PM.
This topic is locked and can not be replied to.