Sourcing bash aliases with Ruby

C

Chris Kilmer

I'm trying to build a script that automates the sourcing of multiple
bash alias files using ruby. I'm running into problems in that it while
my alias files get run, the terminal session doesn't recognize any of
the aliases that were set.

I'll use a simple example to be more clear:

My original .bash_rc originally looked like this:

~/.bash_aliases

My .bash_aliases was a simple test:

alias rt="echo the rt alias worked"
echo the alias file was run


When I source directly with

~/.bash_rc

The alias gets set, I see the message 'the alias file was run' and the
rt command works as expected. So, every bash terminal session has
access to rt. Great. Awesome. That's how it should work.

However, I want to set aliases using Ruby. I change my .bash_rc

from: . ~/.bash_aliases

to: ruby ~/set_aliases.rb

My set_aliases.rb looks like this:

system "source ~/.bash_aliases"

Now, each terminal session I open actually runs the file. I know this
because I get the message 'the alias file was run'. However, the
terminal session doesn't have access to the rt alias (command unknown).

I'm thinking that the problem has to do with the process context that
the aliases are set in, but I don't know how to fix the problem.

Any ideas would be greatly appreciated. Thanks.
 
B

Ben Bleything

I'm thinking that the problem has to do with the process context that
the aliases are set in, but I don't know how to fix the problem.

Ding. When you call system(), it fires up a subordinate shell which
runs whatever command you pass in. Then, that shell exits and your
aliases are gone.

What's the problem you're trying to solve? I think there's probably a
better (non-Ruby) way to do what you want.

Ben
 
W

Wilson Bilkovich

I'm trying to build a script that automates the sourcing of multiple
bash alias files using ruby. I'm running into problems in that it while
my alias files get run, the terminal session doesn't recognize any of
the aliases that were set.


I'm thinking that the problem has to do with the process context that
the aliases are set in, but I don't know how to fix the problem.

Any ideas would be greatly appreciated. Thanks.

You're right. The aliases are getting added to the environment created
by 'system', but not added back to the parent. I'm pretty sure this is
a basic UNIX security issue. I'm not sure that there's a fix, sadly.
Hopefully I am wrong.
 
C

Chris Kilmer

Ben said:
Ding. When you call system(), it fires up a subordinate shell which
runs whatever command you pass in. Then, that shell exits and your
aliases are gone.

What's the problem you're trying to solve? I think there's probably a
better (non-Ruby) way to do what you want.

Ben

I'm basically trying to source a bunch of alias files without having to
resort to writing bash scripts. I'm hoping there is a way. Certainly,
if IO can be piped to the screen from the script then an alias can be
piped to the terminal session. Fingers crossed :)
 
B

Ben Bleything

I'm basically trying to source a bunch of alias files without having to
resort to writing bash scripts. I'm hoping there is a way. Certainly,
if IO can be piped to the screen from the script then an alias can be
piped to the terminal session. Fingers crossed :)

Invoking the ruby interpreter to do this is pretty... crazy. It should
be trivial to write a bash script to iterate over a list and require
them all. I'm a tcsh user, otherwise I'd just give you the code :)

Ben
 
L

Louis J Scoras

Invoking the ruby interpreter to do this is pretty... crazy. It should
be trivial to write a bash script to iterate over a list and require
them all. I'm a tcsh user, otherwise I'd just give you the code :)

Stick all the alias files in a directory (.aliases.d) under your home,
then stick this in your bashrc:

for afile in ${HOME}/.aliases.d/*; do
if [[ -r $afile ]]; then
. $afile
fi
done
 
A

ara.t.howard

I'm trying to build a script that automates the sourcing of multiple bash
alias files using ruby. I'm running into problems in that it while my alias
files get run, the terminal session doesn't recognize any of the aliases
that were set.

I'll use a simple example to be more clear:

My original .bash_rc originally looked like this:

. ~/.bash_aliases

My .bash_aliases was a simple test:

alias rt="echo the rt alias worked"
echo the alias file was run


When I source directly with

. ~/.bash_rc

The alias gets set, I see the message 'the alias file was run' and the
rt command works as expected. So, every bash terminal session has
access to rt. Great. Awesome. That's how it should work.

However, I want to set aliases using Ruby. I change my .bash_rc

from: . ~/.bash_aliases

to: ruby ~/set_aliases.rb

My set_aliases.rb looks like this:

system "source ~/.bash_aliases"

Now, each terminal session I open actually runs the file. I know this
because I get the message 'the alias file was run'. However, the
terminal session doesn't have access to the rt alias (command unknown).

I'm thinking that the problem has to do with the process context that
the aliases are set in, but I don't know how to fix the problem.

Any ideas would be greatly appreciated. Thanks.

a shell must be interactive to obtain aliases. this will work

harp:~ > cat a.rb
require 'rubygems'
require 'session'

lines = lambda{|string| string.split %r/\n/}

bash = Session::Bash::new 'program' => 'bash --login -i'

stdout, stderr = bash.execute 'alias'
p lines[stdout].grep(/ua/)

stdout, stderr = bash.execute 'ua'
p lines[stdout][0,10]



harp:~ > ruby a.rb
["alias ua='uname -a'"]
["\e]0;ahoward@harp:~\aLinux harp.ngdc.noaa.gov 2.4.21-47.0.1.EL #1 Fri Oct 13 18:04:55 EDT 2006 i686 i686 i386 GNU/Linux", "\e]0;ahoward@harp:~\a\e]0;ahoward@harp:~\a\e]0;ahoward@harp:~\a"]


but, as you see, interactive shells dump all sorts of crap to stdout.

-a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top