Last active
March 30, 2023 04:49
-
-
Save SidhBhat/bcd7f75e788ec3eea7c15088b98120cc to your computer and use it in GitHub Desktop.
Compile and run interactivly a c/c++ file. Useful for beginners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# You can use this script to compile and run a single file c or cpp program interactively | |
# the code should be open in a editor, while call this script to execute the code | |
## I had used this script as an alternative to Turbo++ (which my school had asked me to use) | |
options=$(getopt -o cphl:d: --long help,language:,directory: -- "$@") | |
[ $? -eq 0 ] || { | |
echo -e "\e[1;31mUnexpected Error\e[0m terminating...." | |
exit 3 | |
} | |
eval set -- $options | |
# echo "$@" | |
dir=/tmp/kdev-run | |
language=c | |
while [ -n "$1" ]; do | |
case "$1" in | |
-c) | |
language=c | |
shift | |
;; | |
-p) | |
language=c++ | |
shift | |
;; | |
-l | --language) | |
shift | |
if [ "$1" == "C" ]; then | |
language=c | |
elif [ "$1" == "C++" ]; then | |
language=c++ | |
else | |
echo "Unrecongnised option : $1" | |
exit 1 | |
fi | |
shift | |
;; | |
-d | --directory) | |
shift | |
[ -d "$1" ] || { | |
echo -e "\e[1;31mError\e[0m : No such directory : $1" | |
exit 2 | |
} | |
dir=$1 | |
shift | |
;; | |
-h | --help) | |
echo "usage: kdev-run.sh [OPTIONS] ... [FILE]" | |
echo -e "Compile and run interactivly the c/c++ code in file\n" | |
echo "Short Options :" | |
echo -e " -h\t\t\tHelp, Display this Message." | |
echo -e " -c\t\t\tRun C code, use this if the file is in C. (defualt)" | |
echo -e " -p\t\t\tRun C++ code, use this if the file is in C++." | |
echo -e " -l <lang>\t\tSpecify the language to use \"C\" or \"C++\"." | |
echo -e " -d <dir>\t\tSet directory to be the build directory. (defualt : /tmp/kdev-run)\n" | |
echo "Long Options :" | |
echo -e " --help\t\tHelp, Display this Message." | |
echo -e " --language=<lang>\tSpecify the language to use \"C\" or \"C++\"." | |
echo -e " --directory=<dir>\tSet directory to be the build directory. (defualt : /tmp/kdev-run)\n\n" | |
echo "Exit Status :" | |
echo -e " 0\tSuccessful execution" | |
echo -e " 1\tInvalid Options" | |
echo -e " 2\tInvalid Directories or Files" | |
echo -e " 3\tUnexpected Error while Pharsing" | |
shift | |
exit 0 | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
done | |
[ -f "$1" ] || { | |
echo -e "\e[1;31mError\e[0m : Invalid File : $1" | |
exit 2 | |
} | |
mkdir -p "$dir" | |
file=$(readlink -f "$1") | |
clear | |
echo -e "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@\e[1;37m\tWelcome to C/C++ programming\e[0m\t@\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" | |
echo "Press 'r' to run the code, 's' save the source and object files (In $HOME/SIDE) and 'd' to discard and exit" | |
read var | |
until [ "$var" == "r" -o "$var" == "s" -o "$var" == "d" ]; | |
do | |
echo -e "\e[1;31mInvalid Option\e[0m" | |
echo "Press 'r' to run the code, 's' save the source and object files (In $HOME/SIDE) and 'd' to discard and exit" | |
read var | |
done | |
while [ "$var" == "r" ]; do | |
cd "$dir" | |
clear | |
if [ "$language" == "c" ]; | |
then | |
gcc -Wall "$file" -o ./a.out # compile the code | |
elif [ "$language" == "c++" ]; | |
then | |
g++ -Wall "$file" -o ./a.out # compile the code | |
fi | |
echo -e "\e[1;37m<<----------------------OutPut---------------------->>\e[0m\n" | |
if [ -f ./a.out ]; | |
then | |
( time ./a.out ) 2> ./time.txt #Run the code | |
#size a.out >> size.txt | |
exit=$? | |
else | |
echo -e "...!!!\e[1;31mError Compiling Program\e[1;37m!!!..." | |
fi | |
echo -e "\n\e[1;37m<<-----------------------END----------------------->>\e[0m" | |
echo " Statistics" | |
echo -e "\n<<-----------------Memory Layout---------------->>" | |
echo "$(size a.out)" | |
echo -e "\n<<----------------Execution time---------------->>" | |
echo "$(cat time.txt)" | |
echo -e "\nExit code : $exit" | |
echo -e "\n\e[1;37m<<------------------------------------------------->>\e[0m" | |
rm -f ./{a.out,time.txt} | |
echo "Press 'r' to run the code again, 's' save the source and object files (In $HOME/SIDE) and 'd' to discard and exit" | |
read var | |
until [ "$var" == "r" -o "$var" == "s" -o "$var" == "d" ]; | |
do | |
echo -e "\e[1;31mInvalid Option\e[0m" | |
echo "Press 'r' to run the code again, 's' save the source and object files (In $HOME/SIDE) and 'd' to discard and exit" | |
read var | |
done | |
done | |
if [ "$var" == "s" ]; then | |
cd "$dir" | |
mkdir -p ~/SIDE | |
gcc -x "$language" "$file" -o ./a.out # compile the code | |
outfile="$(readlink -f ./a.out)" | |
echo "File name for source code (do not add extentions, they are automatically added)" | |
read name | |
named="$name" | |
if [ -f "$file" ]; then | |
mkdir -p ~/SIDE/$named/ | |
if [ "$language" == "c" ]; then | |
cp "$file" ~/SIDE/$named/$name.c | |
exit1=$? | |
chmod 640 ~/SIDE/$named/$name.c | |
elif [ "$language" == "c++" ]; then | |
cp "$file" ~/SIDE/$named/$name.cpp | |
exit1=$? | |
chmod 640 ~/SIDE/$named/$name.cpp | |
fi | |
else | |
echo -e "\e[1;31mError, No Source Code File\e[0m" | |
fi | |
echo "File name for object file (do not add extentions, they are automatically added)" | |
read name | |
if [ -f "$outfile" ]; then | |
mkdir -p ~/SIDE/$named/ | |
cp "$outfile" ~/SIDE/$named/$name.out | |
exit2=$? | |
chmod 750 ~/SIDE/$named/$name.out | |
else | |
echo -e "\e[1;31mError, No Object File\e[0m" | |
fi | |
if [ $exit1 -eq 0 -a $exit2 -eq 0 ]; then | |
echo -e "\e[1;37mMoving files Successfully\e[0m" | |
elif [ $exit1 -eq 1 -a $exit2 -eq 0 ]; then | |
echo -e "\e[1;31mError Moving Source File\nNot All Files Were Saved\e[0m" | |
elif [ $exit1 -eq 0 -a $exit2 -eq 1 ]; then | |
echo -e "\e[1;31mError Moving Object File\nNot All Files Were Saved\e[0m" | |
elif [ $exit1 -eq 1 -a $exit2 -eq 1 ]; then | |
echo -e "\e[1;37mError Moving Files\e[0m" | |
fi | |
rm -f "$outfile" | |
exit | |
elif [ "$var" == "d" ]; | |
then | |
echo -e "\e[1;31mExit\e[0m" | |
if [ -n "$outfile" -a -f "$outfile" ]; then | |
rm -f "$outfile" | |
fi | |
if [ "$dir" == "/tmp/kdev-run" ]; then | |
rm -r "$dir" | |
fi | |
exit | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment