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: Mun
Subject: Re: [Help-bash] bash to csh converter?
Date: Wed, 23 Mar 2016 12:39:50 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Greg,

On Wed, Mar 23, 2016 at 05:42 AM PDT, Greg Wooledge wrote:
GW> On Tue, Mar 22, 2016 at 09:09:05PM -0600, Bob Proulx wrote:
GW> >   exec tcsh -c 'source environ.csh; exec bash'
GW> > [...]
GW> > It's a
GW> > hack but a useful one considering the other constraints.  This is for
GW> > the command line.  Could be set up as a bash alias.
GW> 
GW> Now that I know we're talking about "source me" config files, rather
GW> than scripts....
GW> 
GW> The traditional alternative is to write two different config files --
GW> one with (fully portable) sh syntax and one with csh syntax.  Source
GW> the appropriate one based on which shell the user is in.
GW> 
GW> The other alternative that I can think of would be to write a single
GW> config file that detects which shell is interpreting it, and issues the
GW> appropriate commands for that shell.  In essence, it would be two wholly
GW> separate config files, in the same physical file, with some voodoo code
GW> to skip to the correct section.  I've never actually seen this done in
GW> real life, but I've seen academic exercises to support the concept.(*)

I thought of those two alternatives as well; and feel the first is more
to my liking.  The approach of supporting multiple shell interpreters in
one file makes me cringe and gives me negative points for supportability ;)

GW> Here is something I came up with just now:
GW> 
GW> ================================================================
GW> imadev:~$ cat x
GW> #!/bin/echo source_me_dont_run_me!
GW> 
GW> unset status; false; test "$status" = 1 && goto csh_version
GW> 
GW> # POSIX sh version
GW> export JAVA_HOME=ugh
GW> export ORACLE_HOME=bleh
GW> return
GW> 
GW> csh_version:
GW> setenv JAVA_HOME ugh
GW> setenv ORACLE_HOME bleh
GW> ================================================================
GW> 
GW> imadev:~$ chmod +x x
GW> imadev:~$ ./x
GW> source_me_dont_run_me! ./x
GW> 
GW> Sourcing in bash:
GW> imadev:~$ unset ORACLE_HOME JAVA_HOME
GW> imadev:~$ echo "$BASH_VERSION"
GW> 4.4.0(1)-rc1
GW> imadev:~$ source ./x
GW> imadev:~$ declare -p ORACLE_HOME JAVA_HOME
GW> declare -x ORACLE_HOME="bleh"
GW> declare -x JAVA_HOME="ugh"
GW> 
GW> Sourcing in /bin/sh (on HP-UX, it's a ksh "lite"):
GW> $ unset ORACLE_HOME JAVA_HOME
GW> $ . ./x
GW> $ env | grep _HOME
GW> JAVA_HOME=ugh
GW> ORACLE_HOME=bleh
GW> 
GW> Sourcing in csh:
GW> % unsetenv ORACLE_HOME JAVA_HOME
GW> % source ./x
GW> % env | grep _HOME
GW> JAVA_HOME=ugh
GW> ORACLE_HOME=bleh
GW> 
GW> Sourcing in tcsh:
GW> > unsetenv ORACLE_HOME JAVA_HOME
GW> > source ./x
GW> > env | grep _HOME
GW> JAVA_HOME=ugh
GW> ORACLE_HOME=bleh
GW> 
GW> Obviously this is not the path that most people take.  If you feel
GW> safer writing two config files, which is the traditional approach,
GW> that's perfectly fine.

Thanks for the example; I will keep it handy just in case I need to
refer to it in the future.

Regards,

-- 
Mun


GW> (*) For fun, check these out:
GW> 
GW> http://stchaz.free.fr/which_interpreter
GW> 
https://github.com/stephane-chazelas/misc-scripts/blob/master/which_interpreter
GW> 
GW> Similar concept, taken to extreme levels.  I don't understand more than
GW> a tiny piece of it.



reply via email to

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