Last active
May 31, 2020 09:59
-
-
Save nilput/8e2f75edf43607c065fa033f020bef74 to your computer and use it in GitHub Desktop.
a script to compile and execute C snippets
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 | |
| function die() { | |
| echo "$*" >&2 | |
| [ -n "$tmp" -a -d "$tmp" ] && rm -rf "$tmp" | |
| exit 1; | |
| } | |
| tmp="$(mktemp -d -t cexec-XXXXXX)" && [ -n "$tmp" -a -d "$tmp" ] || die "couldn't create tmp dir"; | |
| [ "$1" = '-h' ] && die "$(printf "Usage: $0 <<<'C Source code")" | |
| cat <(printf "#include <%s.h>\n" stdio stdlib string) \ | |
| <(printf 'int main(int argc,char *argv[]){') \ | |
| - \ | |
| <(echo "}") | gcc -o "$tmp/prog" -x c - || die "compilation failed" && "$tmp"/prog "$@" | |
| die 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment