The environment is Mac and CentOS.
To run a program called ~/hoge/ruby.rb
from a terminal, it usually must be $ruby~/hoge/ruby.rb
.
Can I do this like $hoge-option
?
If possible, please let me know what steps and knowledge you need, and if there is a problem with doing so.
ruby
Let's start with the principles.
The file manager on the GUI (for example, Windows explorer, Linux nautilus) determines that it is a ruby script based on the contents and extensions of the file. So when you double-click, it automatically
ruby/home/mattn/hoge/ruby.rb
The command is now executed.On the other hand, when you run from a terminal, that is, from a shell, it doesn't automatically. However, by adding two points to the script file, you can run it from the GUI or from the terminal.
As mentioned above, UNIX-based operating systems start the file
#!How to run the interpreter
By giving the line, you can specify how to run the file.In other words, the extension .rb
can be used to boot with perl.
Also, because the interpreter is installed differently depending on the operating system,
#!/usr/bin/env ruby
It is common to write thatenv resolves the commands given in the argument from the PATH environment variable and executes them.
You can't just give a shebang.Execute the following commands so that you can run them directly from the command line:
chmod+x to/hoge/ruby.rb
Now you can double-click to boot from a file manager on the GUI and run it from a terminal.
By adding these two correspondences, the file extension is no longer necessary, so if you put it in a place where you pass, you will be able to execute it only by name.
On Unix-compatible or Linux systems, you can specify an interpreter at the beginning of a file so that the script can run on a specific interpreter when it starts.
Specifically, write the following on the first line of the ruby script file:
#!/usr/bin/env ruby
Then you can give the file execution permission to boot it directly.
$chmod+x to/hoge/ruby.rb
$ ~/hoge/ruby.rb
~/hoge/ruby.rb
can be used as the hoge command
by renaming it as hoge
or creating a symbolic link where path
passes.
Note that the description in this first line (starting with #!
) is called shebang
.
See Wikipedia for more information.
Put hashbang in the first line of the Ruby script as follows:
#!/usr/bin/env ruby
pARGV
For example, write the above code in the text file foo
and run it in the shell as follows:
$chmod+xfoo
$ ./foo bar
You should be able to see the [bar"]
output to make sure you have taken the arguments.
(In my environment, I was able to check with the Mac OSX and zsh, bash, tcsh on Ubuntu.)
/usr/local/bin
, you don't have to run it in the relative path.
Would it be possible to write a Shell Script?
#!/bin/sh
ruby~hoge/ruby.rb$1
Save the file under the name hoge, chmod+x, and put it where the path goes. I think it works with $hoge-option (option is in $1)
As a supplement to Madapaja's post, I think shebang
only took one argument in many operating systems, so #!/usr/bin/envruby
is the argument, so you cannot pass the command line argument to ruby
.
#!/bin/sh
exec ruby-S-x "$0" ${@+"$@"}
#!ruby
# The contents of ruby.rb are described below.ARGV contains command-line arguments.
Once you run ruby
from the shell script, as shown in , you can pass command-line arguments.
ruby
skips the line up to #!ruby
.
In addition, chmod+x
must give you permission to execute.
You can run the script as $ruby.rb-option
by following these steps:(If you want to run it as $hoge-option
, rename the script hoge
as you wrote.)
#!/usr/bin/envruby
in the first line of the scriptchmod+x to/hoge/ruby.rb
$HOME/hoge
to the environment variable PATH
or move ruby.rb
to the directory down the pathExplain what each step is doing.
The first line of the text file with #! (absolute path to the program)
is called the Shebang line.
Specifies the program to run the file as a script./usr/bin/env ruby
looks for and executes ruby
in the directory specified in the environment variable PATH
. You can also specify the former code directly, such as #!/usr/local/bin/ruby
.
chmod+x through/hoge/ruby.rb
adds permissions to the file so that the current user can run through/hoge/ruby.rb
as a program.See $manchmod
for more information.
The environment variable PATH
is used to set the directory in which you want to search for programs (for example, $echo$PATH
can be run without specifying the program name in the directory that is written in PATH
Therefore, in order to run the program as a command, pass it through the directory that contains the program or move it to the directory where it passes.
For Bash, write export PATH=/hoge/fuga/dir:$PATH
in through /.bash_profile
."For more information, search ""path bash"" and so on."
The rails command is executed like rails new or rails generate model.
In fact, this rails command is actually a ruby script.
$which rails tells you the filename.
Try catting the file name or opening it in editor.
It should look like this.
The following is the case on my machine with ruby in rbenv in MacOS X.
$which rails
/Users/katoy/.rbenv/shims/rails
$ cat/Users/katoy/.rbenv/shims/rails
#!/usr/bin/env bash
set-e
[-n "$RBENV_DEBUG" ] & set -x
program="${0###*/}"
if ["$program"="ruby"]; then
for arg; do
case "$arg" in
-e*|--)break;;
*/* )
if [-f "$arg" ]; then
export RBENV_DIR="${arg%/*}"
break
fi
;;
esac
done
fi
export RBENV_ROOT="/Users/katoy/.rbenv"
exec"/usr/local/Cellar/rbenv/0.4.0/libexec/rbenv"exec"$program""$@"
This is how the ruby program can run like a normal command.
There are many other things besides rails that allow ruby scripts to run like normal commands in this way.
(rake, rspec, bundle, coffee-script, ...)
If you really want to create a command line application with ruby, I recommend the following book.
Build Awesome Command-Line Applications in Ruby2
© 2024 OneMinuteCode. All rights reserved.