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

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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 :)
@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.
@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).
3

put this as the first line of the script:

#!/usr/bin/env ruby

Comments

0

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

1 Comment

Pointless use of a shell script. Ruby is an interpreted language that supports the standard #!/usr/bin/ruby.
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

Comments

Your Answer

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.