Skip to content

Instantly share code, notes, and snippets.

@jovannic
Last active June 7, 2021 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovannic/9239591 to your computer and use it in GitHub Desktop.
Save jovannic/9239591 to your computer and use it in GitHub Desktop.
sh2csh.sed
#!/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