Discussion:
Sourcing a csh script from a ksh script
(too old to reply)
Linux Lover
2003-08-22 15:14:59 UTC
Permalink
Greetings,
I have this problem in which I "inherited" a huge system implemented
using shell scripts. This is already a maintenance nightmare, but what
makes it especially painful is that about half of the scripts are
written in ksh and half in csh. To make matters even worse, many of
the csh scripts are simply duplication of the ksh ones, so whenever a
change needs to be done, it has to be implemented in BOTH the ksh and
the csh version...

I would love to get rid of the csh scripts and convert to an all ksh
shop. However, in my work environment I cannot do it all at once - it
must be done gradually.

I was therefore thinking of starting by just eliminating the
duplication, by having a small wrapper ksh script that does nothing
except for calling the corresponding ("twin") csh script. That at
least will allow me to implement changes only once (until I convert
all scripts to ksh).

Unfortunately however, I don't know how to run a csh script from a ksh
script, so that the ENVIRONMENT VARIABLES are inherited.

I have written the following small ksh script to test a csh script
that only export a variable named MYVAR (assigned to
"thisismyvariable"):

#! /bin/ksh
/bin/csh `which config.csh`
echo "MYVAR: ${MYVAR}"

The output is:
MYVAR:

which is not what I want. I want:
MYVAR: thisismyvariable

Which I currently only get if I run the following csh script:

#! /bin/csh
source `which config.csh`
echo "TAPELIBMAP: ${TAPELIBMAP}"

Is there any way to accomplish what I want to do?

Thanks in advance!
L.L.
Remco
2003-08-22 15:51:16 UTC
Permalink
Post by Linux Lover
Greetings,
I have this problem in which I "inherited" a huge system implemented
using shell scripts. This is already a maintenance nightmare, but what
makes it especially painful is that about half of the scripts are
written in ksh and half in csh. To make matters even worse, many of
the csh scripts are simply duplication of the ksh ones, so whenever a
change needs to be done, it has to be implemented in BOTH the ksh and
the csh version...
The trick is to write a script that produces either a valid SH syntax
script or a CSH script depending on the situation. That way you only
have to maintain one script. Much the way the tset -s option works

$ cat <<EOF > ./myscript
#!/bin/sh
case $SHELL in
*csh) EXPORT () { eval "echo setenv $1 \'\$$1\'\;"; };;
*) EXPORT () { eval "echo export $1=\'\$$1\'\;"; };;
esac
MYVAR="myvar"
EXPORT MYVAR
EOF
$ chmod 755 ./myscript
$ ./myscript
To actually evaluate the script use:
$ eval `./myscript`
--
R.
Kevin Rodgers
2003-08-22 16:43:57 UTC
Permalink
Post by Linux Lover
#! /bin/ksh
/bin/csh `which config.csh`
echo "MYVAR: ${MYVAR}"
MYVAR: thisismyvariable
#! /bin/csh
source `which config.csh`
echo "TAPELIBMAP: ${TAPELIBMAP}"
How about something like:


#!/bin/ksh

$(/bin/csh -c "source `which config.csh`; env" |
sed -e 's/^\([^=]*\)=\(.*\)/export \1="\2"; /')

The idea is that /bin/csh sources the config.csh file and runs env to
print VARIABLE=VALUE lines, which are piped into sed to convert them
to export VARIABLE="VALUE"; lines, which are substituted as a single
/bin/ksh command.
--
Kevin Rodgers
Laurent Vogel
2003-08-23 07:23:54 UTC
Permalink
Post by Kevin Rodgers
#!/bin/ksh
$(/bin/csh -c "source `which config.csh`; env" |
sed -e 's/^\([^=]*\)=\(.*\)/export \1="\2"; /')
The idea is that /bin/csh sources the config.csh file and runs env to
print VARIABLE=VALUE lines, which are piped into sed to convert them
to export VARIABLE="VALUE"; lines, which are substituted as a single
/bin/ksh command.
Interesting idea. For more safety the sed part should be:

