how to source a csh script in bash to set the environment [closed]
In your ~/.bashrc
(or the first of ~/.bash_profile
, ~/.bash_login
, and ~/.profile
that exists) source this script using something like . ~/bin/sourcecsh
:
#!/bin/bash# This should be sourced rather than executedwhile read cmd var valdo if [[ $cmd == "setenv" ]] then eval "export $var=$val" fidone < ~/.cshrc
This version eliminates the evil eval
:
#!/bin/bash# This should be sourced rather than executed# yes, it will be sourcing within sourcing - what so(u)rcery!source /dev/stdin < \<( while read cmd var val do if [[ $cmd == "setenv" ]] then echo "export $var=$val" fi done < cshrc)
Edit:
Without sourcing stdin:
while read cmd var valdo if [[ $cmd == "setenv" ]] then declare -x "$var=$val" fidone < cshrc
How about just defining a function called setenv, like so
setenv() { echo setting $1 to $2 export $1=$2}
and then sourcing the .cshrc file.
When I do this in bash, I get
[dws@oxygen ual-read-only]$ source cshrcsetting ORACLE_SID to TESTsetting ORACLE_HOME to /oracle/TEST/home/products/10204setting EPC_DISABLED to TRUEsetting MANPATH to /usr/local/man:/usr/share/mansetting EDITOR to visetting LD_LIBRARY_PATH to /oracle/TEST/home/products/10204/lib:/usr/sfw/lib/64setting NLS_LANG to AMERICAN_AMERICA.UTF8setting NLS_DATE_FORMAT to DD-MON-RR[dws@oxygen ual-read-only]$ env | grep ORACLEORACLE_SID=TESTORACLE_HOME=/oracle/TEST/home/products/10204
I'm in the same boat. A coworker showed me the following:
Start off in bash, without the stuff in thwe environment:
bash> echo $$12632bash> echo $FOO
Here's the csh file that gets source'd:
bash> cat setup-env.cshsetenv FOO "some csh stuff"echo FOO=$FOO in csh
Here's the command:
bash> csh -c 'source setup-env.csh;exec bash'
Look at the output from csh
FOO=some csh stuff in csh
And look at the output from the new bash shell
bash> echo $$ 13487 bash> echo $FOO some csh stuff
Now leave, and go back to the original bash shell
bash> exitexitbash> echo $$12632bash>
Note the echo $$ to see the process IDs, so that we can see they are different shell processes.
My coworker uses this enough that he puts it into an alias, like:
# make csh environment scripts useable (sourceable) from bash function # from Phil McCoy, Wed Nov 9 2011source_csh () { exec csh -c " source $*; setenv ALREADY_SOURCED \"$ALREADY_SOURCED:$*:\"; exec bash"}# sounds like a great idea to do source_csh .cshrc or .login# but naively done is infinitely recursive, # since the exec'ed bash will run .bashrc
Unfortunately, I have found that I often needed not just environment variable setup, but also aliases setup, as in the modules package http://modules.sourceforge.net/.
I have been able to automate this "csh source script recipes" by using Perl Expect. But I have not been able to be as interactive as I would like, except for the above.