[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sourcing bash aliases with Ruby

Chris Kilmer

12/8/2006 7:19:00 PM

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.

--
Posted via http://www.ruby-....

6 Answers

Ben Bleything

12/8/2006 7:24:00 PM

0

On Sat, Dec 09, 2006, Chris Kilmer wrote:
> 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

Wilson Bilkovich

12/8/2006 7:25:00 PM

0

On 12/8/06, Chris Kilmer <christopher.kilmer@biego.com> wrote:
> 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.

Chris Kilmer

12/8/2006 7:34:00 PM

0

Ben Bleything wrote:
> On Sat, Dec 09, 2006, Chris Kilmer wrote:
>> 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

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

--
Posted via http://www.ruby-....

Ben Bleything

12/8/2006 7:55:00 PM

0

On Sat, Dec 09, 2006, Chris Kilmer wrote:
> 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

Louis J Scoras

12/8/2006 8:45:00 PM

0

On 12/8/06, Ben Bleything <ben@bleything.net> wrote:

> 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


--
Lou.

Ara.T.Howard

12/8/2006 9:23:00 PM

0