bashによるシェルスクリプト整形

シェルスクリプト整形を性懲りもなくやりつづける

方針
bashのfunctionをdeclare -fで表示すると整形してあることを利用
制限
sedぐらいで終わらせる

できた。最近の研究成果の集大成に近いなこれ(笑

だんだんbashというかunixとCが最大のコンピュータウイルスっていっている意味が分かってきた気がする。

方法

$ cat スクリプト名|(read -rd '' s;t=/tmp/$$;echo "function a(){">$t;echo "$s">>$t;echo -e " }\n declare -f a">>$t;chmod +x $t;a=`$t|sed '1,2d;$d;s/....//'`;rm $t;echo "$a" )

実験結果

$ cat a
e=/tmp/___$$;t=$e.c;p="echo -n";echo "#include <stdio.h>">$t;if [ $# = 2 ];then if [ $1 == -e ];then $p "$2">>$t;else $p "int main(){">>$t;a='printf("%';b="\\n\",$2)";case $1 in -m)$p "$2";;-d)$p "${a}d$b";;-f)$p "${a}f$b";;esac>>$t;$p ";}">>$t;fi;else read -rd '' s;$p "$s">>$t;fi;gcc $t -o $e;[ -e $e ]&&($e;rm $e);rm $t;
$ cat a|(read -rd '' s;t=/tmp/$$;echo "function a(){">$t;echo "$s">>$t;echo -e " }\n declare -f a">>$t;chmod +x $t;a=`$t|sed '1,2d;$d;s/....//'`;rm $t;echo "$a" )
e=/tmp/___$$;
t=$e.c;
p="echo -n";
echo "#include <stdio.h>" > $t;
if [ $# = 2 ]; then
    if [ $1 == -e ]; then
        $p "$2" >> $t;
    else
        $p "int main(){" >> $t;
        a='printf("%';
        b="\\n\",$2)";
        case $1 in 
            -m)
                $p "$2"
            ;;
            -d)
                $p "${a}d$b"
            ;;
            -f)
                $p "${a}f$b"
            ;;
        esac >> $t;
        $p ";}" >> $t;
    fi;
else
    read -rd '' s;
    $p "$s" >> $t;
fi;
gcc $t -o $e;
[ -e $e ] && ( $e;
rm $e );
rm $t