-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun
executable file
·42 lines (35 loc) · 880 Bytes
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
build() {
case "$1" in
*.c) gcc "$1" -o "$2" ;;
*.cpp) g++ "$1" -o "$2" ;;
*)
echo >&2 "run: error: unsupported file type"
exit 1
;;
esac
}
run_executable() {
"$1"
}
main() {
src=$(command -v realpath >/dev/null 2>&1 && realpath "$1" || readlink -f "$1")
executable=$(echo "$src" | sed -E 's/\.(c|cpp)$/.run/')
if [ "$src" = "$executable" ]; then
echo >&2 "run: error: file name cannot be the same as the run file name"
exit 1
fi
if [ ! -f "$src" ]; then
echo >&2 "run: error: source file does not exist"
exit 1
fi
build "$src" "$executable"
if [ ! -f "$executable" ]; then
echo >&2 "run: error: build failed"
exit 1
fi
run_executable "$executable"
rm -f "$executable"
}
main "$1"