tags:

views:

73

answers:

4

Hi ,

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

A: 

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:

  1. 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)
  2. change the permissions of the file, to allow it to execute: chmod +x teraData.rb
Tim McNamara
A: 

write a shell script that invokes the ruby script. Make sure both are executable.

hvgotcodes
Pointless use of a shell script. Ruby is an interpreted language that supports the standard `#!/usr/bin/ruby`.
sarnold
yeah its a bit weak. maybe an alias...
hvgotcodes
+2  A: 

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.

bta
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 the `PATH`, there's no need for the shell function.
sarnold
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 You should click on the tick to the left of this answer :)
Tim McNamara
@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
@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 your `PATH` instead of the script).
bta
+1  A: 

put this as the first line of the script:

#!/usr/bin/env ruby
ennuikiller