I have a bash script that reads a config file with key/value pairs and uses these values to modify the functioning of other scripts correspondingly.
Example .cfg file:
layout='true' code_compliance='true' .... .... variablename='false' output='true'
The file consists of a very less amount of lines currently but there might be a chance of increase in the size of the file in the future.
I have written code that reads the file line by line and gets the key value pairs. Is it better (in terms of performance such as speed, memory requirements and coding standards) to code this way or to search for the key strings and find its corresponding value using grep?
#!/bin/bash
###########################################################################################################################
#This script is responsible for reading inputs from the configuration file and running corresponding scripts.
#If the value for an option is set to true, then the suitable script is run.
#If the user provides an invalid value for an option, an error message is displayed.
#Likewise, if an invalid option is provided, an error message is displayed.
###########################################################################################################################
SCRIPT=`readlink -f $0`
SCRIPTPATH=`dirname $SCRIPT`
TCPATH=$(echo $SCRIPTPATH | sed 's/\/bld.*//')
dir=$TCPATH/bld/code_compliance
# Define the colours used for interface output.
source $TCPATH/bld/common/def_colours
echo "VF Code Compliance Tests: $(date)" > $3
echo "" >> $3
while read line2
do
name2=$line2
name2=${name2^^}
if [[ $name2 =~ "=" && $name2 =~ "OUTPUT" ]]; then
option2=${name2%=*}
value2=${name2#*=}
start2=${value2#*\'}
end2=${start2%\'*}
output=$end2
fi
done < $2
while read line
do
name=$line
name=${name^^}
if [[ $name =~ "=" ]]; then
option=${name%=*}
value=${name#*=}
start=${value#*\'}
end=${start%\'*}
if [[ $output == "TEXTFILE" ]]; then
if [[ "$end" == "TRUE" ]]; then
case $option in
VARIABLENAME*)
bash $dir/varname $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
CLASSSTRUCTNAME*)
bash $dir/classnamecheck $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
CONSTNAME*)
bash $dir/constcheck $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
FUNCNAME*)
bash $dir/funcnamecheck $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
FILECOMPLETE*)
bash $dir/filecomplete $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
HEADERGUARD*)
bash $dir/headerguard $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
FILENAME*)
bash $dir/filenamesplchar $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
HEADERFILECHECK*)
bash $dir/headercheck $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
LAYOUT*)
bash $dir/layout $1 >> $3 2> /dev/null
sed -r -i "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $3
;;
CODE_COMPLIANCE*)
;;
OUTPUT*)
;;
*)
echo -e "${RED}Invalid option ${CYAN}$option ${RED}for value ${CYAN}$end${NO_COLOUR}" ;;
esac
elif [[ "$end" == "FALSE" ]]; then
:
elif [[ "$end" == "TEXTFILE" || "$end" == "CONSOLE" ]]; then
:
else
echo -e "${RED}Invalid value ${CYAN}$end ${RED}for option ${CYAN}$option${NO_COLOUR}"
fi
elif [[ $output == "CONSOLE" ]]; then
if [[ "$end" == "TRUE" ]]; then
case $option in
VARIABLENAME*) bash $dir/varname $1 ;;
CLASSSTRUCTNAME*) bash $dir/classnamecheck $1 ;;
CONSTNAME*) bash $dir/constcheck $1 ;;
FUNCNAME*) bash $dir/funcnamecheck $1 ;;
FILECOMPLETE*) bash $dir/filecomplete $1 ;;
HEADERGUARD*) bash $dir/headerguard $1 ;;
FILENAME*) bash $dir/filenamesplchar $1 ;;
HEADERFILECHECK*) bash $dir/headercheck $1 ;;
LAYOUT*) bash $dir/layout $1 ;;
CODE_COMPLIANCE*) ;;
OUTPUT*) ;;
*)
echo -e "${RED}Invalid option ${CYAN}$option ${RED}for value ${CYAN}$end${NO_COLOUR}" ;;
esac
elif [[ "$end" == "FALSE" ]]; then
:
else
echo -e "${RED}Invalid value ${CYAN}$end ${RED}for option ${CYAN}$option${NO_COLOUR}"
fi
elif [[ $output == "BOTH" ]]; then
if [[ "$end" == "TRUE" ]]; then
case $option in
VARIABLENAME*) bash $dir/varname $1 | tee -a $3 ;;
CLASSSTRUCTNAME*) bash $dir/classnamecheck $1 | tee -a $3 ;;
CONSTNAME*) bash $dir/constcheck $1 | tee -a $3 ;;
FUNCNAME*) bash $dir/funcnamecheck $1 | tee -a $3 ;;
FILECOMPLETE*) bash $dir/filecomplete $1 | tee -a $3 ;;
HEADERGUARD*) bash $dir/headerguard $1 | tee -a $3 ;;
FILENAME*) bash $dir/filenamesplchar $1 | tee -a $3 ;;
HEADERFILECHECK*) bash $dir/headercheck $1 | tee -a $3 ;;
LAYOUT*) bash $dir/layout $1 | tee -a $3 ;;
CODE_COMPLIANCE*) ;;
OUTPUT*) ;;
*)
echo -e "${RED}Invalid option ${CYAN}$option ${RED}for value ${CYAN}$end${NO_COLOUR}" ;;
esac
elif [[ "$end" == "FALSE" ]]; then
:
else
echo -e "${RED}Invalid value ${CYAN}$end ${RED}for option ${CYAN}$option${NO_COLOUR}"
fi
fi
fi
done < $2
if [[ $output == "TEXTFILE" ]]; then
echo -e "\n${BOLD_GREEN}Code Compliance has been successfully completed and the output is stored as a text file in $3 ${NO_COLOUR}\n"
fi
if [[ $output == "CONSOLE" ]]; then
echo -e "\n${BOLD_GREEN}Code Compliance has been successfully completed and the output is printed in the console${NO_COLOUR}\n"
fi
exit 0
I have to read the option output
from the .cfg file whether to print the output to console or to a file or to both. Are there any better ways to do it rather than having multiple case
statements and if
conditions?