該腳本文件主要根據程序的擴展名來確定該程序屬於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 $*