-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
53 lines (44 loc) · 1.14 KB
/
run.sh
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
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# Default values
file_name="main.cpp"
# Handle command-line options using getopts
while getopts ":f:" opt; do
case $opt in
f)
file_name="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Shift the option index so that $1 points to the first non-option argument (directory path)
shift "$((OPTIND-1))"
# Check if the directory exists
dir_path=$(dirname "$1")
if [ ! -d "$dir_path" ]; then
echo "Error: Directory not found."
exit 1
fi
# Change the current directory to the user-provided directory
cd "$dir_path"
# Check if the main.cpp file exists in the directory
if [ ! -f "$file_name" ]; then
echo "Error: $file_name not found in the directory."
exit 1
fi
# Compile main.cpp using g++
g++ -std=c++17 "$file_name" -o my_program
# Check if the compilation was successful
if [ $? -eq 0 ]; then
echo "Compilation successful."
# Run the compiled program
./my_program
else
echo "Compilation failed."
fi