How to source csh-file in korn shell ?

How to source csh-file in korn shell ?

Post by Jerry X » Sun, 04 Aug 1996 04:00:00



I am using Korn Shell. I have a c shell file which sets several environment
variables. People using c shell can source that file. But I am in k shell.
I tried . filename, but korn shell can't recognize those c shell syntax
though there is a #!/bin/csh line in that c shell file.

Anybody has any suggestion ?

Thanks in advance.

Jerry

 
 
 

How to source csh-file in korn shell ?

Post by Bill Marc » Tue, 06 Aug 1996 04:00:00



>I am using Korn Shell. I have a c shell file which sets several environment
>variables. People using c shell can source that file. But I am in k shell.
>I tried . filename, but korn shell can't recognize those c shell syntax
>though there is a #!/bin/csh line in that c shell file.

>Anybody has any suggestion ?

sed 's/^ *setenv //
s/^ *set //' file >newfile
--

"Outside of a dog, a book is man's best friend.  Inside a dog, it's too
dark to read." -- Marx

 
 
 

How to source csh-file in korn shell ?

Post by Brian S Hil » Wed, 07 Aug 1996 04:00:00


: I am using Korn Shell. I have a c shell file which sets several environment
: variables. People using c shell can source that file. But I am in k shell.
: I tried . filename, but korn shell can't recognize those c shell syntax
: though there is a #!/bin/csh line in that c shell file.

There are a number of scripts, source programs, and documents available
that discuss this particular problem.

ftp.wg.omron.co.jp in /pub/unix-faq/docs/script-vs-env. # "script-vs-env" doc

ftp ftp.uu.net/archive/comp.sources.unix/volume13/korner
korner                  Convert (some) csh scripts to ksh scripts

ftp ftp.uu.net/archive/comp.sources.unix/volume42/envv/*
envv/patch01            envv - handle env. variables in shell-independent way
envv-1.2/part01         envv-1.2 - handle env. vars in shell-independent way
envv-1.2/patch[1-4]     envv-1.2 - handle env. vars in shell-independent way

I recommend that you peruse all the above sources available to you,
especially the first, as it describes the nature of the problem as well
as solving it for a variety of shells.

In addition, here are some scripts that convert one shell's environment
to another's (usually csh to ksh/bash/zsh).

function csh2bash_alias ()
# convert csh alias: eg: alias dir 'ls -al \!* | more'

{       if [ "x$2" = "x" ]
        then    declare -f $1
        else    echo $2 | egrep -q '(\!|#)'
                if [ $? -eq 0 ]

                                               s/\\!:\([1-9]\)/\"$\1\"/g
                                               s/#/\\#/g')

                fi
                eval function $1 \(\) "{" command "$comm"  "}"
        fi

Quote:}

#! /bin/sh
#
# Convert Csh aliases to Bash aliases.  Adapted from a similar program
# supplied with zsh.
#
# This is a quick script to convert csh aliases to Bash aliases/functions.
# Pipe the output of csh's alias command through this; it will generate
# a series of alias/function definitions on stdout, suitable for
# processing by bash.
#
# This is not perfect, but it gets most common aliases; it should manage to
# cut down a lot of the busy work.
#
sed -e 's/      (\(.*\))/       \1/' >/tmp/cz$$.1
grep ! /tmp/cz$$.1 >/tmp/cz$$.2
grep -v ! /tmp/cz$$.1 >/tmp/cz$$.3
sed -e "s/'/'"\\\\"''"/g -e 's/^\([^        ]*\)    \(.*\)$/alias \1='"'\2'/" \
        /tmp/cz$$.3
sed -e 's/![:#]*/$/g' -e 's/^\([^       ]*\)    \(.*\)$/\1 () { \2 }/' /tmp/cz$$.2
rm /tmp/cz$$.?
exit 0

#!/bin/ksh
# c2z - environment conversion tool
# Contributed by Bart Schaefer
# (Tweaked a bit by Paul Falstad)
#
# This is a quick script to convert csh aliases to zsh aliases/functions.
# It also converts the csh environment and local variables to zsh.  c2z
# uses the csh to parse its own dot-files, then processes csh output to
# convert the csh settings to zsh.
#
# When run as a zsh fuction, c2z runs csh as if it were an interactive
# shell whenever the parent zsh is interactive.  When run as a shell
# script, the -i switch can be used to force this behavior.
#
# The -l (login) switch causes csh to run as if it were a login shell.
# This is done "properly" if c2z is used as a zsh function, otherwise
# it's faked by explicitly sourcing .login.  Use with caution if your
# .login initializes an X server or does other one-time-only startup
# procedures.
#
# usage:
#       c2z [-i] [-l]
#
# You can use this script in your .zshrc or .zlogin files to load your
# regular csh environment into zsh; for example, in .zlogin:
#
#       . =(c2z -l)
#
# This is not perfect, but it gets most common aliases and variables.
# It's also rather time-consuming to do this every time you log in.
# However, if you're moving from csh to zsh for the first time, this
# can get you started with a familiar environment right away.
#
# In case your mailer eats tabs, $T is set to expand to a tab.
#
T="`echo x | tr x '\011'`"
# If we're zsh, we can run "- csh" to get the complete environment.
#
MINUS=""
LOGIN=""
INTERACT=""
case "$VERSION" in
zsh*)
    case $1 in
    -l*) MINUS="-" ;;
    -i*) INTERACT="-i" ;;
    esac
    if [[ -o INTERACTIVE ]]; then INTERACT="-i"; fi
    setopt nobanghist
    ;;
