2016年9月26日月曜日

ランダムな内容のファイルを生成するシェルスクリプト

標準出力からファイルのリストを取り出し、ランダムなテキストをランダムなサイズで出
力する

#!/bin/sh

output()
{
    linesize=`cat /dev/urandom | tr -dc '0-9' | fold -w 3 | head -n 1`
    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n $linesize > $1
}

while read fullpath; do
    [ -d $fullpath ] && continue

    if [ -f $fullpath ]; then
        output $fullpath
        echo "Modified $fullpath"
        continue
    fi

    mkdir -p `dirname $fullpath`

    filename=`basename $fullpath`
    case $filename in
        .*)    # ドットで始まる名前はディレクトリにする
            mkdir -p $fullpath
            ;;
        *)
            output $fullpath
            ;;
    esac
    echo "Created $fullpath"
done

exit 0