$(/bin/csh -c "source `which config.csh`; env" |
sed '
# remove non variables
/^[a-zA-Z_][a-zA-Z0-9_]*=/!d
# escape shell metacharacters '\', '"', '$', '!'
s/[\$!"]/\\&/g
# replace 'foo=bar' by 'export foo="bar";'
s/\([^=]*=\)\(.*\)/export \1"\2";/
')

Laurent
Juergen Heck
2003-08-23 09:02:44 UTC
Permalink
Post by Linux Lover
Greetings,
I have this problem in which I "inherited" a huge system implemented
using shell scripts. This is already a maintenance nightmare, but what
makes it especially painful is that about half of the scripts are
written in ksh and half in csh. To make matters even worse, many of
the csh scripts are simply duplication of the ksh ones, so whenever a
change needs to be done, it has to be implemented in BOTH the ksh and
the csh version...
I would love to get rid of the csh scripts and convert to an all ksh
shop. However, in my work environment I cannot do it all at once - it
must be done gradually.
I was therefore thinking of starting by just eliminating the
duplication, by having a small wrapper ksh script that does nothing
except for calling the corresponding ("twin") csh script. That at
least will allow me to implement changes only once (until I convert
all scripts to ksh).
Unfortunately however, I don't know how to run a csh script from a ksh
script, so that the ENVIRONMENT VARIABLES are inherited.
I have written the following small ksh script to test a csh script
that only export a variable named MYVAR (assigned to
#! /bin/ksh
/bin/csh `which config.csh`
echo "MYVAR: ${MYVAR}"
MYVAR: thisismyvariable
#! /bin/csh
source `which config.csh`
echo "TAPELIBMAP: ${TAPELIBMAP}"
Is there any way to accomplish what I want to do?
Thanks in advance!
L.L.
#! /bin/ksh
export TAPELIBMAP=$(/bin/csh -c '`which config.csh`; echo
"$TAPELIBMAP"')
echo "TAPELIBMAP: ${TAPELIBMAP}"

Regards
Juergen
Juergen Heck
2003-08-23 09:09:23 UTC
Permalink
Post by Linux Lover
Post by Linux Lover
#! /bin/csh
source `which config.csh`
echo "TAPELIBMAP: ${TAPELIBMAP}"
Is there any way to accomplish what I want to do?
Thanks in advance!
L.L.
#! /bin/ksh
export TAPELIBMAP=$(/bin/csh -c '`which config.csh`; echo
"$TAPELIBMAP"')
echo "TAPELIBMAP: ${TAPELIBMAP}"
should be

#! /bin/ksh
export TAPELIBMAP=$(/bin/csh -c 'source `which config.csh`; echo
"$TAPELIBMAP"')
echo "TAPELIBMAP: ${TAPELIBMAP}"

Regards
Juergen
Linux Lover
2003-08-26 14:56:30 UTC
Permalink
Post by Linux Lover
Unfortunately however, I don't know how to run a csh script from a ksh
script, so that the ENVIRONMENT VARIABLES are inherited.
Is there any way to accomplish what I want to do?
Thank you all for your replies and ideas (especially Kevin Rodgers who
was first to introduce the idea of a sed-based converter from env to
export).

Unfortunately, none of the solutions worked "out-of-the-box", but they
had enough in them to let me experiment until I got mine working. For
the benefit of all, I am posting a WORKING script that does the magic
(Thanks again everybody):

#!/bin/ksh
KORNED=$(/bin/csh -c "source `which config.csh`; env" | sed -e '/=/!d'
-e '/LOGNAME=/d' -e 's/^\([^=]*\)=\(.*\)/export \1="\2"; /')
eval ${KORNED}

Note: the "KORNED=" line is ONE line. If truncated in the posting
process, just join the lines in order for it to execute properly.

L.L.

Loading...