Defining bash aliases from Ruby script - ruby

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.

Related

Running source command on parent shell from bash script

I have a bash script where I make a few changes to the .bashrc. I then want to run the bashrc from my script so I've been running
source ~/.bashrc
to avoid having to reload my shell. The problem I've been seeing is that it's only being set in the subshell bash is running in.
Can I do anything from my script so that the source command is run in the parent shell?
What you could do, if you really wanted to: Provide a shell function which checks whether .bashrc has been modified, and if it is the case, sources this file. Let#s call this function check_reload. Then define your PS1 as
PS1='$(check_reload) .....'
With this setup, your .bashrc will be reloaded before you get a new command line.
While this should solve your problem, I personally would not do it: I consider the information in .bashrc fairly static, and I would not use a script to modify it, but do it manually with a text editor. But of course everyone can do this as he likes....

How to run program and launch shell

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

Create aliases through shell script

We are using git for source control, which is installed on my windows machine.
In a nutshell I would like to create an alias for a shell script. I figure this out: alias ulf='. ulf.sh'. So far so good.
What I want is to add this to a script we have that configures all our git aliases (setaliases.sh). So far what I have read is that setting alias only applies to the current shell and when executing a shell script it does it in another shell. So I found out that you can call source ./setaliases.sh. This works right until I close down git-bash. Then I have to recreate the alias.
To sum it up: I want to create a global alias from within a .sh script.
You can input source .setalias.sh in your .bash_login file.
or another way to use alias is add some line in your .bashrc like this
alias ll='ls -lah'
alias gg='git status -s'
# to call more alias in setalias.sh file
source ~/.setalias.sh

How do we persist a `source bash_profile` command outside of a ruby script?

What we're trying to do is call source bash_profile to reload a bash_profile file. The script grabs a person's bash_profile and load it onto a person's computer. The problem is that the source bash_profile won't persist outside the ruby script. After the script ends, the terminal looks the same as it did before. How can we make it so that source bash_profile persists outside the ruby script?
The bash_profile usually modifies the bash environment (installing functions, aliases, variables, readline bindings, etc.), and there is really no way to modify the environment of a parent bash process.
So the best you can do is end the ruby script by execing a new bash, specifying the -l (or --login) option to make it a login shell so that it will start by sourcing bash_profile. (You can also do this by making the first character of argument -, usually by setting it to -bash.)
If you have control over the way the ruby script is initiated, you might be able to cause it to be execed, in order that it replaces the parent bash process. That will make for a cleaner process tree.

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

Resources