免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6935 | 回复: 6
打印 上一主题 下一主题

把csh,sh或ksh语法的脚本相互转换的程序  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2002-09-16 10:35 |只看该作者 |倒序浏览
[这个贴子最后由valentine在 2002/09/16 11:34am 编辑]

我有一大堆csh的脚本想要转换成sh下面可以用的
想自己写又太菜,没有头绪,老大们帖一篇出来供大家参考参考
谢先!

论坛徽章:
0
2 [报告]
发表于 2002-09-16 11:33 |只看该作者

把csh,sh或ksh语法的脚本相互转换的程序

这是我找到的一个csh2ksh converter.
你可以用一下看.
#!/usr/bin/nawk -f
#*TAG:35739 34:Apr 17 1996:0755:sh.d/csh2ksh:csh2ksh:
# csh2ksh 1.4
#************************************************************************
# Original byaniel Zepeda (daniel.zepeda@waii.com)
# Enhanced version by:Brian Hiles (bsh20858@challenger.atc.fhda.edu)
#
# This script will change a C-Shell script into a Korn Shell script.
# usage: csh2ksh <script.csh>
#
# Obviously, this script isn't going to pick up every little thing.
# You can do yourself a big favor by splitting multiple commands per line.
# Makes a half-hearted attempt to substitute multiple commands per line.
#************************************************************************

# glob (ksh N/I), hash (csh N/I), rehash, hashstat (N/I)
# get this to work on /usr/bin/vgrind, /usr/local/src/tcpdump-3.0/configure
# Each occurrence of a foreach, switch, while, if...then and else built-in
# must appear as the first word on its own input line.
# passthru!=&quot;ON&quot; &amp;&amp; /.../ {
# option to pass through/don't pass through unconvertable tokens
# Keep track of goto labels&#59; if encountered, turn into a
# function, else make into an &quot;exit&quot; (special case &quot;_bailout&quot.
# Strip comments, place in buffer to be re-appended to $0 at EOR.
# It has to be assumed that syntactic units will be EOL, semicolon delimited

