help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] bash to csh converter?


From: Greg Wooledge
Subject: Re: [Help-bash] bash to csh converter?
Date: Wed, 23 Mar 2016 08:42:15 -0400
User-agent: Mutt/1.4.2.3i

On Tue, Mar 22, 2016 at 09:09:05PM -0600, Bob Proulx wrote:
>   exec tcsh -c 'source environ.csh; exec bash'
> [...]
> It's a
> hack but a useful one considering the other constraints.  This is for
> the command line.  Could be set up as a bash alias.

Now that I know we're talking about "source me" config files, rather
than scripts....

The traditional alternative is to write two different config files --
one with (fully portable) sh syntax and one with csh syntax.  Source
the appropriate one based on which shell the user is in.

The other alternative that I can think of would be to write a single
config file that detects which shell is interpreting it, and issues the
appropriate commands for that shell.  In essence, it would be two wholly
separate config files, in the same physical file, with some voodoo code
to skip to the correct section.  I've never actually seen this done in
real life, but I've seen academic exercises to support the concept.(*)

Here is something I came up with just now:

================================================================
imadev:~$ cat x
#!/bin/echo source_me_dont_run_me!

unset status; false; test "$status" = 1 && goto csh_version

# POSIX sh version
export JAVA_HOME=ugh
export ORACLE_HOME=bleh
return

csh_version:
setenv JAVA_HOME ugh
setenv ORACLE_HOME bleh
================================================================

imadev:~$ chmod +x x
imadev:~$ ./x
source_me_dont_run_me! ./x

Sourcing in bash:
imadev:~$ unset ORACLE_HOME JAVA_HOME
imadev:~$ echo "$BASH_VERSION"
4.4.0(1)-rc1
imadev:~$ source ./x
imadev:~$ declare -p ORACLE_HOME JAVA_HOME
declare -x ORACLE_HOME="bleh"
declare -x JAVA_HOME="ugh"

Sourcing in /bin/sh (on HP-UX, it's a ksh "lite"):
$ unset ORACLE_HOME JAVA_HOME
$ . ./x
$ env | grep _HOME
JAVA_HOME=ugh
ORACLE_HOME=bleh

Sourcing in csh:
% unsetenv ORACLE_HOME JAVA_HOME
% source ./x
% env | grep _HOME
JAVA_HOME=ugh
ORACLE_HOME=bleh

Sourcing in tcsh:
> unsetenv ORACLE_HOME JAVA_HOME
> source ./x
> env | grep _HOME
JAVA_HOME=ugh
ORACLE_HOME=bleh

Obviously this is not the path that most people take.  If you feel
safer writing two config files, which is the traditional approach,
that's perfectly fine.

(*) For fun, check these out:

http://stchaz.free.fr/which_interpreter
https://github.com/stephane-chazelas/misc-scripts/blob/master/which_interpreter

Similar concept, taken to extreme levels.  I don't understand more than
a tiny piece of it.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]