I just wrote a quick bash script to validate the header files from the C/C++ source file. This code just extracts the header file from the source code and runs a quick search in "include" folder, and prints the result. Nothing new, just wanted to write a bash script for learning and fun.
There are many improvements, still to be done. Ideas, suggestions and constructive criticism is welcome.
Here's the code:
- #!/bin/bash
- #
- # Description : A quick 'n' dirty bash script for validating header files.
- # Author : RahulB
- # Usage : ./validate.sh SourceFile
- # Platform : GNU/Linux
- # Greets to : Anant Sir, Hackuin, b0nd bro, and all g4h members.
- #
- if [ ! $# = 1 ] ; then #Checking for exact one command line argument
- printf "Usage : $0 SourceFile \n"
- exit 1
- fi
- if [ ! -s $1 ] ; then #Checking file's existence/ sufficient privilege
- printf "Error : Either file doesn't exist or you don't have sufficient privileges. \n"
- exit 1
- fi
- declare FILENAME="$1"
- declare EXT="${FILENAME##.*}" #Extracting file extension
- declare WD=$(pwd) #Saving current working directory
- declare VDIR=$(ls -F /usr/include/c++/ | grep '/') #Extracting version directory
- if [ "$EXT" = "cpp" ] ; then #Determing whether it's C or C++ by file extension
- DIR="/usr/include/c++/$VDIR" #C++ include directory
- elif [ "$EXT" = "c" ] ; then
- DIR='/usr/include/' #C include directory
- else
- printf "Error : Use .cpp for C++ and .c for C source files.\n"
- exit 1;
- fi
- cat $1 | grep '#include' | cut -d '<' -f2 | cut -d '>' -f1 > demo.log #Extracting header files' name
- cd "$DIR" #Changing directory to include folder
- for LINE in $(cat "$WD/demo.log") #Line-by-line processing
- do
- if test -s "$LINE" ; then #Checking if header file exists or not.
- printf "$LINE exists. \n"
- else
- printf "Check the header file $LINE. \n"
- fi
- done
- printf "\n\n"
- exit 0
Thanks and Regards.
Cheers.
No comments:
Post a Comment