Skip to content

Instantly share code, notes, and snippets.

@haile01
Last active August 28, 2021 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haile01/0962c6f018b153b9697372219d455f68 to your computer and use it in GitHub Desktop.
Save haile01/0962c6f018b153b9697372219d455f68 to your computer and use it in GitHub Desktop.
Bash script for cpp automation in CP for vim enthusiasts
#!/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