Skip to content

Instantly share code, notes, and snippets.

@bokwoon95
Last active March 23, 2020 17:06
Show Gist options
  • Save bokwoon95/80a2aafe236317a636864b576d5f333b to your computer and use it in GitHub Desktop.
Save bokwoon95/80a2aafe236317a636864b576d5f333b to your computer and use it in GitHub Desktop.
Run C files by compiling, executing and deleting the binary. Emulating a C interpreter
#!/bin/bash
in="$1"
out="$(echo "$in" | sed 's/\.c$//g')"
rand="$(date +%s)$(LC_ALL=C tr -dc a-zA-Z0-9 < /dev/urandom | head -c10)"
cleanup() {
rm -rf "$rand$out"
}
trap cleanup EXIT
if ! clang -Wall -Wextra "$in" -o "$rand$out"; then
exit 1
fi
if ! clang -Wall -Wextra -Werror "$in" -o "$rand$out" >/dev/null 2>&1; then
read -p 'Warnings detected, proceed? (enter to proceed, n to abort) ' proceed
if [ "$proceed" = 'n' ]; then
echo 'aborted'
exit 1
fi
fi
./"$rand$out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment