-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodepy
More file actions
executable file
·34 lines (30 loc) · 1.17 KB
/
codepy
File metadata and controls
executable file
·34 lines (30 loc) · 1.17 KB
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
#!/bin/bash
# Loop through all arguments
for filename in "$@"; do
# Check if the file has a .py extension
if [[ "$filename" == *.py ]]; then
# Check if the file exists and is not empty
if [ -s "$filename" ]; then
# Check if the file is executable, and if not, make it executable
if ! [ -x "$filename" ]; then
chmod +x "$filename"
fi
# Check if the first line contains the shebang
if ! head -n 1 "$filename" | grep -q '#!/usr/bin/python3'; then
# Append the shebang to the beginning of the file using a different delimiter
sed -i '1s|^|#!/usr/bin/python3\n|' "$filename"
fi
else
# Create an empty file
touch "$filename"
# Give execute permission (if needed)
chmod +x "$filename"
# Add a Python shebang to the file
echo '#!/usr/bin/python3' > "$filename"
fi
# Open the file for editing with the cursor at the last character
code --goto "$filename:$(($(wc -c < "$filename") - 1))"
else
echo "Skipping $filename: Not a .py file"
fi
done