-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_py.sh
50 lines (42 loc) · 1.06 KB
/
run_py.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
#!/bin/bash
# Default values
file_name="main.py"
# 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.py file exists in the directory
if [ ! -f "$file_name" ]; then
echo "Error: $file_name not found in the directory."
exit 1
fi
# Run the Python script
python3 "$file_name"
# Check if the execution was successful
if [ $? -eq 0 ]; then
echo "Execution successful."
else
echo "Execution failed."
fi