Skip to content

Instantly share code, notes, and snippets.

@5a494d
Last active July 19, 2018 01:59
Show Gist options
  • Save 5a494d/683c306aacdde7186b319c55482c9bb5 to your computer and use it in GitHub Desktop.
Save 5a494d/683c306aacdde7186b319c55482c9bb5 to your computer and use it in GitHub Desktop.
Execute c/c++ single file with bash
#!/bin/bash
echo "# File name: $1"
CPPFILE=$1
# Remember to put file path to environment system
# .profile .bash_profile .bash_login
# if [ -d "$HOME/.bash" ] ; then
# PATH="$PATH:$HOME/.bash"
# fi
if [ -f "./$CPPFILE" ]; then
file=$(basename -- "$CPPFILE")
CPPEXT="${CPPFILE##*.}"
CPPNAME="${CPPFILE%.*}"
echo "# Compiling"
COMFILE="$CPPNAME.o"
g++ -c ./$CPPFILE -o ./$COMFILE
g++ -o $CPPNAME $COMFILE
if [ -f "./$CPPNAME" ]; then
echo "# Executing file"
chmod +x ./$CPPNAME
echo "######################### BEGIN OUTPUT ##########################"
./$CPPNAME
echo "######################### END OUTPUT ############################"
else
echo "################## HOUSTON WE HAVE A PROBLEM ####################"
fi
else
echo "# File not found!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment