「Hagi utils」の版間の差分

提供: Wikinote
移動: 案内検索
(fill コマンド)
(plog コマンド)
行123: 行123:
 
     -n          時刻等を付加せず、コマンドの実行結果のみを出力する。
 
     -n          時刻等を付加せず、コマンドの実行結果のみを出力する。
  
 +
ソースコード <toggledisplay>
 
  #!/bin/bash
 
  #!/bin/bash
 
  ### parameters ###
 
  ### parameters ###
行175: 行176:
 
     sleep $INTERVAL
 
     sleep $INTERVAL
 
  done
 
  done
 +
</toggledisplay>

2009年7月26日 (日) 13:26時点における版

【秘密】(笑)

ちょっとしたコマンドやライブラリを自宅と会社で同期するためのページ。 学生時代に作ってたヤツは今じゃ全然使い物にならない…。

C 言語

printb() 関数

整数を 2 進数で表示する関数。

#include <stdio.h>

void printb(unsigned num) {
    int i;
    for (i = 31; i >= 0; i--) {
        putchar(((num >> i) & 1) + '0');
    }
}

void printb64(unsigned long long num) {
    int i;
    for (i = 63; i >= 0; i--) {
        putchar(((num >> i) & 1) + '0');
    }
}

使用例:argv[1]atoi() に渡して printb() にかけるコマンド

[hagio@lab ~]$ printb 1234567890
01001001100101100000001011010010
[hagio@lab ~]$ printb -1
11111111111111111111111111111111

fill コマンド

標準入力の各行を、指定された行長までスペースで埋めるコマンド。

#include <stdio.h>

int main(int argc, char *argv[]) {
    int max, len;
    int c;

    if (argc != 2) {
        printf("Usage: fill MAXLEN < FILE\n");
        return 1;
    }

    max = atoi(argv[1]);
    len = 0;
    while ((c = getchar()) != EOF) {
        if (c == '\n') {
            for ( ; len < max; len++) {
                putchar(' ');
            }
            putchar('\n');
            len = 0;
        }
        else {
            putchar(c);
            len++;
        }
    }
}

printf() で簡単にできた…。バッファリングバージョン。

#include <stdio.h>
#include <string.h>
#define MAXLEN 1024

int main(int argc, char *argv[]) {
    int max, len;
    char line[MAXLEN];

    if (argc != 2) {
        printf("Usage: fill MAXLEN < FILE\n");
        return 1;
    }
    max = atoi(argv[1]);
    while (fgets(line, MAXLEN, stdin)) {
        len = strlen(line);
        line[len-1] = '\0';
        printf("%-*s\n", max, line);
    }
}

gawk スクリプト

maxlen コマンド

テキストファイル中の最も長い行の長さを求めるスクリプト。

#!/bin/gawk -f
## Usage: maxlen FILE
## Print maximum line length of FILE

BEGIN {
    if (ARGC != 2) {
        print "Usage: maxlen FILE"
        exit 1
    }
}
{
    len = length()
    if (max < len) max = len
}
END {
    if (ARGC == 2) {
        print max
    }
}

まあこれくらいインスタントに書いた方が早いですが。

$ gawk '{if (max < length()) max = length()}END{print max}' FILE

とか。

bash スクリプト

plog コマンド

プログラムを定期的に実行し、その出力にタイムスタンプを付与するシェルスクリプト。

Usage:
    plog [-d] [-i INTERVAL] [-n] command
    plog -h
Options:
    -d           時刻に加えて日付を付加する。
    -h           使い方を表示する。
    -i INTERVAL  コマンドを実行する間隔(秒)。デフォルトでは 1 秒。
    -n           時刻等を付加せず、コマンドの実行結果のみを出力する。

ソースコード <toggledisplay>

#!/bin/bash
### parameters ###
INTERVAL=1
PRINT_TIME=1
PRINT_DATE=0
### constants ###
CMD_NAME=$(basename $0)
DEFAULT_SCRIPT='{ print strftime("%T"), $0 }'
ADDDATE_SCRIPT='{ print strftime("%F %T"), $0 }'
### flags & variables ###
GETOPT_ERR=0

### functions ###
function usage() {
   echo "Usage:"
   echo "    $CMD_NAME [-d] [-i INTERVAL] [-n] command"
   echo "    $CMD_NAME -h"
   echo "Options:"
   echo "    -d          Print date besides time."
   echo "    -h          Print this help."
   echo "    -i INTERVAL Specify the interval between executions. (seconds) Default is 1 s."
   echo "    -n          Do not print time or date. Print command output only."
}

### main ###
while getopts "i:ndh" opt; do
   case $opt in
       i) INTERVAL=$OPTARG;;
       n) PRINT_TIME=0;;
       d) PRINT_DATE=1;;
       h) usage; exit 0;;
       ?) GETOPT_ERR=1;;
   esac
done
shift $(( $OPTIND - 1 ))

if [ $GETOPT_ERR -eq 1 -o $# -eq 0 ]; then
   usage
   exit 0
fi

CMD=$@
while : ; do
   if [ $PRINT_TIME -eq 0 ]; then
       $CMD
   elif [ $PRINT_DATE -eq 1 ]; then
       $CMD | gawk "$ADDDATE_SCRIPT"
   else
       $CMD | gawk "$DEFAULT_SCRIPT"
   fi
   sleep $INTERVAL
done

</toggledisplay>