6

Programming in C shell is considered harmful by some people, but csh scripts are continued to be written by some hardcore csh fans. Is there a free/open source converter to translate csh script into bash?

If such a program does not exist, is it possible (from a purely technical consideration) to write one?

  • 1
    Well, your second question is fairly easy: yes, it's possible to write one: bash is Turing complete. Now, converting to idiomatic bash, much harder. But that's off-topic for Unix & Linux, I'd think. – derobert Dec 20 '16 at 19:50
  • I was thinking along the lines of traversing the parse tree in csh to generate bash source code. Not sure how readable the generated code might turn out to be. Seems like an interesting problem :) – Gowtham Dec 21 '16 at 10:49
  • Getting it to be readable but awkward is probably a sane amount of work, then going for idiomatic is where all the time will go. Of course, as long as you're not trying to source the script, you can freely call csh scripts from bash (and vice versa), so there isn't really a need to convert them most of the time. – derobert Dec 21 '16 at 13:24
  • (You could also try csh to Perl, idiomatic would be much harder, but just "it works" would probably be easier, especially if you use IPC::Run). – derobert Dec 21 '16 at 13:26
2

I have two very distinct parts to this answer. I'll start with the slightly more technical bit, and then with more of personal thought (ok, rant).


The bash shell comes with a cshtobash script hidden away in the examples/misc directory of its source distribution.

This will not take a csh script and convert it into bash, but it may possibly be useful for someone as a first step in creating a prototype sh-style ~/.profile file from an already existing csh-style ~/.login file.

Input: Standard OpenBSD csh initialization file.

# $OpenBSD: dot.login,v 1.6 2015/12/15 16:37:58 deraadt Exp $
#
# csh login file

if ( ! $?TERMCAP ) then
        if ( $?XTERM_VERSION ) then
                tset -IQ '-munknown:?vt220' $TERM
        else
                tset -Q '-munknown:?vt220' $TERM
        endif
endif

stty    newcrt crterase

set     savehist=100
set     ignoreeof

setenv  EXINIT          'set ai sm noeb'

if (-x /usr/games/fortune) /usr/games/fortune

Output: Not quite the same thing for bash... but at least something.

# csh aliases


# csh environment variables

export USER='me'
export HOME='/home/me'
export PATH='/usr/bin:/bin:/usr/ucb:.'
export TERM='rxvt'
export SHELL='/bin/bash'
export EXINIT='set ai sm noeb'

# csh variables

set -o ignoreeof # ignoreeof
PS1='% '
prompt2='? '
HISTFILESIZE='100'

# special csh variables converted to bash equivalents

Yes, C-shell programming is "considered harmful". That does not mean that you can't write C-shell shell scripts that are not harmful, or not robust. It also does not mean that every single C-shell script needs to be converted to bash. And it definitely doesn't mean that such a conversion should be done by an automatic tool! It just means it's an awkward language that has some really odd quirks, especially if you're more used to sh.

As with any two programming languages, when one falls out of fashion to the benefit of the other, this does not mean that existing software needs to be rewritten. It also doesn't mean that proficient speakers of the out-of-fashion language should stop speaking. It just means that this may not be the language to pick up if you're looking for a modern language to do shell scripting in.

On the other hand, if you're given the task of extending or in some other way modify a csh script, then there might be a reason to

  1. read the code
  2. understand it
  3. reimplement it in another language

... especially if you're not good at csh programming.

As for automatic translation: Why?

If the script is working and you have the interpreter for it on your system, there is no need for translate the code. Translating the code automatically is unlikely to generate "better" code, and it very likely won't generate maintainable code, and you'd have to spend time on verifying that the translated script behaves in the same way as the original script anyway.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.