I have created this bash script for Compile And Run c++ code, car++.sh. As I'm new to bash scripting, I'm not sure about good bash programming practices.
Please review it to see if you could find any anomaly.
#!/bin/bash
found=false;
output=;
o_is_set=false;
#retrieve output_filename from arguments as provided by user
for arg in "$@"
do
if [ "$o_is_set" = true ]
then
output="$arg";
found=true;
break;
fi
if [[ $arg == -o ]]
then
o_is_set=true;
fi
done
#else retrieve from file name of first .cpp or .c++ file specified
if [ "$found" = false ]
then
for file in "$@"
do
ext=$(echo $file | tr '[:upper:]' '[:lower:]');
if [[ $ext == *.cpp ]]
then
output=$(basename "$file" ".cpp");
found=true;
break;
fi
if [[ $ext == *.c++ ]]
then
output=$(basename "$file" ".c++");
found=true;
break;
fi
done
fi
#else retrieve from first .c file specified
if [ "$found" = false ]
then
for file in "$@"
do
ext=$(echo $file | tr '[:upper:]' '[:lower:]');
if [[ $ext == *.c ]]
then
output=$(basename "$file" ".c");
found=true;
break;
fi
done
fi
#output file name is not specified by user and there is no C++ or C file,
#lets see what g++ says about it
if [ "$found" = false ]
then
echo;
g++ -Wall -std=c++11 "$@";
exit "$?";
fi
run="./$output";
g++ -Wall -o "$output" -std=c++11 "$@" && $run;