I have this program in ruby. I won't explain the whole process, but it gave me in the end a string.
I'd like to use this string in my shell. For now, I can generate it with ruby mysoft.rb
I'd like to use the result's string in a command, for exemple, when I commit with git, with something like this
git commit -m "$generated_string"
I would that the file was install on the computer and be usable by him. With a single command, he could have the generated string, from any directory, like a "normal" command like "ls" for exemple.
I have no idea how to do that ? Should I do a Gem ? Or something else. I'm new in Ruby, so, pretty confused.
Thanks a lot.
You are looking for shell command substitution; the syntax depends on which shell you are using. For example, if using bash or csh:
$ git commit -m `ruby mysoft.rb`
The following syntax does the same thing but in bash only:
$ git commit -m $(ruby mysoft.rb)
Change the first line of your program to this:
#!/usr/bin/env ruby
This line tells the shell that ruby should be used to execute this script by default.
Before you can run the script however you need to add the executable bit to the file:
> chmod gou+x mysoft.rb
Now you can type on the command line directly:
> ./mysoft.rb
And ruby will run your program.
If you want to make the command globally available on your machine, for example with the name mysoft, then you need to do this:
> sudo cp mysoft.rb /usr/bin/mysoft
This will install the program in the bin directory of the system. After this whenever you type mysoft anywhere on the machine, your program will run.
Related
I am trying to use git bash to run my .sh file that was generated using Makefile.
When running the command ./run.sh I get this message ./run.sh: line 1: /home/user/run: No such file or directory
To run a script file (using Git Bash), you do the following:
Add a "sh-bang" line on the first line (e.g. #!/bin/bash OR #!/usr/bin/env sh) this is how git bash knows a file is executable.
Use ./ (or any valid dir spec): ./script.sh
Note : any "sh-bang" will work
You are using git bash so I suppose you are using Windows.
As for me I always use shebang on my scripts. Depending on the content of your script, you may add one of the following lines at the first line of your script.
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed
#!/usr/awk
#!/usr/bin/python
If you still have problems running the script with ./run.sh command, you may try to use sh run.sh (on Git bash) and it should execute the script just as ./run.sh does it on linux.
This error message says that the first line of the script tries to execute an executable program named run in your home directory, and this does not exist.
I don't know what run.sh is supposed to do, but if you want to execute it a program, you need to make sure that the program exists, for instance by creating it.
I have a Ruby script in lib/my_file.rb. I want to run this file when someone cds into the directory and enters start_project.
To run a Ruby file, we should use ruby filename.rb, but how do I run a file using a command that in turn translates to "ruby filename.rb".
It depends for the most part what you are trying to accomplish.
If you want to use this as a shortcut on your machine, you can add an alias to your .bash_profile or .bashrc like so:
alias start_project='ruby filename.rb'
or add a function:
function start_project {
ruby filename.rb
}
If this is part of a repo, you probably want a file in your repo to do this. We can write a bash script for that:
#!/bin/bash
ruby filename.rb
If the bash script is called start_project.sh you can call it by by typing ./start_project.sh as long as you provide executable permissions (see "bash Permissions").
I'm looking for solution how can run Ruby script without invoking it directly like
ruby /path/to/file.rb
So far, I have been using aliases in my .bashrc to create shortcut like
alias myscript='ruby /path/to/file.rb'
But now, I need to create a gem which I'd like to use on different computers and my current approach doesn't fit well for this.
What you could do is the following:
Create a shell script which invokes the Ruby script as your alias does:
ruby /path/to/file.rb
Set a softlink to the /usr/bin/ path to invoke it in the shell using somecommand:
ln -s /full/path/to/the/previously/created/shellscript /usr/bin/somecommand
If you wanna go further, you could create a shell script which does the soft-linking automatically.
Add a shebang to the beginning of the script
#!/usr/bin/env ruby
(check that shebang is #!)
then make your script executable
chmod +x file.rb
Now you can run the file as a "standalone" executable
# For example
$ ./file.rb
("Standalone", because the ruby interpreter still needs to be installed somewhere in your path.)
I'm on OS X (with bash) and a newbie at unix. I want to know if it's possible to amend some file such that to run a ruby program, I don't need "ruby file.rb", but instead can just run "ruby.rb".
Is there a reason NOT to do this?
Thanks!
Yes you can do this.
Assuming ruby.rb has something like this in it:
#!/usr/bin/env ruby
puts 'Hello world'
At the command line: chmod +x ruby.rb
This makes it executable.
Then you can execute it like this:
./ruby.rb
For more details see wikibooks.
EDIT (Jörg W Mittag): Using #!/usr/bin/env ruby instead of #!/usr/bin/ruby as the shebang line is more portable, because on every Unix produced in the last 20 years, the env command is known to live in /usr/bin, whereas Ruby installations are typically all over the place. (E.g., mine lives in /home/joerg/jruby-1.2.0/bin/ruby.)
As others have mentioned, you want to have a shebang (#!) line at the beginning, and change the permissions to executable.
I would recommend using #!/usr/bin/env ruby instead of the path to Ruby directly, since it will make your script more portable to systems that may have Ruby installed in different directories; env will search in your search path, and so it will find the same Ruby that you would execute if you ran ruby on the command line. Of course, this will have problems if env is in a different location, but it is much more common for env to be at /usr/bin/env than for Ruby to be at /usr/bin/ruby (it may be in /usr/local/bin/ruby, /opt/bin/ruby, /opt/local/bin/ruby, etc)
#!/usr/bin/env ruby
puts "Hello!"
And make it executable:
chmod +x file.rb
chmod +x /path/to/file
No reason not to do it, as long as you prefix the interpreter with a shebang (#!/usr/local/ruby or whatever the path is on OSX). The shell doesn't care.
Place the correct shebang as the first line of your file. ex:
#!/usr/bin/ruby
in the shell, make the file executable
chmod +x file
If you want to do anything more complicated with running this application, you can always create a shell script:
#! /bin/sh
ruby ruby.rb
If you save it to run_script, you just have to chmod +x it as mentioned previously, then execute the following command:
$ ./run_script
I doubt this will be any more useful in your particular situation than the solutions already mentioned, but it's worth noting for completeness's sake.
I am new to programming and am trying to follow an example which uses #! comment in ruby.
I am consistently get the message:
bash: matz.rb: command not found
I am using this comment:
#! /usr/bin/env ruby
I have tried it with and without the space after ! as well as with and without the env.
when I use the
$ which ruby
ruby is in: /usr/bin/ruby
I have also gone into the operating system and changed the permissions on the file matz.rb to rwx for all users w/ no effect. Am I doing something wrong or do I have my system set up incorrectly?
The /usr/bin/env part is fine. You need to give bash the path to matz.rb when you run it. If you're in the directory with matz.rb, type "./matz.rb". The directory "." means the current directory - bash doesn't look there by default when running programs (like Windows does).
The env program (/usr/bin/env) searches the executable search path - the PATH environment variable - for the ruby program as if you typed it at the command prompt, and runs that program. The shebang comment doesn't do this. If you want to give your script to other people who might not have ruby installed in the same place as you, then you should use the "#!/usr/bin/env ruby" comment so that it will work as long as they can run ruby by typing "ruby".
If you're in the same directory as the matz.rb file, be sure to run it as
$ ./matz.rb
and not just
$ matz.rb
Here's a shell session demonstrating this working:
$ ls -la m*
-rwxr-xr-x 1 gareth gareth 32 8 Jan 08:46 matz.rb
$ cat matz.rb
#!/usr/bin/env ruby
puts "Matz"
$ matz.rb
-bash: matz.rb: command not found
$ ./matz.rb
Matz
Your file wasn't created on Windows was it? If it has \r\n line endings, that will upset bash. You can open it with Vim and check:
vi matz.rb
:set ff=unix
:wq
If when you tab-complete the "ff=" part it says dos, then it has the wrong file format. Alternatively, run dos2unix and try to run the file again:
apt-get install sysutils
dos2unix matz.rb
It sounds like you're on a Unix/Linux system and just typing matz.rb on the command line. If you're trying to execute a command in the current directory, you need to call it like ./matz.rb. The "./" tells it to look in the current directory rather than just /usr/bin and friends.
I failed to see any answer indicating you to change the executable mode of the file, so you might wanna try and do
chmod +x matz.rb
before you go and try doing
./matz.rb
Also it might be better not to attach a .rb extension to the file, such is the case for normal ruby / rails scripts e.g. script/generate, script/console etc.
You can use the 'shebang' line with either:
#!/usr/bin/ruby
#!/usr/bin/env ruby
But the script needs to be executable (you indicated it is) and in your shell $PATH.
echo $PATH
Put the script in one of those directories, or modify your path, otherwise specify the full path to it, for example:
export PATH=$PATH:/home/user/bin
or one of these:
./matz.rb
/home/user/bin/matz.rb
You can also run the Ruby interpreter passing the script filename as an argument, and it will be executed. This is particularly useful if you have another version of Ruby installed on your system (say, for testing, like Ruby Enterprise Edition, REE):
/usr/bin/ruby matz.rb
/opt/ree/bin/ruby matz.rb
Have you tried the ShaBang as following to directly point to ruby?
#! /usr/bin/ruby
Then you call the script from the commandline as
./matz.rb
Under Unix/Linux systems the dot in front of a command to search for the command in the current directory. If you give a path like /usr/bin/ruby, it will search in the current directory for a directory called usr...
A command without a dot/ in front is searched in locations specified by the path variable of the environment.
A command with a / on the beginning is searched exactly from root following the specified path.
Inside your ShaBang, you want to specify the exact path to the interpreter so "/usr/bin/ruby" is the correct one. On the commandline, where you want your script to be executed, you need to call the script with "./matz.rb" otherwise the bash will search a command like /usr/bin/matz.rb what leads to your errormessage.