Last active
August 28, 2021 09:26
-
-
Save haile01/0962c6f018b153b9697372219d455f68 to your computer and use it in GitHub Desktop.
Bash script for cpp automation in CP for vim enthusiasts
This file contains hidden or 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 | |
# A simple bash script to automate compiling and running CP codes for vim enthusiasts | |
# === HOW TO USE === | |
# It is recommended to split up to 4 screens: code editor (1), gcc output (2), input editor (3), output watch (4) | |
# and create 3 files: <name>.cpp, <name>.inp, <name>.out | |
# - Run "cpp.sh <name>" in gcc output (2) | |
# - Run "tail -f -v <name>.out" in output watch (4) | |
# - Code as you like in <name>.cpp file and edit input in <name>.inp, any save action in vim would trigger the script to compile and run the code | |
# Disclaimer: Use standard I/O, the script've already piped input and output to the program. | |
if [[ $1 == "--prepare" ]]; then | |
echo -e "#include <bits/stdc++.h>\nusing namespace std;\n\n\nint main() {\n\t// Main source code here\n}" > "./$2.cpp"; | |
touch "./$2.inp"; | |
touch "./$2.out"; | |
else | |
path=$(pwd); | |
echo "Watching $1 in $path"; | |
inotifywait -mqe close_write --format "%w%f" "$path" | while read -r filename; do | |
if [ "$filename" = "$path/$1.cpp" -o "$filename" = "$path/$1.inp" ]; then | |
echo "Compiling $1.cpp" | |
if gcc "./$1.cpp" -lstdc++ -o main; then | |
echo "Complile successful"; | |
echo "====Running===="; | |
./main < "./$1.inp" > "./$1.out"; | |
rm -rf ./main; | |
else | |
echo "Compile error"; | |
fi | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment