How to "source" a csh script from ksh script

How to "source" a csh script from ksh script

Post by mikea.. » Sat, 10 Jun 2000 04:00:00



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.

 
 
 

How to "source" a csh script from ksh script

Post by David Hasset » Sat, 10 Jun 2000 04:00:00



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

Hmmm. Lots of csh questions this morning. :-) The general problem is
that when you "exec" csh, it takes over the current shell (hence, it is
in a sub-shell until 'exit' is called, and then all the environment
variables disappear). I'll assume that what you want to do is export
some csh variables into the ksh environment. The only reliable way I can
think of doing this is to mangle the csh file with sed into a ksh
script, then write that to a temp file. Your ksh script would then
source that script. I would imagine that this could get very complicated
for a non-trivial script. Here goes:

The sed fragment attempts to change every occcurance of setenv to
export, then change every ' = ' to a '=' to make the assignments valid.

e.g. assuming a csh_script_name of the form:

#! /bin/csh -f
setenv testarg1 = "this is test 1"
setenv testarg2 = "this is test
2"                                              

Sample code might be...

-----------8<-------------
#!/bin/ksh
echo test1
sed 's/setenv/export/; s/ = /=/' csh_script_name >
mangled_csh_script.tmp

[ ! -f mangled_csh_script.tmp ] && echo "Eek! Bailing out..." && exit 1

. mangled_csh_script.tmp # '.' means 'source'
rm -f mangled_csh_script.tmp

echo test2
-----------8<-------------

Very messy, consumes a lot of resources, and is very fallible. A better
approach in the long run might be just to convert the csh file into a
ksh file.

Sorry I couldn't be of more help. Csh isn't really my forte. ;-)

Cheers,

Dave. :-)

 
 
 

How to "source" a csh script from ksh script

Post by Ken Pizzi » Sat, 10 Jun 2000 04:00:00



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

In the general case, not really, but...

Quote:>  The csh_script_name script attempts to set environment variables.

If that's all that's needed, then:
  #ksh stuff...
  eval $(csh -c 'source script; printenv' | sed 's/^/export /')
  #more ksh stuff...
will get you pretty close.  This doesn't handle some things
correctly (e.g., if a value has a space in it), but it can
probably serve as a starting point.

                --Ken Pizzini

 
 
 

How to "source" a csh script from ksh script

Post by Dan Merc » Sat, 10 Jun 2000 04:00:00




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

Nontrivial:

#!/usr/bin/ksh

[[ $SOURCED_CSH -ne 1 ]] && {
   export SOURCED_CSH=1
   exec /usr/bin/csh -c "source csh_script_name;exec $0"
   }

echo "SOMEVAR=$SOMEVAR"

Now,  some other notes:

   only exported vars will be available to the ksh script -
   that is,  only setenv's not set vars.

   If you want to process args,  you should do it before sourcing.

   The above will also source .cshrc - that may produce odd side
   effects.  To suppress that,  change the exec line to:

   exec /usr/bin/csh -fc "source csh_script_name;exec $0"

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

How to "source" a csh script from ksh script

Post by brian hile » Sat, 10 Jun 2000 04:00:00



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

This is quite adequately answered by my response to the thread
entitled "Scheme for setting up env in either sh" by "Joe & Laura".
As I wrote a sophisticated csh to ksh translator script, I came up
against exactly this design decision, so I can consider myself rather
the expert on this little topic.

There is very rarely any formal necessity to source one language's
code from within another. Generally what is _really_ desired is to
propagate the environment from one to another, and for which
software tools and techniques see the aforementioned thread.

-Brian

 
 
 

1. Condition checking "<=" and ">=" in ksh script

Hi all,

Have a quick question, hope you can helps...
I'm trying to write a ksh script that can do the following:

   IF ( "8.1" <=   i    < "9.1" )
           print message 1
   END IF

Here is my script, but it seems does not give me the correct result that
i want.  And, "-ge" and "-lt" seems not working for ksh shell.  Look
forward to hearing from you.  Thanks.

   #!/bin/ksh
   j=CDMA
   i="$1"
   echo $i
   echo $j

   if [[ "$j" = "CDMA" && "$i" -ge 8.1 && "$i" -lt "9.2" ]]
   then
           echo "yes"
   else
           echo "no"
   fi

Best regards
kit

2. Debugging Jumpstart

3. "man2ascii"-script ( was: Re: Is there a "man2ascii"-script???? )

4. Provider is Netcom-How do I get email?

5. Sourcing a csh script from a ksh script

6. show file ages -- better than ls -l

7. csh: how to redirect I/O to/from "source" or "eval"

8. wu-ftpd 2.6.1

9. Did I *dream* that in sh, "." == csh's "source"?

10. Using "if" in "ksh" to change "for loop" values....

11. GETSERVBYNAME()????????????????????"""""""""""""

12. "NEED script" "will pay at least $50" NOTICE:

13. A script to change "string1" to "string2" in 50 files.