linux脚本中创建文件,shell脚本实现自动创建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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值