-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_cpp.sh
More file actions
executable file
·42 lines (34 loc) · 864 Bytes
/
run_cpp.sh
File metadata and controls
executable file
·42 lines (34 loc) · 864 Bytes
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
#!/bin/bash
# Set filenames
SRC="code.cpp"
EXE="code.out"
INPUT="input.txt"
OUTPUT="output.txt"
DEBUG="dbg.txt"
# Compile the C++ source file with debug info
echo "Compiling $SRC..."
g++ -Wall -Wextra -O2 "$SRC" -o "$EXE" 2>"$DEBUG"
# Check if compilation was successful
if [[ $? -ne 0 ]]; then
echo "Compilation failed. See $DEBUG for errors."
exit 1
fi
echo "Running $EXE..."
# Run the program with input and redirect stdout and stderr
./"$EXE" <"$INPUT" >"$OUTPUT" 2>>"$DEBUG"
# Function to display file contents
display_file() {
local filename="$1"
if [[ -f "$filename" ]]; then
echo -e "\nContents of $filename:"
cat "$filename"
else
echo "$filename does not exist."
fi
}
# Display results
display_file "$OUTPUT"
display_file "$DEBUG"
# Prompt for exit
echo -e "\nPress any key to exit..."
read -n 1 -s