代码之家  ›  专栏  ›  技术社区  ›  helpermethod

为awk编写shell包装器脚本

  •  2
  • helpermethod  · 技术社区  · 16 年前

    这是我的剧本

    #!/bin/sh
    
    awk='
    
    BEGIN {FS = ",?+" }
    
    # removes all backspaces preceded by any char except _
    function format() {
        gsub("[^_]\b", "")
    }
    
    function getOptions() {
        getline
        format() 
    
        print
    }
    
    {
        format()
    
        if ($0 ~ /^SYNOPSIS$/ {
            getOptions()
            next            
        }
    
        if ($0  /^[ \t]+--?[A-Za-z0-9]+/) {
            print $0
        }
    }
    
    END { print "\n" }'
    
    path='/usr/share/man/man1'
    list=$(ls $path)
    
    for item in $list
    do
        echo "Command: $item"
        zcat $path$item | nroff -man | awk "$awk"
    done > opts
    

    顺便说一下,我在用纳克。

    提前谢谢

    3 回复  |  直到 16 年前
        1
  •  5
  •   Dale Hagglund    16 年前

    据我所知,有几件事不对:

    1. $awk . 你需要一个单一的报价后的行 END { ... }
    2. 使用 在任何地方也许你的意思是在 do 环
    3. awk 对于分号通常是相当宽容的,但这方面的任何问题都与在shell脚本中使用分号无关。
        2
  •  3
  •   Dennis Williamson    16 年前

    这三条线:

    path='/usr/share/man/man1'
    
    list=$(ls $path)
    
    for item in $list
    

    需要更改为:

    path='/usr/share/man/man1'
    
    for item in $path/*
    

    如果文件名中有空格,则 ls 不打算以这种方式使用。

        3
  •  1
  •   ghostdog74    16 年前

    我不确定你的意思,但如果我理解正确,你的showOpts.awk就是脚本开头的awk代码,所以你可以这样做

    path='/usr/share/man/man1'
    list=$(ls $path)
    
    for item in $list
    do
        echo "Command: $item"
        zcat $path$item | nroff -man | nawk ' BEGIN {FS = ",?+" }
    # removes all backspaces preceded by any char except _
    function format() {
        gsub("[^_]\b", "")
    }
    
    function getOptions() {
        getline
        format()
    
        print
    }
    
    {
        format()
    
        if ($0 ~ /^SYNOPSIS$/ {
            getOptions()
            next
        }
    
        if ($0  /^[ \t]+--?[A-Za-z0-9]+/) {
            print $0
        }
    }
    
    END { print "\n" } '
    done >> opts
    

    您可能应该使用>&燃气轮机;而不是>。