Patch-ID: bash41-003
[bash.git] / examples / misc / cshtobash
blobce49bfc9f0b1df8f280d95938cf7a6a78434331e
1 #! /bin/bash
3 # cshtobash - convert csh aliases, environment variables, and variables to
4 # bash equivalents
6 # usage: cshtobash [filename]
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'.
12 # Inspired by (and some borrowed from) a similar program distributed with
13 # zsh-3.0.
15 # Chet Ramey
16 # chet@po.cwru.edu
18 trap 'rm -f $TMPFILE1 $TMPFILEa $TMPFILEe $TMPFILEv $TMPFILEco $TMPFILEci' 0 1 2 3 6 15
20 { TMPFILE1=$(mktemp -t cb.1.XXXXXX) &&
21 TMPFILEa=$(mktemp -t cb.a.XXXXXX) &&
22 TMPFILEe=$(mktemp -t cb.e.XXXXXX) &&
23 TMPFILEv=$(mktemp -t cb.v.XXXXXX) &&
24 TMPFILEco=$(mktemp -t cshout.XXXXXX) &&
25 TMPFILEci=$(mktemp -t cshin.XXXXXX)
26 } || exit 1
29 T=$'\t'
31 SOURCE="${1:+source $1}"
33 cat << EOF >$TMPFILEci
34 $SOURCE
35 alias >! $TMPFILEa
36 setenv >! $TMPFILEe
37 set >! $TMPFILEv
38 EOF
40 # give csh a minimal environment, similar to what login would provide
41 /usr/bin/env - USER=$USER HOME=$HOME PATH=/usr/bin:/bin:/usr/ucb:. TERM=$TERM SHELL=$SHELL /bin/csh -i < $TMPFILEci > $TMPFILEco 2>&1
43 # First convert aliases
45 cat << \EOF >$TMPFILE1
46 mkalias ()
48 case $2 in
49 '') echo alias ${1}="''" ;;
50 *[#\!]*)
51 comm=$(echo $2 | sed 's/\!\*/"$\@"/g
52 s/\!:\([1-9]\)/"$\1"/g
53 s/#/\#/g')
54 echo $1 \(\) "{" command "$comm" "; }"
56 *) echo alias ${1}=\'$(echo "${2}" | sed "s:':'\\\\'':")\' ;;
57 esac
59 EOF
61 sed "s/^\([a-zA-Z0-9_]*\)$T\(.*\)$/mkalias \1 '\2'/" < $TMPFILEa >>$TMPFILE1
63 echo '# csh aliases'
64 echo
66 $BASH $TMPFILE1 | sed -e 's/\$cwd/\$PWD/g' \
67 -e 's/\$term/\$TERM/g' \
68 -e 's/\$home/\$HOME/g' \
69 -e 's/\$user/\$USER/g' \
70 -e 's/\$prompt/\$PS1/g'
72 # Next, convert environment variables
73 echo
74 echo '# csh environment variables'
75 echo
77 # Would be nice to deal with embedded newlines, e.g. in TERMCAP, but ...
78 sed -e '/^SHLVL/d' \
79 -e '/^PWD/d' \
80 -e "s/'/'"\\\\"''"/g \
81 -e "s/^\([A-Za-z0-9_]*=\)/export \1'/" \
82 -e "s/$/'/" < $TMPFILEe
84 # Finally, convert local variables
85 echo
86 echo '# csh variables'
87 echo
89 sed -e 's/'"$T"'/=/' \
90 -e "s/'/'"\\\\"''"/g \
91 -e '/^[A-Za-z0-9_]*=[^(]/{
92 s/=/='"'/"'
93 s/$/'"'/"'
94 }' < $TMPFILEv |
95 sed -e '/^argv=/d' -e '/^cwd=/d' -e '/^filec=/d' -e '/^status=/d' \
96 -e '/^verbose=/d' \
97 -e '/^term=/d' \
98 -e '/^home=/d' \
99 -e '/^path=/d' \
100 -e '/^user=/d' \
101 -e '/^shell=/d' \
102 -e '/^cdpath=/d' \
103 -e '/^mail=/d' \
104 -e '/^home=/s//HOME=/' \
105 -e '/^prompt=/s//PS1=/' \
106 -e '/^histfile=/s//HISTFILE=/' \
107 -e '/^history=/s//HISTSIZE=/' \
108 -e '/^savehist=$/s//HISTFILESIZE=${HISTSIZE-500}/' \
109 -e '/^savehist=/s//HISTFILESIZE=/' \
110 -e '/^ignoreeof=$/s/^.*$/set -o ignoreeof # ignoreeof/' \
111 -e '/^ignoreeof=/s//IGNOREEOF=/' \
112 -e '/^noclobber=/s/^.*$/set -C # noclobber/' \
113 -e '/^notify=/s/^.*$/set -b # notify/' \
114 -e '/^noglob=/s/^.*$/set -f # noglob/' \
117 # now some special csh variables converted to bash equivalents
118 echo
119 echo '# special csh variables converted to bash equivalents'
120 echo
122 sed -e 's/'"$T"'/=/' < $TMPFILEv |
123 grep "^cdpath=" |
124 sed 's/(//
125 s/ /:/g
126 s/)//
127 s/cdpath=/CDPATH=/'
130 sed -e 's/'"$T"'/=/' < $TMPFILEv |
131 grep "^mail=" |
132 sed 's/(//
133 s/ /:/g
134 s/)//
135 s/mail=/MAILPATH=/' |
136 sed -e 's/MAILPATH=\([0-9][0-9][^:]*\)$/MAILCHECK=\1/' \
137 -e 's/MAILPATH=\([0-9][0-9][^:]*\):\(.*\)/MAILCHECK=\1 MAILPATH=\2/'
139 exit 0