how could I run a ruby script as a command in linux.
I have this script which access the lib but i have to run it as
teraData.rb
i want to run it as teradata (or some meaningful command ) with args on linux from any command promt.
Where should i place the script and what should I do?
I am kinda new to linux so please help
4 Answers 4
If the script is executable and the first line of the script is #!/usr/bin/ruby
(or whatever the path to your ruby interpreter might be), then you should be able to launch the script directly (i.e. $ ./myscript.rb
).
Otherwise, execute the interpreter and pass it the script as an argument (ruby ./myscript.rb
).
If you want to run the script from anywhere using a simple command, wrap one of these methods in a bash function like so:
function teraData {
ruby /path/to/script/teraData.rb $*
}
Place this function definition in your .bashrc
file to have it automatically loaded whenever you open a shell.
-
While it's useful to learn about shell functions, I think it should be noted that the
.rb
suffix on the file isn't necessary, and if the script is placed into a directory on thePATH
, there's no need for the shell function.sarnold– sarnold2010年08月05日 01:08:59 +00:00Commented Aug 5, 2010 at 1:08 -
I have put the directory in the PATH but i still have to type teraData.rb .I want a shell script/command like ls .user384070– user3840702010年08月05日 01:38:43 +00:00Commented Aug 5, 2010 at 1:38
-
@user384070 You should click on the tick to the left of this answer :)Tim McNamara– Tim McNamara2010年08月05日 03:51:52 +00:00Commented Aug 5, 2010 at 3:51
-
@user384070- Does the shell function I listed not provide the functionality that you want? It should allow you to launch the script by typing
teraData <your arguments>
from any directory.bta– bta2010年08月05日 15:31:47 +00:00Commented Aug 5, 2010 at 15:31 -
@user384070- Alternatively, if you can launch the script by typing
./teraData.rb
you should be able to create a symlink to the script and name it whatever you want (place the symlink in yourPATH
instead of the script).bta– bta2010年08月05日 15:34:18 +00:00Commented Aug 5, 2010 at 15:34
put this as the first line of the script:
#!/usr/bin/env ruby
write a shell script that invokes the ruby script. Make sure both are executable.
-
2Pointless use of a shell script. Ruby is an interpreted language that supports the standard
#!/usr/bin/ruby
.sarnold– sarnold2010年08月05日 01:08:02 +00:00Commented Aug 5, 2010 at 1:08
As you're new to linux, I recommend:
$ cd /path/to/file
$ ruby ./teraData.rb
Once you gain confidence, it's also possible to just enter the filename in the shell prompt. To do this, you need to:
- change the first line of
teraData.rb
to#! /usr/bin/env ruby
(this will find the correct executable for you, other first lines are possible) - change the permissions of the file, to allow it to execute:
chmod +x teraData.rb