1#! /bin/bash 2# 3# cshtobash - convert csh aliases, environment variables, and variables to 4# bash equivalents 5# 6# usage: cshtobash [filename] 7# 8# If filename is given, that file is sourced. Note that csh always 9# sources .cshrc. To recreate your csh login environment, run 10# `cshtobash ~/.login'. 11# 12# Inspired by (and some borrowed from) a similar program distributed with 13# zsh-3.0. 14# 15# Chet Ramey 16# chet@po.cwru.edu 17# 18trap 'rm -f /tmp/cb$$.? cshout cshin' 0 1 2 3 6 15 19 20T=$'\t' 21 22SOURCE="${1:+source $1}" 23 24cat << EOF >cshin 25$SOURCE 26alias >! /tmp/cb$$.a 27setenv >! /tmp/cb$$.e 28set >! /tmp/cb$$.v 29EOF 30 31# give csh a minimal environment, similar to what login would provide 32/usr/bin/env - USER=$USER HOME=$HOME PATH=/usr/bin:/bin:/usr/ucb:. TERM=$TERM SHELL=$SHELL /bin/csh -i < ./cshin > cshout 2>&1 33 34# First convert aliases 35 36cat << \EOF >/tmp/cb$$.1 37mkalias () 38{ 39 case $2 in 40 '') echo alias ${1}="''" ;; 41 *[#\!]*) 42 comm=$(echo $2 | sed 's/\!\*/"$\@"/g 43 s/\!:\([1-9]\)/"$\1"/g 44 s/#/\#/g') 45 echo $1 \(\) "{" command "$comm" "; }" 46 ;; 47 *) echo alias ${1}=\'$(echo "${2}" | sed "s:':'\\\\'':")\' ;; 48 esac 49} 50EOF 51 52sed "s/^\([a-zA-Z0-9_]*\)$T\(.*\)$/mkalias \1 '\2'/" < /tmp/cb$$.a >>/tmp/cb$$.1 53 54echo '# csh aliases' 55echo 56 57$BASH /tmp/cb$$.1 | sed -e 's/\$cwd/\$PWD/g' \ 58 -e 's/\$term/\$TERM/g' \ 59 -e 's/\$home/\$HOME/g' \ 60 -e 's/\$user/\$USER/g' \ 61 -e 's/\$prompt/\$PS1/g' 62 63# Next, convert environment variables 64echo 65echo '# csh environment variables' 66echo 67 68# Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ... 69sed -e '/^SHLVL/d' \ 70 -e '/^PWD/d' \ 71 -e "s/'/'"\\\\"''"/g \ 72 -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \ 73 -e "s/$/'/" < /tmp/cb$$.e 74 75# Finally, convert local variables 76echo 77echo '# csh variables' 78echo 79 80sed -e 's/'"$T"'/=/' \ 81 -e "s/'/'"\\\\"''"/g \ 82 -e '/^[A-Za-z0-9_]*=[^(]/{ 83 s/=/='"'/"' 84 s/$/'"'/"' 85 }' < /tmp/cb$$.v | 86sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \ 87 -e '/^verbose=/d' \ 88 -e '/^term=/d' \ 89 -e '/^home=/d' \ 90 -e '/^path=/d' \ 91 -e '/^user=/d' \ 92 -e '/^shell=/d' \ 93 -e '/^cdpath=/d' \ 94 -e '/^mail=/d' \ 95 -e '/^home=/s//HOME=/' \ 96 -e '/^prompt=/s//PS1=/' \ 97 -e '/^histfile=/s//HISTFILE=/' \ 98 -e '/^history=/s//HISTSIZE=/' \ 99 -e '/^savehist=$/s//HISTFILESIZE=${HISTSIZE-500}/' \ 100 -e '/^savehist=/s//HISTFILESIZE=/' \ 101 -e '/^ignoreeof=$/s/^.*$/set -o ignoreeof # ignoreeof/' \ 102 -e '/^ignoreeof=/s//IGNOREEOF=/' \ 103 -e '/^noclobber=/s/^.*$/set -C # noclobber/' \ 104 -e '/^notify=/s/^.*$/set -b # notify/' \ 105 -e '/^noglob=/s/^.*$/set -f # noglob/' \ 106 107 108# now some special csh variables converted to bash equivalents 109echo 110echo '# special csh variables converted to bash equivalents' 111echo 112 113sed -e 's/'"$T"'/=/' < /tmp/cb$$.v | 114grep "^cdpath=" | 115sed 's/(// 116 s/ /:/g 117 s/)// 118 s/cdpath=/CDPATH=/' 119 120 121sed -e 's/'"$T"'/=/' < /tmp/cb$$.v | 122grep "^mail=" | 123sed 's/(// 124 s/ /:/g 125 s/)// 126 s/mail=/MAILPATH=/' | 127sed -e 's/MAILPATH=\([0-9][0-9][^:]*\)$/MAILCHECK=\1/' \ 128 -e 's/MAILPATH=\([0-9][0-9][^:]*\):\(.*\)/MAILCHECK=\1 MAILPATH=\2/' 129 130exit 0 131