#!/bin/bash
function usage
{
echo "Usage: ./codeformat.sh ";
}
# 判断传入脚本的参数内容
# 是文件返回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