Last active
June 7, 2021 03:59
-
-
Save jovannic/9239591 to your computer and use it in GitHub Desktop.
sh2csh.sed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sed | |
# i can do 90% of the csh conversion work in batch, automagically! | |
# ...he said, 3 hours ago. | |
# ...but f*** all, it works! (does all my conversion, besides functions) | |
# change the shell to csh | |
1s|#!.*|#!/bin/csh -f| | |
# change if/while brackets | |
/^\s*\(if\|elif\|while\) / { | |
s/elif \[ /else if ( / | |
s/if \[ /if ( / | |
s/while \[ /while ( / | |
s/ \]; then/ ) then/ | |
s/ \]; do/ )/ | |
} | |
/^\s*fi$/s/fi/endif/ | |
# change until into inverted while | |
/^\s*until / { | |
s/until \[ /while ( ! ( / | |
s/ \]; do/ ) )/ | |
} | |
# change for in -> foreach | |
/^\s*for / { | |
s/for /foreach / | |
s/ in / (/ | |
s/; do/)/ | |
} | |
# change tests | |
/^\s*\(if\|else if\|while\|foreach\) / { | |
s/-a/\&\&/ | |
s/-o/||/ | |
s/-eq/==/ | |
s/-ne/!=/ | |
s/-gt/>/ | |
s/-lt/</ | |
s/-le/<=/ | |
s/-ge/>=/ | |
s/\(\$\w\+\) = /"\1" == / | |
} | |
# loop ends, done -> end | |
/^\s*done/s/done/end/ | |
# convert case statements | |
/^\s*case / { | |
s/case /switch (/ | |
s/ in/)/ | |
} | |
/^\s*[^(]*)$/s/\(^\s*\)\(.*\))$/\1case \2:/ | |
/^\s*case /s/\(^\s*\)case \([^|]\+\)|\([^:]\+\):/\1case \2:\n\1case \3:/ | |
/^\s*\*)/s/\*)/default:/ | |
/^\s*esac/s/esac/endsw/ | |
/^\s*;;/s/;;/breaksw/ | |
# input reads | |
/^\s*read / { | |
s/read /set / | |
s/$/ = $</ | |
} | |
# assignments | |
/^\s*\w\+=/ { | |
s/\(^\s*\)\(\w\+\)=`expr/\1@ \2 = `expr/ | |
/`expr/ { | |
s/[\\]\*/*/ | |
s/`expr // | |
s/`$// | |
} | |
s/\(^\s*\)\(\w\+\)=/\1set \2 = / | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment