Last active
January 4, 2024 00:05
-
-
Save haseeb-heaven/2a289da737a648ee5868f76e5935b671 to your computer and use it in GitHub Desktop.
A universal script for compiling and running various programming languages. It supports C, C++, Java, Go, C#, Python, JavaScript, Swift, Scala, Ruby, and Kotlin, identifying the language by file extension and using the appropriate compiler or interpreter, usage: ./CodeRunner.sh program.cpp
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 | |
# Get the filename from the command line argument | |
filename=$1 | |
debug=0 | |
cpp_version="c++17" | |
# Determine the file extension, compiler, and language based on the filename | |
if [[ $filename == *.c ]]; then | |
extension=".c" | |
compiler="gcc" | |
language="c" | |
elif [[ $filename == *.cpp ]]; then | |
extension=".cpp" | |
compiler="g++" | |
language="c++" | |
elif [[ $filename == *.java ]]; then | |
extension=".java" | |
compiler="javac" | |
language="java" | |
elif [[ $filename == *.go ]]; then | |
extension=".go" | |
compiler="go run" | |
language="go" | |
elif [[ $filename == *.cs ]]; then | |
extension=".cs" | |
compiler="csc" | |
language="csharp" | |
elif [[ $filename == *.py ]]; then | |
extension=".py" | |
compiler="python" | |
language="python" | |
elif [[ $filename == *.js ]]; then | |
extension=".js" | |
compiler="node" | |
language="javascript" | |
elif [[ $filename == *.swift ]]; then | |
extension=".swift" | |
compiler="swift" | |
language="swift" | |
elif [[ $filename == *.scala ]]; then | |
extension=".scala" | |
compiler="scala" | |
language="scala" | |
elif [[ $filename == *.rb ]]; then | |
extension=".rb" | |
compiler="ruby" | |
language="ruby" | |
elif [[ $filename == *.kt ]]; then | |
extension=".kt" | |
compiler="kotlinc" | |
language="kotlin" | |
else | |
echo "Error: Unsupported file type" | |
exit 1 | |
fi | |
# Check if the user specified a C++ version | |
if [ $language == "c++" ]; then | |
if [[ $3 == c++* ]]; then | |
version=${3#c++} | |
if [[ $version == 17 || $version == 14 || $version == 11 || $version == 0x ]]; then | |
cpp_version="c++$version" | |
fi | |
fi | |
fi | |
# Check if the user specified the "--debug" flag | |
if [[ $2 == "--debug" ]]; then | |
debug=1 | |
fi | |
# Print compilation message if debug mode is enabled | |
if [ $debug -eq 1 ]; then | |
if [ $language == "c++" ]; then | |
echo "Compiling $filename with $compiler (C++ $cpp_version)..." | |
else | |
echo "Compiling $filename with $compiler..." | |
fi | |
fi | |
# Compile the source code based on the language | |
if [ $language == "c++" ]; then | |
$compiler $filename -std=$cpp_version -o ${filename%.*} | |
elif [ $language == "java" ]; then | |
$compiler $filename | |
elif [ $language == "go" ]; then | |
$compiler $filename | |
elif [ $language == "csharp" ]; then | |
$compiler /out:${filename%.*}.exe $filename | |
elif [ $language == "python" ]; then | |
$compiler $filename | |
elif [ $language == "javascript" ]; then | |
$compiler $filename | |
elif [ $language == "swift" ]; then | |
$compiler $filename | |
elif [ $language == "scala" ]; then | |
$compiler $filename | |
elif [ $language == "ruby" ]; then | |
$compiler $filename | |
elif [ $language == "kotlin" ]; then | |
$compiler $filename | |
fi | |
# Check if compilation was successful | |
if [ $? -ne 0 ]; then | |
echo "Compilation failed" | |
exit 1 | |
fi | |
# Print running message if debug mode is enabled | |
if [ $debug -eq 1 ]; then | |
echo "Running ${filename%.*}..." | |
fi | |
# Run the compiled program based on the language | |
if [ $language == "java" ]; then | |
java ${filename%.*} | |
elif [ $language == "go" ]; then | |
$compiler $filename | |
else | |
./${filename%.*} | |
fi | |
# Print finished message if debug mode is enabled | |
if [ $debug -eq 1 ]; then | |
echo "Finished running ${filename%.*}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment