hi, I have a csh script, which has
setenv X xyz etc
My shell is korn
Is there some way I can "source" this to have the variables in my current korn shell?
thanks
hi, I have a csh script, which has
setenv X xyz etc
My shell is korn
Is there some way I can "source" this to have the variables in my current korn shell?
thanks
Hi.
Here is one way. Start with a csh script and source the script that has your setenv defined. That will create environment variables that can be passed to children processes. Then in the first script call the Korn shell script. Here's how it would work with tcsh and pdksh:
#!/usr/bin/env tcsh
# @(#) first.csh Demonstrate calling Korn shell script.
echo
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" tcsh
echo
source second.csh
./s1
exit 0
and second:
setenv X xyz
and the Korn script:
#!/bin/ksh -
# @(#) s1 Demonstrate inheriting environment variables from csh.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset
echo
echo " Results:"
echo " variable X is \"$X\""
exit 0
If you execute ./s1 by itself, we expect that the variable will not be defined:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
pdksh 5.2.14 99/07/13.2
Results:
./s1[12]: X: parameter not set
However, if you execute the first csh script, you get:
% ./first.csh
(Versions displayed with local utility version)
Linux 2.6.11-x1
tcsh 6.13.00
(Versions displayed with local utility "version")
Linux 2.6.11-x1
pdksh 5.2.14 99/07/13.2
Results:
variable X is "xyz"
You might also experiment with creating a function setenv which takes parameters variable-name and value, etc.
Best wishes ... cheers, drl
I'd go with a function to handle this AS LONG AS there are no other csh-specific lines in the file.
setenv() { parm=$1; shift; eval '\${$parm}="$*"'; }
(I'm not sure that's 100% correct.) Then source it with
#!/bin/ksh
. somefile.csh
printenv
The other thing you can do is pass the source file through awk and read the input into eval:
#!/bin/ksh
eval `awk somefile.csh '/^setenv/ { $1=""; var=$2;$2=""; print var "=" $0; }'`
printenv
Again, not sure if it's 100% correct. There might be some spaces in the output of the awk command that should be quoted and such.
This is working for me: had to grep some junk out of printenv...
environment.ksh:
#!/usr/bin/ksh
for i in `csh -c 'source environment.csh; printenv | grep -v \% | grep \='`
do
eval export $i
done
------
source this file, as in " . environment.ksh"