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
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:" readselect
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
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 ##################
################################################################