Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1310526
  • 博文数量: 213
  • 博客积分: 7590
  • 博客等级: 少将
  • 技术积分: 2185
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-31 17:31
个人简介

热爱开源,热爱linux

文章分类

全部博文(213)

文章存档

2018年(4)

2017年(1)

2015年(1)

2014年(5)

2013年(2)

2012年(2)

2011年(21)

2010年(82)

2009年(72)

2008年(23)

分类: LINUX

2010-09-08 16:02:08


大家也许遇到过这样一个问题,如果每次写一个C语言文件的时候都要重头开始写一个C文件包含的头文件,main函数等,如果每次创建都要这样写,感觉很麻烦,如果能创建一个C文件的时候,这些头文件什么的都能写在文件头部,那就能为你省下好多时间来思考你的算法问题,而不是像一个打字员一样,重新输入新的头文件等,基于此,我就写了一个自动创建C语言的脚本,只需要将脚本c_create.sh放到/usr/local/bin/目录下,就可以在你系统的任何地方创建脚本为你创建的C文件模板了
脚本用法1:c_create.sh 将创建main函数包含void参数的C模板
脚本用法2:c_create.sh -n 将创建main函数包含int argc,char *argv[] 参数的C模板
下面是脚本具体程序(有可能有些例外情况没有考虑到,有谁发现了帮忙指正下,嘿嘿...)

#!/bin/bash

#

# Script: c_create.sh

# Author: CaoJiangfeng

# Date: 09/06/2010

# Purpose: This script is used to create a c file with

# some comment in and some other information

# Version: 1.0

#

#####################################################################

################## Define some variables here ######################

#####################################################################

C_FILE=$1 # $1 is the file we want to create

SCRIPT=`basename $0`

WHICH_TO_CREATE=1 # This variable decides which c file to create

PARAMETER_ERROR=65 # If parameters number is not right

FILE_IS_DIR=66 # If a file name as same as a directory ,then exit this code $FILE_IS_DIR 66

NEW_FILE_CREATED=67 # If a new file created ,then exit $NEW_FILE_CREATED

NEW_SAME_NAME_FILE_CREATED=68 # If a new file overwrite the exist same name file ,then exit $NEW_SAME_NAM_EFIL_CREATED

EXIT__NOT_CREATE_FILE=69 # When we create a same file has exist ,if we do not create the exit $EXIT_NOT_CREATE_FILE


#####################################################################

################# Define some functions here #####################

#####################################################################

#

#####################################################################

#

function usage
{
    # This function shows how to use the script and some parameters

    echo "Usage:"
    echo "    $SCRIPT < file_to_create > or"
    echo "    bash $SCRIPT < file_to_create >"
    echo "Help:
    default - Create a C file with void parameter in main
    -n - Create a C file with argc ,*argv[] parameters in main
    "
    
    exit $PARAMETER_ERROR
}
#

#####################################################################

#

function create_file
{    
    # This functiion create a c file according to the mass parameters

    # from function file_operations

    
    # Determine the number of parameters

    if [ $# -eq 1 ];

    then
        C_FILE=$1
    fi    
    
echo "/*******************************************************************
* Filename: $C_FILE
* Author: CaoJiangfeng
* Date: `date`
* Purpose: This file is used to ...
* Version: 1.0
*
********************************************************************/
#include
#include
"
>$C_FILE
if [ $WHICH_TO_CREATE -eq 1 ] ;
then
    echo "int main(void)
{
    // add your code here
    
    return 0;
}"
>>$C_FILE
else
    echo "int main(int argc, char *argv[])
{    
    // add your code here
        
    return 0;
}"
>>$C_FILE
fi

}
function file_operations
{
    
    case $1 in
    1)
        echo -n "Please input the new file name you want to create:"
        read newfilename
        create_file $newfilename
        echo "New file $C_FILE has been created!"
        exit $NEW_FILE_CREATED
    ;;    
    2)
        create_file
        echo "Overwrite $C_FILE has been created!"
        exit $NEW_SAME_NAME_FILE_CREATED
    ;;
    *)
        echo "Exiting create C file ..."
        exit $EXIT_NOT_CREATE_FILE
    ;;
    esac
    
}
######################################################################

################### Start of main ######################

######################################################################



# Operate on specific parameters

while getopts "n" OPTION
do
    case $OPTION in
    n)    
        WHICH_TO_CREATE=2 # Extend C file to create

        echo "$WHICH_TO_CREATE"
        C_FILE=$2
    ;;
    \?)    
        :
    ;;
esac
done
# Determine if the number of parameters is correct ,if not right show help

if [ $# -eq 0 -o $# -gt 2 ] ;

then
    usage
fi
# Determin if the parameter is right(-n)

para="-n"
if [ $# -eq 2 -a "$1" != "$para" ];

then
    echo "error parameter"
    usage
fi

# To judge if the file $1 which is to create already exists in current dir

for file in *
do
    if [ "$file" = "$C_FILE" ] ;
    then
        if [ -f $file ]    ;
        then
            echo "$C_FILE has same name with exist file $file"
            echo "What would you want to do?"
            echo "input:
                1 - means you want to create other name file
                2 - means you want to overrite the $file
                3 - means you want to quit
            "

            echo -n "Please select which yoou want to do:"
            read select
         file_operations $select
        elif [ -d $file ] ;
        then
            echo "$file is a directory,you cannot create a same file name $C_FILE"
            exit $FILE_IS_DIR
        fi

    fi
done

# If there is no same file in the current directory,the run follows

create_file $C_FILE

exit 0


2.下面添加一个创建脚本文件的shell脚本

#!/bin/bash

#

# Script: bash_create.sh

# Author: CaoJiangfeng

# Date: 09/06/2010

# Purpose: This script is used to create a bash file with

# some comment in and some other information

# Version: 1.0

#


BASH_FILE=$1
SCRIPT=`basename $0`
VARIABLERROR=65


function usage
{
    echo "Usage:"
    echo "$SCRIPT < file_to_create > or"
    echo "bash $SCRIPT < file_to_create >"
    exit $VARIABLEERROR
}

if [ $# -ne 1 ];

then
    usage
    exit $VARIABLEERROR
fi

echo "#!/bin/bash
#
# Script: $BASH_FILE
# Author: CaoJiangfeng
# Date: `date "
+%F %r"`
# Purpose: This script is used to ...
# Version: 1.0
#
################################################################
################ Define some variables here ##################
################################################################
# Define some global variables

################################################################
################ Define some functions here ##################
################################################################

################################################################
################ Start of main ##################
################################################################

exit 0"
> $BASH_FILE

# Modify the file execute permissions

chmod 755 $BASH_FILE

exit 0


阅读(3623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~