2012/12/26

dog (1)

以前、某雑誌のシェルスクリプト大喜利に投稿したネタ。
こんな感じのお題に対する回答。

cat コマンドの作者が遺言を遺しました。
「私の人生における最大の公開は、 dog コマンドを作り忘れてしまったことだ。 あぁ誰か作ってくれぇー。ガクッ」
さぁ、一体どんなコマンド?

お題がネタっぽいので回答もネタっぽくしてみた。
more (1) を拡張して less (1) が、 yacc (1) を拡張して bison (1) が作られた様に、 dog (1) は cat (1) が拡張されたものという発想で httphttps そして ftp といった schema に対応した cat (1) というスクリプトにしてみた。
内容はとても単純なので cat → dog という発想だけが勝負のスクリプトである。

雑誌では割愛されてしまったネタとしてのマニュアルも掲載してみる。
というか、実はマニュアルの方が書きたかったという…(笑

  1#!/bin/sh
  2#
  3# NAME
  4#   dog -- concatenate and print files
  5#
  6# SYNOPSIS
  7#   dog [cat options] [file ...]
  8#
  9# DESCRIPTION
 10#   Dog is a file concatenate and print command in the style of cat(1).
 11#   It should be upwardly compatible with original cat.
 12#
 13#   The dog utility reads files sequentially, writing them to the standard
 14#   output.  The file operands are processed in command-line order.  If file
 15#   has protocol schema such as 'http://', 'https://' or 'ftp://', dog utility
 16#   gets the file from remote system and display it, otherwise dog assumes
 17#   local file. The dog utility uses wget(1), curl(1), or lynx(1) to retrieve
 18#   the file from remote system. If dog utility could not find these commands
 19#   from standard command path, disable remote file display.
 20#   All option of dog is same as cat(1) except '-h'.
 21#
 22# EXIT STATUS
 23#   The dog utility exits 0 on success, and >0 if an error occurs.
 24#
 25# SEE ALSO
 26#   Rob Pike, "UNIX Style, or cat -v Considered Harmful", USENIX Summer
 27#   Conference Proceedings, 1983.
 28#
 29# STANDARDS
 30#   The dog utility is *NOT* complient with the IEEE Std 
 31#   1003.2-1992 (``POSIX.2'') specification, even now and forever.
 32#   
 33
 34curl="-s -o -"
 35wget="-q -O -"
 36lynx="-source"
 37
 38# get command name and path
 39for i in wget curl lynx
 40do
 41    for j in /bin /usr/bin /usr/local/bin /opt/bin /opt/local/bin
 42    do
 43        if [ -x ${j}/${i} ]
 44        then
 45            command="${j}/${i} `eval echo '$'${i}`"
 46            break 2
 47        fi
 48    done
 49done
 50
 51# args
 52while [ ${1} != "" ]
 53do
 54    case ${1} in
 55        -h )
 56            sed -n '2,/^$/s/^#//p' ${0}
 57            exit 1
 58            ;;
 59        -- )
 60            shift
 61            break
 62            ;;
 63        -* )
 64            opt="${opt} $1"
 65            ;;
 66        *  )
 67            break
 68            ;;
 69    esac
 70    shift
 71done
 72
 73for i in $*
 74do
 75    if expr ${i} : "https*://" > /dev/null
 76    then
 77        if [ -n "${command}" ]
 78        then
 79            ${command} ${i} | cat ${opt}
 80        else
 81            echo "${0##*/}: cannot get remote file: ${i}" 1>&2
 82        fi
 83    elif expr ${i} : "ftp*://" > /dev/null
 84    then
 85        case ${command} in
 86            *wget*|*curl* )
 87                ${command} ${i} | cat ${opt}
 88                ;;
 89            * )
 90                echo "${0##*/}: cannot get remote file: ${i}" 1>&2
 91                ;;
 92        esac
 93    else
 94        cat ${opt} ${i}
 95    fi
 96done
    

今回のシェルスクリプト大喜利にも投稿したので、 某雑誌が発売されたら紹介する予定。

トラックバック
https://blog.bsdhack.org/index.cgi/Computer/20121226.trackback
コメント














メッセージ: Ready to post a comment.


Copyright © 2008-2020 Mitzyuki IMAIZUMI. All rights reserved.