Chinaunix首页 | 论坛 | 博客
  • 博客访问: 311153
  • 博文数量: 48
  • 博客积分: 4510
  • 博客等级: 中校
  • 技术积分: 556
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:19
文章分类

全部博文(48)

文章存档

2012年(1)

2011年(9)

2010年(1)

2009年(12)

2008年(25)

分类:

2008-11-26 22:11:40

平时经常要和各种脚本打交道,而且要不时的写些小脚本试验语言的语法或语义,每次都 vim foo 然后再 chmod a+x foo,甚或 touch foo; chmod a+x foo 然后再 vim foo 输入一堆固定不变的东西显得很浪费时间,故有下面这个小脚本 


#!/bin/bash

script_type=${0##*/}
file_name=init_script.sh
if [ -e "$1" ]; then
    echo "$1 exist!"
    exit 1;
elif [ "$script_type" = "init_script.sh" ]; then
    echo "usage: init_scripttype scriptname"
    exit 1;
elif [ $# -ne 1 ]; then
    echo "usage: $script_type filename"
    exit 1;
else
    case "$script_type" in
        init_perl )
        echo -e "#!/usr/bin/perl\nuse strict;\nuse warnings;" > $1
        chmod a+x $1
        vim + $1 ;;
        init_python )
        echo "#!/usr/bin/python" > $1
        chmod a+x $1
        vim $1 ;;
        init_bash )
        echo "#!/bin/bash" > $1
        chmod a+x $1
        vim $1 ;;
        init_tcl )
        echo "#!/usr/bin/tclsh" > $1
        chmod a+x $1
        vim $1 ;;
        init_tk )
        echo "#!/usr/bin/wish -f" > $1
        chmod a+x $1
        vim $1 ;;
        init_cpp )
        echo -e "#include \nusing namespace std;" > $1
        vim + $1 ;;
        init_c )
        echo "#include " > $1
        vim $1 ;;
        * )
        echo "$script_type"
        echo "init what kind of script? " 1>&2
        exit 1;
    esac
fi


可将此脚本命名为 init_script.sh,放在 ~/bin 中,然后在 ~/bin 中建立一些到 init_script.sh 的软链接,如:

ln -s init_script.sh init_perl

ln -s init_script.sh init_bash

ln -s init_script.sh init_c


等等,然后就可以使用 init_perl foo 等来建立新的脚本文件。新建立的文件不但权限已经设置好,而且还会在头部加入常用的代码。

当然,这个脚本可以扩展的更为通用。例如根据命令行选项或者调用脚本时的名字从一个默认或指定的目录中选取适当的模板文件,然后根据命令行选项或其他信息对模板进一步修改,等等。
阅读(1438) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

machine2009-01-03 21:40:29

不错,这个我刚好需要。