BEGIN {
#XXX need to save IFS? (always reinitialized to ' \t\n')
# Any functs below may be elim'd if script does not use feature.
# However, make sure to elim the respective alias and declaration!
#
prevcase=-1
if (ENVIRON[&quot;passthru&quot;]!=&quot;&quot #XXX
# flag to pass through unsubstitutable tokens to output
print &quot;passthru=&quot; (passthru=&quot;ON&quot >&quot;/dev/tty&quot;
if (ENVIRON[&quot;casualvars&quot;]!=&quot;&quot
# flag to suppress the assumption of variables being arrays
print &quot;casualvars=&quot; (casualvars=&quot;ON&quot >&quot;/dev/tty&quot;
if (ENVIRON[&quot;nopreamble&quot;]!=&quot;&quot
# flag to suppress the output of the env preamble code
print &quot;nopreamble=&quot; (nopreamble=&quot;ON&quot >&quot;/dev/tty&quot;

if (nopreamble!=&quot;ON&quot
print &quot;#!ksh\n&quot; \
&quot;set -A argv '' \&quot;$@\&quot;\n&quot; \
&quot;home=$HOME mail=$MAIL prompt=$PS1 shell=$SHELL \\\n&quot; \
&quot;term=$TERM user=$USER _IFS=$IFS IFS=:\n&quot; \
&quot;set -A cdpath '' $CDPATH&#59; set -A path '' $PATH\n&quot; \
&quot;IFS=\&quot; $_IFS\&quot; alias chdir=cd login='exec /bin/login' \\\n&quot; \
&quot;nice='nice ' rehash='PATH=$PATH' unhash='set +o traceall'\n&quot; \
&quot;set -o nounset\n&quot; \
&quot;#\n## any of the following functions+attributes and/or aliases\n&quot; \
&quot;## may be eliminated if that feature is not used in this script\n#\n&quot; \
&quot;# avail to serve the usual purpose of a label at or near EOF\n&quot; \
&quot;function _bailout\n&quot; \
&quot;{\texit\n}\n&quot; \
&quot;# directory stack: nec to maintain correct dirstack upon chdir\n&quot; \
&quot;alias _cd=cd&#59; typeset -fx _cd\n&quot; \
&quot;function _cd\n&quot; \
&quot;{\t# make top of directory stack cwd\n&quot; \
&quot;\tset -o noglob +o nounset\n&quot; \
&quot;\t\\cd \&quot;$@\&quot; &amp;&amp; set +A _dirstack ~+/\n}\n&quot; \
&quot;# emulate the csh implementation of \&quot;limit\&quot;\n&quot; \
&quot;alias limit=_limit&#59; typeset -fx _limit\n&quot; \
&quot;function _limit\n&quot; \
&quot;{\teval ulimit -a ${1:+\| &quot; \
&quot;egrep \\\&quot;^(nofiles\\\\\\()?${1#cpu}\\\&quot; ||\n&quot; \
&quot;\tprint -ru2 limit -- No such limit}\n}\n&quot; \
&quot;# emulate the csh implementation of \&quot;shift\&quot;\n&quot; \
&quot;alias shift=_shift&#59; typeset -fx _shift\n&quot; \
&quot;function _shift\n&quot; \
&quot;{\tif [[ ${1-argv} = argv ]]\n&quot; \
&quot;\tthen\ttrap 'set -- \&quot;${argv[@]}\&quot;\n&quot; \
&quot;\t\t\\shift 2\n&quot; \
&quot;\t\tset -A argv \&quot;\&quot; \&quot;$@\&quot;' EXIT\n&quot; \
&quot;\telse\ttypeset _varx=$1\n&quot; \
&quot;\t\teval set -- \&quot;\\\&quot;\\${$1[@]?}\\\&quot;\&quot;\n&quot; \
&quot;\t\t\\shift\n&quot; \
&quot;\t\tset -A $_varx \&quot;$@\&quot;\n&quot; \
&quot;\tfi\n}\n&quot; \
&quot;# emulate the csh implementation of \&quot;source\&quot;\n&quot; \
&quot;typeset -fx _source\n&quot; \
&quot;function _source\n&quot; \
&quot;{\tif\tfile ${1source -- Too few arguments} 2>&amp;- |\n&quot; \
&quot;\t\tgrep -s '/bin/k?sh'\n&quot; \
&quot;\tthen\ttrap '. \&quot;$@\&quot;' EXIT\n&quot; \
&quot;\telse\tprint -u2 cannot source \\\&quot;$1\\\&quot; -- not k/sh script\n&quot; \
&quot;\t\texit 1\n&quot; \
&quot;\tfi\n}\n&quot; \
&quot;# directory stack: emulate the csh implementation of \&quot;dirs\&quot;\n&quot; \
&quot;typeset -fx dirs\n&quot; \
&quot;function dirs\n&quot; \
&quot;{\t# display directory stack\n&quot; \
&quot;\tset -o noglob +o nounset\n&quot; \
&quot;\ttypeset dir\n&quot; \
&quot;\t(($#>=2)) &amp;&amp;\n&quot; \
&quot;\t{\tprint -ru2 'dirs: Too many arguments'\n&quot; \
&quot;\t\treturn 1\n&quot; \
&quot;\t}\n&quot; \
&quot;\tif [[ $1 = -l ]]\n&quot; \
&quot;\tthen\tfor dir in ${_dirstack
  • :-$PWD/}\n&quot; \
    &quot;\t\tdo\tprint -nr -- \&quot;${dir%/} \&quot;\n&quot; \
    &quot;\t\tdone\n&quot; \
    &quot;\telse\tfor dir in ${_dirstack
  • :-$PWD/}\n&quot; \
    &quot;\t\tdo\tdir=${dir#$HOME/}\n&quot; \
    &quot;\t\t\tcase $dir in\n&quot; \
    &quot;\t\t\t('')\tprint -nr -- '~ ' &#59;&#59;\n&quot; \
    &quot;\t\t\t(/*)\tprint -nr -- \&quot;${dir%/} \&quot; &#59;&#59;\n&quot; \
    &quot;\t\t\t(*)\tprint -nr -- \&quot;~/${dir%/} \&quot; &#59;&#59;\n&quot; \
    &quot;\t\t\tesac\n&quot; \
    &quot;\t\tdone\n&quot; \
    &quot;\tfi\n&quot; \
    &quot;\tprint\n}\n&quot; \
    &quot;# directory stack: emulate the csh implementation of \&quot;popd\&quot;\n&quot; \
    &quot;typeset -fx popd\n&quot; \
    &quot;function popd\n&quot; \
    &quot;{\t# cd to and/or delete stack entry\n&quot; \
    &quot;\tset -o noglob +o nounset\n&quot; \
    &quot;\tif ((!$#))\n&quot; \
    &quot;\tthen\t# pop directory off the stack, cd to new top\n&quot; \
    &quot;\t\tif \\cd ${_dirstack[1]Directory stack empty.}\n&quot; \
    &quot;\t\tthen\tunset _dirstack[0]\n&quot; \
    &quot;\t\t\tset -A _dirstack ${_dirstack
  • }\n&quot; \
    &quot;\t\t\tdirs\n&quot; \
    &quot;\t\telse\tunset _dirstack[0]\n&quot; \
    &quot;\t\t\tset -A _dirstack ${_dirstack
  • }\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\tfi\n&quot; \
    &quot;\telif [[ $# = 1 &amp;&amp; $1 = ++([0-9]) ]]\n&quot; \
    &quot;\tthen\t# case of popd +n: delete nth dir from stack\n&quot; \
    &quot;\t\ttypeset -i ndir\n&quot; \
    &quot;\t\tset ${1#+}\n&quot; \
    &quot;\t\t# uncomment to use last element if param exceeds permissible:\n&quot; \
    &quot;\t\t#let \&quot;$1 > (ndir=${#_dirstack
  • }-1)\&quot; &amp;&amp; set -- $ndir\n&quot; \
    &quot;\t\tlet \&quot;$1 > ${#_dirstack
  • }-1\&quot; &amp;&amp;\n&quot; \
    &quot;\t\t{\tprint -u2 'popd: Directory stack not that deep.'\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\t}\n&quot; \
    &quot;\t\t(($1<=0)) &amp;&amp;\n&quot; \
    &quot;\t\t{\tprint -u2 'popd: Bad directory.'\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\t}\n&quot; \
    &quot;\t\tunset _dirstack[$1]\n&quot; \
    &quot;\t\tset -A _dirstack ${_dirstack
  • }\n&quot; \
    &quot;\t\tdirs\n&quot; \
    &quot;\telse\tprint -u2 'popd: Too many arguments.'\n&quot; \
    &quot;\t\treturn 1\n&quot; \
    &quot;\tfi\n}\n&quot; \
    &quot;# directory stack: emulate the csh implementation of \&quot;pushd\&quot;\n&quot; \
    &quot;typeset -fx pushd\n&quot; \
    &quot;function pushd\n&quot; \
    &quot;{\t# make new top of stack cwd\n&quot; \
    &quot;\tset -o noglob +o nounset\n&quot; \
    &quot;\tif ((!$#))\n&quot; \
    &quot;\tthen\t# case of pushd without args&#59; swap top two directories\n&quot; \
    &quot;\t\tif \\cd ${_dirstack[1]No other directory.} 2>&amp;-\n&quot; \
    &quot;\t\tthen\tset +A _dirstack ${_dirstack[1]} ${_dirstack[0]}\n&quot; \
    &quot;\t\t\tdirs\n&quot; \
    &quot;\t\telse\tprint -ru2 \\\n&quot; \
    &quot;\t\t\t\&quot;pushd: ${_dirstack[1]}: No such file or directory.\&quot;\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\tfi\n&quot; \
    &quot;\telif [[ $# = 1 &amp;&amp; $1 = ++([0-9]) ]]\n&quot; \
    &quot;\tthen\t# case of pushd +n: rotate left n times directory stack\n&quot; \
    &quot;\t\ttypeset -i ndir nrot=${1#+}\n&quot; \
    &quot;\t\ttypeset firstdir\n&quot; \
    &quot;\t\t# uncomment to use last element if param exceeds permissible:\n&quot; \
    &quot;\t\t#let \&quot;nrot+1>(ndir=${#_dirstack
  • })\&quot; &amp;&amp; let nrot=ndir-1\n&quot; \
    &quot;\t\tlet \&quot;nrot > (ndir=${#_dirstack
  • }-1)\&quot; &amp;&amp;\n&quot; \
    &quot;\t\t{\tprint -ru2 'pushd: Directory stack not that deep.'\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\t}\n&quot; \
    &quot;\t\t((nrot<=0)) &amp;&amp;\n&quot; \
    &quot;\t\t{\tprint -ru2 'pushd: +0: No such file or directory.'\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\t}\n&quot; \
    &quot;\t\twhile let '(nrot-=1)>=0'\n&quot; \
    &quot;\t\tdo\tfirstdir=${_dirstack[0]}\n&quot; \
    &quot;\t\t\tunset _dirstack[0]\n&quot; \
    &quot;\t\t\tset -A _dirstack ${_dirstack
  • } $firstdir\n&quot; \
    &quot;\t\tdone\n&quot; \
    &quot;\t\t\\cd ${_dirstack[0]} &amp;&amp; dirs\n&quot; \
    &quot;\telif (($#==1))\n&quot; \
    &quot;\tthen\t# one argument is directory to push\n&quot; \
    &quot;\t\tif \\cd ${1No such file or directory.} 2>&amp;-\n&quot; \
    &quot;\t\tthen\tset -A _dirstack ~+/ ${_dirstack
  • :-$OLDPWD/}\n&quot; \
    &quot;\t\t\tdirs\n&quot; \
    &quot;\t\telse\tprint -ru2 \&quot;pushd: $1: No such file or directory.\&quot;\n&quot; \
    &quot;\t\t\treturn 1\n&quot; \
    &quot;\t\tfi\n&quot; \
    &quot;\telse\tprint -ru2 'pushd: Too many arguments.'\n&quot; \
    &quot;\t\treturn 1\n&quot; \
    &quot;\tfi\n}\n\n&quot; \
    &quot;LINENO=0&quot;
    }

    {donescan=&quot;OFF&quot; }# like a &quot;next&quot; command, except ...

    /^[\t ]*($|#)/ {# empty line or comment
    # Suppress newlines between case patterns, so that
    # introduced escaped newlines will act appropriately.
    if (prevcase==NR-1)
    prevcase+=1
    else
    print
    next
    }

    {gsub(&quot;[\t ]+&#59;&quot;, &quot;&#59;&quot# so that &quot;command &#59;&quot; !~ /command[\t ]/
    if ($0!~/\\[\t ]+/)
    sub(&quot;[\t ]+$&quot;, &quot;&quot
    sub(&quot;&#59;$&quot;, &quot;&quot;)
    }

    /\$/ {# normalize and convert variable syntax
    #XXX must appear before `.*`
    gsub(&quot;\\${\\?&quot;, &quot;${#&quot;)&#59; gsub(&quot;\\$\\?&quot;, &quot;$#&quot;)
    gsub(&quot;\\${?#?[A-Za-z_][A-Za-z_0-9]+(\\[.*]\\])?}?&quot;, &quot;!<!&amp;!>!&quot;)
    gsub(&quot;\\$[1-9]&quot;, &quot;!<!&amp;!>!&quot;)
    #
    # delimit variable with braces, if none exist:
    gsub(&quot;!>!\\[&quot;, &quot;[&quot;)
    if (gsub(&quot;\\]&quot;, &quot;&amp;}&quot;))
    gsub(&quot;\\]}}&quot;, &quot;]}&quot;)
    if (gsub(&quot;!<!\\$&quot;, &quot;&amp;{&quot;))
    gsub(&quot;!<!\\${{&quot;, &quot;!<!${&quot;)
    if (gsub(&quot;!>!&quot;, &quot;}&amp;&quot;))
    gsub(&quot;}}!>!&quot;, &quot;}!>!&quot;)
    #
    # add default &quot;[@]&quot; array indices, if none exist:
    gsub(&quot;\\[\\*\\]&quot;, &quot;[@]&quot;)#
  • -> [@]
    # turn positional parameters into argv array r-values (for robustness)
    if (casualvars!=&quot;ON&quot;)
    if (gsub(&quot;\\${[1-9]}!>!&quot;, &quot;${argv[!#!&amp;!#!&quot;))
    {gsub(&quot;!#!\\${&quot;, &quot;&quot;)
    gsub(&quot;}!>!!#!&quot;, &quot;]}&quot;)
    }
    if (casualvars!=&quot;ON&quot;)
    if (gsub(&quot;}!>!&quot;, &quot;[@]&amp;&quot;))
    gsub(&quot;\\[@\\]\\[@\\]&quot;, &quot;[@]&quot;)# ${var} -> ${var[@]}
    gsub(&quot;}\\[&quot;, &quot;[&quot;)# ${var}[ -> ${var[
    #
    # substitute special variables:
    gsub(&quot;\\$#0&quot;, &quot;${0:+1}&quot;)# special case: $?0 must be in ((...))
    gsub(&quot;\\${status\\[@\\]}&quot;, &quot;$?&quot;)
    gsub(&quot;\\${child\\[@\\]}&quot;, &quot;$!&quot;)
    gsub(&quot;\\${cwd\\[@\\]}&quot;, &quot;${PWD}&quot;)
    #
    # miscellaneous:
    gsub(&quot;![<>]!&quot;, &quot;&quot;)
    #XXXgsub(&quot;:g[ehrtq]&quot;, &quot;&quot;)# specifiers won't work on array elmts
    gsub(&quot;:q&quot;, &quot;&quot;)# implement instead using dquotes!
    gsub(&quot;}:e&quot;, &quot;##*.}&quot;)
    gsub(&quot;}:h&quot;, &quot;%/*}&quot;)
    gsub(&quot;}:r&quot;, &quot;%.*}&quot;)
    gsub(&quot;}:t&quot;, &quot;##*/}&quot;)
    }

    /(^|[\t &#59;&amp;|])set[\t ]/ {# set command syntax
    #XXX $0 alert! (must then be EOL)
    # &quot;set var = ( ... )&quot;
    # -xor-
    # &quot;set var = value&quot; -or- &quot;set var=value&quot; -or- &quot;set var&quot;
    # &quot;set echo/ignoreeof/noclobber/noglob/nonomatch/notify/verbose&quot;
    sub(&quot;(^|[\t ]+)set[\t ]+&quot;, &quot;&quot;)
    if (gsub(&quot;[\t ]+=&quot;, &quot;=&quot;))
    gsub(&quot;=[\t ]+&quot;, &quot;=&quot;)
    if ($0~&quot;\\)($|[\t ])&quot;)
    {# &quot;set var=(...)&quot; -> &quot;set -A var ... &#59;&quot;
    gsub(&quot;[^\t ]+=\\(&quot;, &quot; &#59; set -A &amp;&quot;)
    gsub(&quot;=\\(&quot;, &quot; &quot;)
    gsub(&quot;\\)&quot;, &quot; &#59; &quot;)
    gsub(&quot;set -A argv[^&#59;]*&quot;, &quot;&amp; \\&amp;\\&amp; set -- \&quot;${argv[@]}\&quot;&quot;)
    sub(&quot;^ &#59; &quot;, &quot;&quot;)
    }
    else
    {#XXX quoted text may get changed!!
    $0=&quot; &quot; $0 &quot; &quot;
    gsub(&quot;[\t ]+&quot;, &quot;  &quot;)
    gsub(&quot; [A-Za-z_0-9]+ &quot;, &quot; &amp;= &quot;)
    gsub(&quot; +=&quot;, &quot;=&quot;)
    sub(&quot;^  +&quot;, &quot;&quot;) #XXX
    print &quot;DEBUG: |&quot; $0 &quot;|&quot; >&quot;/dev/tty&quot;
    # &quot;set echo/ignoreeof/noclobber/noglob/nonomatch/notify/verbose&quot;
    sub(&quot;echo=&quot;, &quot;&#59;set -o xtrace &#59;&quot;)
    sub(&quot;ignoreeof=&quot;, &quot;&#59;set -o ignoreeof &#59;&quot;)
    sub(&quot;noglob=&quot;, &quot;&#59;set -o noglob &#59;&quot;)
    sub(&quot;nonomatch=&quot;, &quot;&#59;set -o verbose &#59;&quot;)
    sub(&quot;verbose=&quot;, &quot;&#59;set -o verbose &#59;&quot;)
    }
    }

    /(^|[\t &#59;&amp;|])source[\t ]/ {# source script
    #XXX either this or function _source defined above!
    if (nopreamble==&quot;ON&quot;)
    gsub(&quot;source[\t ]+&quot;, &quot;. &quot;)
    }

    /(^|[\t &#59;&amp;|])echo($|[\t ])/ {# echo -> print
    gsub(&quot;echo[\t ]*&quot;, &quot;print -- &quot;)
    sub(&quot;print -- -n[\t ]&quot;, &quot;print -n -- &quot;)
    }

    /(^|[\t &#59;&amp;|])foreach[\t ].*\(.*\)/ {# foreach -> for ... do
    # warning! (.*) must appear on the same line
    #XXX must appear before (.*) part
    #XXX $2 alert! (foreach must then be BOL)
    gsub(&quot;foreach&quot;, &quot;for &quot;)
    sub($2, $2 &quot; in &quot;)
    gsub(&quot;\\(&quot;, &quot;&quot;)
    gsub(&quot;\\)&quot;, &quot; &#59; do &quot;)
    }

    /(^|[\t &#59;&amp;|])while[\t ]+\(.*\)/ {# while -> while ... do
    # warning! (.*) must appear on the same line
    gsub(&quot;\\(&quot;, &quot;&quot;)
    gsub(&quot;\\)&quot;, &quot; &#59; do &quot;)
    }

    /(^|[\t &#59;&amp;|])goto[\t ]/ {# goto -> funct call (see docs)
    #XXX must appear before &quot;case&quot; part
    #XXX can goto's be right semi delimited ?
    # _Back_ reference(s) and/or _one_ skip-to-EOF
    # label (&quot;_bailout&quot;) are the only ones permitted.
    gsub(&quot;goto[\t ]+_?&quot;, &quot;_&quot;)
    label[$1]=$1
    }

    /(^|[\t &#59;&amp;|])case[\t ].*/ {# case -> case-pattern
    # warning! must appear rightmost
    #XXX must appear before label part
    if (prevcase!=NR-1)
    gsub(&quot;case[\t ]+&quot;, &quot;&quot;)
    else
    gsub(&quot;case[\t ]+&quot;, &quot;|&quot;)
    gsub(&quot;:&quot;, &quot;\\&quot;)
    # for multiple &quot;case&quot; statements having appeared on same line: #XXX
    gsub(&quot;\\\\[\t ]+\\|?&quot;, &quot; | &quot;)
    prevcase=NR
    }

    /(^|[\t &#59;&amp;|])switch[\t ]+\(.*\)/ {# switch -> case ... {
    # warning! must not appear with another (.*)
    #XXX (.*) must appear on the same line
    gsub(&quot;switch[\t ]+&quot;, &quot;case &quot;)
    gsub(&quot;\\(&quot;, &quot; &quot;)
    gsub(&quot;\\)&quot;, &quot; { &quot;)
    }

    /(^|[\t &#59;&amp;|])breaksw$/ {# breaksw -> &#59;&#59;
    # warning! you _must_ use the (optional!) &quot;breaksw&quot; command
    #XXX can this be right semi delimited?
    gsub(&quot;breaksw&quot;, &quot; &#59;&#59; &quot;)
    }

    /(^|[\t &#59;&amp;|])endsw$/ {# endsw -> esac
    #XXX can this be right semi delimited ?
    gsub(&quot;endsw&quot;, &quot;}&quot;)
    }

    /(^|[\t &#59;&amp;|])alias[\t ]/ {# alias syntax
    #XXX $0 alert! (must then be EOL)
    # unalias is meant to pass through
    # &quot;csh: alias var 1 2&quot; -> &quot;alias var (1 2)&quot;
    sub(&quot;alias[\t ]+[A-Za-z_0-9]+[\t ]&quot;, &quot;&amp;!<!&quot;) #XXX
    sub(&quot; !<!&quot;, &quot;='&quot;)
    $0=$0 &quot;'&quot;
    }

    /(^|[\t &#59;&amp;|])setenv($|[\t ])/ {# setenv -> export
    #XXX $2 alert! (must then be BOL)
    #XXX $0 alert! (must then be EOL)
    #XXX can this be right semi delimited ?
    gsub(&quot;setenv&quot;, &quot;export &quot;)
    if ($2!=&quot;&quot;)
    {sub($2, $2 &quot;=&quot;)
    sub(&quot;=[\t ]*&quot;, &quot;='&quot;)
    $0=$0 &quot;'&quot;
    }
    }

    /(^|[\t &#59;&amp;|])end$/ {# end -> done
    #XXX can this be right semi delimited ?
    gsub(&quot;end&quot;, &quot;done&quot;)
    }

    /(^|[\t &#59;&amp;|])endif$/ {# endif -> fi
    # csh: must be the first word on the line
    #XXX can this be right semi delimited ?
    gsub(&quot;endif&quot;, &quot;fi&quot;)
    }

    /(^|[\t &#59;&amp;|])(else[\t ]+)?if[\t ]*\(/ {# else if -> elif
    # csh: must be the first word on the line
    #XXX must appear before &quot;if&quot; part
    gsub(&quot;else[\t ]+if[\t ]*&quot;, &quot;elif &quot;)
    }

    /(^|[\t &#59;&amp;|])(el)?if[\t ]*\(.*\)/ {# (else)if/then syntax
    # warning! (.*) must appear on the same line
    #XXX except when their is no &quot;then&quot; part!
    #XXX $0 alert! (must then be EOL) ??
    if ($0~/\)[\t ]+then($|[\t ])/)
    {gsub(&quot;\\)[\t ]+then($|[\t ])&quot;, &quot;) &#59; then &quot;)
    }
    else
    {# if ... syntax -> ... &amp;&amp;
    gsub(&quot;if[\t ]+\\(&quot;, &quot;( &quot;)
    gsub(&quot;\\)[\t ]+&quot;, &quot;) \\&amp;\\&amp; &quot;)
    }
    }

    /\(.*\)/ {# expression syntax
    # warning! (.*) part must appear on the same line
    #XXX need to convert to ((...)) when applicable
    #gsub(&quot;\\&amp;\\&amp;&quot;, &quot; ]] \\&amp;\\&amp;\n[[ &quot;)
    #gsub(&quot;\\|\\|&quot;, &quot; ]] ||\n[[ &quot;)
    #
    #   (...)grouping
    #   ~one's complement
    #   !logicalnegation
    #   *  /  %multiplication,division, remainder (These are
    # right associative, which can lead to
    # unexpected results.  Group combinations
    # explicitly withparentheses.)
    #   +   -addition, subtraction (also right associative)
    #   <<   >>bitwiseshift left, bitwise shift right
    #   <   >  <=   >=less than, greater than, less than or equal
    # to, greater than or equal to
    #   ==   !=   =~ !~equal to, not equal to,filename-substitution
    # patternmatch (described below), filename-
    # substitution pattern mismatch
    #   &amp;bitwiseAND
    #   ^bitwiseXOR (exclusive or)
    #   |bitwiseinclusive OR
    #   &amp;&amp;logicalAND
    #   ||logicalOR
    #
    #XXX change into ((...)) or [[ ... ]], where appropriate.
    gsub(&quot;\\(&quot;, &quot; [[ &quot;)
    gsub(&quot;\\)&quot;, &quot; ]] &quot;)
    gsub(&quot;==&quot;, &quot;=&quot;)
    gsub(&quot;=~&quot;, &quot;=&quot;)
    gsub(&quot;!~&quot;, &quot;!=&quot;)
    gsub(&quot;<=&quot;, &quot;-le&quot;)
    gsub(&quot;>=&quot;, &quot;-ge&quot;)
    gsub(&quot;>&quot;, &quot;-lt&quot;)
    gsub(&quot;<&quot;, &quot;-gt&quot;)
    }

    /[\t =&quot;']`.*`/ {# command substitution syntax
    #XXX must be after expression part
    #XXX do csh bquotes need to both be on the same line?
    while (sub(&quot;`&quot;, &quot;\$\(&quot;))
    sub(&quot;`&quot;, &quot;\)&quot;)
    }

    /\$</ {# $< -> read ...
    #XXX must appear after expression syntax
    #XXX set var = ($<) ??
    gsub(&quot;\\$<&quot;, &quot;$(line)&quot;)
    #gsub(&quot;\\=\\$<&quot;, &quot;&quot;)
    #gsub($1, &quot;read &quot; $1)
    }

    /(^|[\t &#59;&amp;|])unalias[\t ]/ {# unalias syntax
    #XXX unalias with no arguments is meant to pass through
    #XXX will choke on escaped file substitution words!
    gsub(&quot;\\*&quot;, &quot;.*&quot;, $2)
    gsub(&quot;\\?&quot;, &quot;.&quot;, $2)
    gsub(&quot;unalias[\t ]+&quot;, &quot;unalias $(typeset +|grep '^&quot; $2 &quot;$')&quot;)
    }

    /(^|[\t &#59;&amp;|])unset[\t ]/ {# unset syntax
    #XXX must appear after `.*` and expression parts
    #XXX will choke on escaped file substitution words!
    gsub(&quot;\\*&quot;, &quot;.*&quot;, $2)
    gsub(&quot;\\?&quot;, &quot;.&quot;, $2)
    gsub(&quot;unset[\t ]&quot;, &quot;unset $(typeset +|grep '^&quot; $2 &quot;$')&quot;)
    }

    #XXX
    /XXX(^|[\t &#59;&amp;|)\@($|[\t ])/ {# @ -> let
    #XXX must appear after expresssion part
    #XXX does &quot;@&quot; have a significance to a &quot;nawk&quot; r.e. ??
    if ($2!=&quot;&quot;)
    {sub(&quot;@[\t ]+&quot;, &quot;(( &quot;)
    $0=$0 &quot; ))&quot;
    }
    else
    {sub(&quot;@&quot;, &quot;typeset -&quot;)
    }
    }

    /(^|[\t &#59;&amp;|])onintr($|[\t ])/ {# interrupt trapping
    #XXX $0 alert (must then be EOL!)
    if ($0~/onintr$/)
    {$0=&quot;trap -&quot;
    }
    else if ($0~/[\t ]-$/)
    {$0=&quot;trap ''&quot;
    }
    else
    {gsub(&quot;onintr[\t ]+&quot;, &quot;trap _&quot;)
    $0=$0 &quot; INT&quot;
    }
    }

    /^[\t ]*default/ {# default: -> *)
    #XXX must appear before &quot;label&quot; part
    gsub(&quot;default:&quot;, &quot;*)&quot;)
    }

    /(^|[\t &#59;&amp;|])[A-Za-z_][A-Za-z_0-9]*/ {# goto label -> function definition
    #XXX $0 alert! (must then be EOL)
    # The special label &quot;_bailout&quot; should be used
    # for the usual purpose of breaking to EOF.
    if ($0~/(^|[\t ]+)_?bailout/)
    next# already defined as function above
    sub(&quot;[A-Za-z_0-9]+:&quot;, &quot;_&amp;&quot;)
    sub(&quot;:&quot;, &quot; () { eval ${1:+trap \&quot;set -- $@&#59; set -A argv \\\&quot;\\$@\\\&quot;\&quot; EXIT}&quot;)
    }

    />&amp;?(\\?!)?/ {# redirection operators syntax
    #     >  >!   >&amp;   >&amp;!
    #     >>  >>&amp;  >>!  >>&amp;!
    gsub(&quot;>>&amp;&quot;, &quot; 2!#!\\&amp;1 !#!>&quot;)
    gsub(&quot;>&amp;&quot;, &quot; 2>\\&amp;1 >&quot;)
    gsub(&quot;!#!&quot;, &quot;>&quot;)
    gsub(&quot;>(\\\\)?!&quot;, &quot;>|&quot;)
    gsub(&quot;>>\\|&quot;, &quot;>>&quot;)# because &quot;>>|&quot; is ksh syntax error
    }

    {gsub(&quot;  +&quot;, &quot; &quot;)# strip redundant spaces
    #XXX this should be subsumed by escnl rejoined in prior pass
    if ($0~/[&amp;\|][\t ]+\\$/)
    # &quot;|&quot;, &quot;||&quot;, &quot;&amp;&amp;&quot; operators don't need escaped newline
    $0=substr($0, 1, length($0)-2)
    sub(&quot;\t &quot;, &quot;\t&quot;)
    sub(&quot;[\t ]+$&quot;, &quot;&quot;)# strip any trailing whitespace
    sub(&quot;[^&#59;]&#59;$&quot;, &quot;&quot;)# strip any trailing semicolon
    sub(&quot;[^&#59;]&#59;$&quot;, &quot;&quot;)# and do it again (#XXXtry &quot;@&quot; feature)
    if (!prevcase || prevcase==NR-1)
    # append right paren delimiting multi-line case patterns
    $0=&quot;)\n&quot; $0
    print
    #printf &quot;%2d,%2d: %s\n&quot;, NR, prevcase, $0
    }

    END {
    #XXX undefined hash print order!
    # for every &quot;onintr&quot; label turned into a function...
    for (key in label)
    if (key!=&quot;_bailout&quot;)
    print &quot;}&#59; &quot; label[key]
    }
  • 论坛徽章:
    0
    3 [报告]
    发表于 2002-09-16 11:41 |只看该作者

    把csh,sh或ksh语法的脚本相互转换的程序

    偶觉得不太会有这种自动转换程序吧,其实自己手工转换一下,正好熟悉几种shell的区别,乐事也

    突然发现danoyang的注册日期和我一样,一样的早,我找了一圈,只有樊老大比我早了,呵呵

    论坛徽章:
    0
    4 [报告]
    发表于 2002-09-16 11:45 |只看该作者

    把csh,sh或ksh语法的脚本相互转换的程序

    老大,你真厉害!我稍微多说几句话,你就把问题解决了,呵呵

    论坛徽章:
    0
    5 [报告]
    发表于 2002-09-16 11:46 |只看该作者

    把csh,sh或ksh语法的脚本相互转换的程序

    呵呵,没想到怎么多行
    的确是我写不出来的!
    谢了!用了再说。。。

    论坛徽章:
    0
    6 [报告]
    发表于 2002-09-16 11:53 |只看该作者

    把csh,sh或ksh语法的脚本相互转换的程序

    不好意思,我是很早就在这里注册了当时还没有UNIX机子用,不常上就忘这回事了,最近换工作有了SOLARIS用又注册时发现名字已经存在还以为遇到同名兄弟......
    但没想到“兄弟”的密码也和我一样。。。。。
    所以虽然注册那么早,还是菜鸟一只需要加倍努力学习!!

    论坛徽章:
    1
    巳蛇
日期:2014-12-29 11:22:41
    7 [报告]
    发表于 2002-09-16 13:34 |只看该作者

    把csh,sh或ksh语法的脚本相互转换的程序

    欢迎danoyang归队
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP