Sourcing a csh script from a ksh script

Sourcing a csh script from a ksh script

Post by Linux Lov » Sun, 24 Aug 2003 00:14:59



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.

 
 
 

Sourcing a csh script from a ksh script

Post by Remc » Sun, 24 Aug 2003 00:51:16



> 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.

 
 
 

Sourcing a csh script from a ksh script

Post by Willia » Sun, 24 Aug 2003 00:54:18



[...]

Quote:

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

Let me know if you find the answer, I haven't seen one.

But, there is an indirect method. Modify the csh script to output its
variables in ksh format, either to a file which ksh can then source and
delete, or to stdout and have your ksh script interpret that. The file
method is probably easier to implement.

I had the opposite problem; I needed to let a bourne shell script set
the environment in a c shell session. -Wm

 
 
 

Sourcing a csh script from a ksh script

Post by Kevin Rodger » Sun, 24 Aug 2003 01:43:57



> #! /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}"

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

 
 
 

Sourcing a csh script from a ksh script

Post by Laurent Voge » Sun, 24 Aug 2003 16:23:54



> 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.

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

 
 
 

Sourcing a csh script from a ksh script

Post by Juergen Hec » Sun, 24 Aug 2003 18:02:44



> 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.

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

Regards
Juergen

- Show quoted text -

 
 
 

Sourcing a csh script from a ksh script

Post by Juergen Hec » Sun, 24 Aug 2003 18:09:23



> > #! /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

 
 
 

Sourcing a csh script from a ksh script

Post by Linux Lov » Wed, 27 Aug 2003 23:56:30



> 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.

 
 
 

1. How to "source" a csh script from ksh script

I need to "source" a csh script from a ksh script.  Is there any way to
do this?  For example:

#!/bin/ksh
echo test1
exec csh
source csh_script_name
echo test2

When this executes, it fails to source the csh_script_name script.  The
csh_script_name script attempts to set environment variables.

Is there any way to do this?

Thanks,
Mike

Sent via Deja.com http://www.deja.com/
Before you buy.

2. USB on Kernel 2.2.x

3. exists any program (script) to port csh scripts to ksh

4. XF86Config for 3.3 w/ Trident 96xx

5. Converting ksh scripts to csh scripts

6. Force HTTP/1.1 - Protocoll

7. csh scripts inside ksh script?

8. System update on the fly?

9. Is it in anyway possible to source csh scripts from ksh?

10. how to call csh scripts from inside other csh scripts

11. CSH to KSH script converter???

12. Converting a csh script to ksh??

13. KSH scripts in CSH