2

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

asked Aug 4, 2010 at 23:57

4 Answers 4

7

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.

answered Aug 5, 2010 at 0:01
5
  • 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. Commented 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 . Commented Aug 5, 2010 at 1:38
  • @user384070 You should click on the tick to the left of this answer :) Commented 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. Commented 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 your PATH instead of the script). Commented Aug 5, 2010 at 15:34
3

put this as the first line of the script:

#!/usr/bin/env ruby
answered Aug 5, 2010 at 0:02
0

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

answered Aug 4, 2010 at 23:59
1
  • 2
    Pointless use of a shell script. Ruby is an interpreted language that supports the standard #!/usr/bin/ruby. Commented Aug 5, 2010 at 1:08
0

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
answered Aug 4, 2010 at 23:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.