该脚本文件主要根据程序的扩展名来确定该程序属于C程序还是C++程序,然后根据不同情况,使用gcc或者g++编译器编译该程序。
#!/bin/bash
# =====================================
# Author : Anna
# exit_code :
# 2 -> Invalid Usage;
# 3 -> not a source code file
# =====================================
fileName=$1
option="$* -g -lpthread"
outFileName=`echo $fileName | cut -d. -f1`
AnalyseFile(){
if ! file $fileName | grep "ASCII" &> /dev/null;then
echo -e "\033[31m<$fileName> is not a ASCII file.\033[0m"
exit 3
fi
}
function ComplieFile {
if echo $fileName | egrep ".*.cc|cpp\>" &> /dev/null;then
CC=g++
echo -e "\033[34m======================== C++_program Compling ===================== \033[0m"
elif echo $fileName | grep ".*.c\>" &> /dev/null;then
CC=gcc
echo -e "\033[35m======================== C_program Compling = ==================== \033[0m"
fi
$CC $option -o $outFileName
}
function PrintResult {
if [ $? -eq 0 ];then
echo -e "\033[33m Congratulation! Complie successfully ^-^! \033[0m"
echo -e "\033[32m `date`\033[0m"
else
echo -e "\033[31m ERROR $CC -o $outfilename $option\033[0m"
fi
}
main(){
if [ $# -le 0 ];then
echo -e "\033[31mUsage : $0 <program-file>\033[0m"
exit 2;
fi
AnalyseFile $*
ComplieFile $*
PrintResult
}
main $*