How to run program and launch shell - ruby

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").

Related

Defining bash aliases from Ruby script

I'm trying to set a Bash alias from a Ruby script. The intended functionality would be (from the Ruby script):
Open ~/.bash_aliases (or something)
Add alias line to file (e.g: alias foo="cd /bar/blah")
Source .bash_aliases
Exit Ruby script
Be able to use new alias
However, using the system command doesn't work because it launches a new subshell.
Any advice?
what you want to do is not doable.
the script you are launching cannot really alter the environment of the shell.
one way to do this would be to source the output of the ruby script and to have the script just generate the commands. This way you are instructing the shell to actually do the right thing.
something like
source $(my_ruby_script.rb)
have the script alter the aliases and at the end read and print out the file.

Run .rb (Ruby) file on a Shell Script

I created a shell script to automate some processes, something like that:
#!/bin/bash
ruby RubyFile.rb
But when I run this script, I get this error:
ruby(2882): Operation not permitted
Any one knows what the hell is this?
I use this when I want want to run Ruby code "as executable".
#!/usr/bin/env ruby
And then chmod the script.
chmod +x script.rb
And run it
./script.rb
I suggest usage of env because running a command through /usr/bin/env has the benefit of looking for whatever the default version of the program is in your current environment.
Why so complicated? Why not just
#!/usr/bin/ruby
or wherever your ruby is?

Run Ruby script without invoking it directly

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.)

Using ruby in UNIX shell

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.

how to create installer of sh file in ubuntu

i m creating one application on ubuntu server in shell script. I write one shell script which runs other perl scripts. i want .exe file of that .sh file
You're just trying to write a shell script. In that case you just need to create the .sh file you want to be executable and then write your shell script as follows (the #!/bin/sh indicates which interpreter to use)
#!/bin/sh
... shell commands ...
then do a
chmod +x myscript.sh
to make it executable. Then to run it you perform a
./myscript.sh
If you want to have you application installed via yum, you should package it as an rpm, put it in a public repository and then make it available to your users.

Resources