*)
    case $1 in
    -l*) LOGIN="source ~/.login" ;;
    -i*) INTERACT="-i" ;;
    esac
    ;;
esac
( eval $MINUS csh $INTERACT ) <<EOF 2>&1 >/dev/null
$LOGIN
alias >! /tmp/cz$$.a
setenv >! /tmp/cz$$.e
set >! /tmp/cz$$.v
EOF

# save stdin
exec 9<&0

# First convert aliases
exec < /tmp/cz$$.a

# Taken straight from ctoz except for $T and "alias --"
sed -e 's/'"$T"'(\(.*\))/'"$T"'\1/' >/tmp/cz$$.1
grep ! /tmp/cz$$.1 >/tmp/cz$$.2
grep -v ! /tmp/cz$$.1 >/tmp/cz$$.3
sed -e "s/'/'"\\\\"''"/g \
    -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/alias -- \1='"'\2'/" \
    /tmp/cz$$.3
sed -e 's/![:#]*/$/g' \
    -e 's/^\([^'"$T"']*\)'"$T"'\(.*\)$/\1 () { \2 }/' \
    /tmp/cz$$.2

# Next, convert environment variables
exec < /tmp/cz$$.e

# Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
sed -e '/^SHLVL/d' \
    -e "s/'/'"\\\\"''"/g \
    -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
    -e "s/$/'/"

# Finally, convert local variables
exec < /tmp/cz$$.v

sed -e 's/'"$T"'/=/' \
    -e "s/'/'"\\\\"''"/g \
    -e '/^[A-Za-z0-9_]*=[^(]/{
        s/=/='"'/"'
        s/$/'"'/"'
        }' |
sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
         -e '/^histchars=/s//HISTCHARS=/' \
         -e '/^history=/s//HISTSIZE=/' \
         -e '/^home=/s//HOME=/' \
         -e '/^ignoreeof=/s/.*/setopt ignoreeof/' \
         -e '/^noclobber=/s/.*/setopt noclobber/' \
         -e '/^notify=/d' \
         -e '/^showdots=/s/.*/setopt globdots/' \
    -e '/^savehist=/s//HISTFILE=\~\/.zhistory SAVEHIST=/' \
         -e '/^autolist=/s/.*/setopt autolist/' \
         -e '/^correct=[cmd]*/s//setopt autocorrect/' \
         -e '/^who=/s//WATCHFMT=/'

exec 0<&9
rm /tmp/cz$$.?
exit

-Brian
--
   ,---.     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.  
  /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \  

__,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.__

 
 
 

1. csh source in korn shell

Hello all,

I am transferring some system scripts from csh to ksh and have been
having problems doing a "source" in the korn shell.  I have tried eval
and exec and neither one seems to do what source from the csh does.
When I exec the file from a Solaris (2.5) machine running Openwin, the
file executes and then closes the window.  It seems to be running the
program in the shell without forking a new process (the desired action)
but then it exits the shell, killing the window with it.  I can get
around this by putting a /bin/ksh as the last line of the file I want to
exec but this looses all of the previous history etc. (although it does
set the variables I want set).

What I am trying to do is to export global variable values to the
current running shell from a file.  Is there a better way to do this?

Thanks in advance....

Dave Bentzen

---------------------------------------------------------------
Dave Bentzen          Senior Engineer
Itron, Inc                  Spokane, WA

If you get to thinkin' you're a person of some influence, try orderin'
somebody else's dog around. - Bix Bender

2. Howto Redhat setup external SCSI tape drive. Looking for generic procedure.

3. best korn shell resources and is there a korn shell faq

4. HELP!!! Mandra Single Network Firewall 7.2 DRIVING ME CRAZY!!

5. csh "source": is there a korn equivalent?

6. S3-Trio64 and XF86_S3`

7. Korn shell emulation of Csh's !? ???

8. Fvwm Config.

9. How to "#!/bin/csh -f" in Korn shell

10. How to attach file to EMAIL MESSAGE within shell script(Korn shell)

11. csh-> korn shell alias

12. Windowing Korn Shell (wksh) source

13. Korn shell source for XENIX?