Hi all,
I have this code :
`source ~/.bash_profile`
As part of a script. But when this line executes I get a sh: command
not found. This looks to me like ruby's ` is running /bin/sh instead
of /bin/bash
So a bit of searching on google and I discover that it's possible to
do something like:
%x{/bin/bash -c "source ~/.bash_profile;"}
This doesn't fail with the command not found, but it doesn't actually
source the file - so it's fairly useless.
Does anyone have a workaround for getting ruby to exec the bash source
command correctly? I think I've tried every combination in the docs
and nothing seems to work.
Thanks,
Kev
Hi,
I'm not sure what you're trying to achieve by the above. If you say
the second command (the %x one) doesn't fail, it probably means that
it *is* sourcing (i.e. running in the same bash process)
the .bash_profile file. But: when you call an OS-level command (in
this case, bash) like this, what happens is that Ruby runs that
command as a child process. So, it is running bash in a child process,
(which in turn reads and executes the commands in .bash_profile), then
returning to your Ruby script - the net effect being that nothing
seems to happen (because the settings done to environment variables
(in the profile file) in a child process, cannot affect the values of
environment variables in the calling (parent) process - both the child
and the parent have their own independent copies of these variables.
The child initially inherits the values for these variables from the
parent when the child is started, but there is no straightforward way
for any changes made to these variables in the child process, to be
propagated back to the same variables in the parent (though there are
workarounds, e.g. you could write the names and values of the env.
vars. to a file in the child process, and then when the child
terminates, read those values from the file back into the
corresponding env. vars. in the parent.
A more typical use of your command would be to use bash, (possibly
"sourcing" the .bash_profile - about which see below), to run a bash
script which does something you need done (e.g. sorting a file) - then
return to your Ruby script. Such changes would be permanent and
visible to the parent.
Sourcing the .bash_profile should actually happen by default when you
run "bash -c" if I remember right, but could be wrong - there are
multiple bash startup files e.g. (.bash_profile. .bash_rc), and I
don't remember or am not sure which one(s) get executed when you run
bash non-interactively from another script) - need to read the bash
man page for that.
HTH
Vasudev Ram
Dancing Bison Enterprises
http://www.dancingbison.com