C 语言代码格式化

#!/bin/bash

function usage
{
	echo "Usage: ./codeformat.sh <file>";
}

# 判断传入脚本的参数内容
# 是文件返回1, 不是文件返回0
function isFile
{
	# 参数大于1个返回0
	if [ $# -ne 1 ]
		then
			return 0
	fi	

	if [ -d $1 ]
		then 
			return 0
		else
			return 1
	fi
}

# 判断传入脚本的参数内容
# 是.c 或者.h文件返回1, 不是返回0
function isCFile
{
	declare SUFFIX ARG1
	
	ARG1=$1
	
	if [ $# -ne 1 ]
		then
			return 0
	fi
		
	SUFFIX=${ARG1##*.} # 截取后缀
	
	if [ "$SUFFIX" = "c" ] || [ "$SUFFIX" = "h" ]
		then
			return 1
		else
			return 0;
	fi
}


declare FILE

FILE=$1

if [ ! -n "$FILE" ] # 命令行参数为空,脚本退出
	then 
		usage
		exit
fi


isFile $FILE 
if [  $? -eq 0 ] # 命令行传入的参数不是文件,脚本退出
	then
		echo "$FILE is not file!"
		exit
fi		

isCFile $FILE 
if [ $? -eq 0 ] # 命令行参数为空,不是.c或者.h文件,脚本退出
	then 
		echo "$FILE is not .c or .h file!"
		exit
fi

# 格式化.c或者.h文件的代码
indent -npro -gnu -i4 -ts4 -sob -l200 -ss -bl -bli 0 -npsl $FILE


转载于:https://my.oschina.net/iamancoder/blog/652004

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值