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

J

Jayson Williams

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
 
D

Diogo Lisboa

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'
 
T

Tim Pease

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
 
J

Jayson Williams

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

Glen Holcomb

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

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.


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

Glen Holcomb

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

On Mon, Nov 24, 2008 at 2:12 PM, Jayson Williams <
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.



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

Kyle Schmitt

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
 
G

Glen Holcomb

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

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!
 
J

Jayson Williams

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
 
S

Siep Korteling

Jayson said:
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
 
K

Kyle Schmitt

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 :)
 
D

Daniel Schömer

Jayson said:
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
 
P

Pit Capitain

2008/11/24 Kyle Schmitt said:
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
 
M

Mohit Sindhwani

Glen said:
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.
 
M

Mohit Sindhwani

Pit said:
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.
 
J

Jayson Williams

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!
 
J

Jayson Williams

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
 
J

Jayson Williams

Daniel,
I don't believe these arguments are included in the association. I am
not sure how to add them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top