#!/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