Skip to content

Commit 92b844b

Browse files
authored
update share script to optionally take arguments
Add -p $port and -d $directory arguments to allow for better flexibility
1 parent cf16e7c commit 92b844b

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

python-fileShare.sh

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
11
#!/bin/bash
2+
# Script for easily sharing files through http, with easy to copy links provided
3+
# Helper functions and optional argument info from http://mywiki.wooledge.org/BashFAQ/035?action=print
24

3-
#Makes different colored text
5+
#Define ANSI codes for colored text
46
GN="\e[32m"
57
RES="\e[0m"
68
CYAN="\e[1;36m"
79

8-
#font=Big http://www.patorjk.com/software/taag/
9-
echo -e "\n$CYAN""
10-
_____ _ _ ______ _ _ _____
11-
| __ \ | | | | | ____(_) | / ____|
12-
| |__) | _| |_| |__ ___ _ __ | |__ _| | ___| (___ ___ _ ____ _____ _ __
13-
| ___/ | | | __| '_ \ / _ \| '_ \ | __| | | |/ _ \ ___ \ / _ \ '__\ \ / / _ \ '__|
14-
| | | |_| | |_| | | | (_) | | | | | | | | | __/____) | __/ | \ V / __/ |
15-
|_| \__, |\__|_| |_|\___/|_| |_| |_| |_|_|\___|_____/ \___|_| \_/ \___|_|
16-
__/ |
17-
|___/
18-
$RES"
19-
echo -e "Created By$GN Ac1d $RES\n"
20-
echo -e "Updated by$CYAN zweilos $RES\n"
10+
echo -e "Created By$GN Ac1d $RES"
11+
echo -e "Updated by$CYAN zweilosec$RES 20FEB2023\n"
12+
13+
# For printing usage info
14+
show_help() {
15+
cat << EOF
16+
Usage: ${0##*/} [-h] [-d DIRECTORY] [-p PORT]
17+
Share the contents of a directory over HTTP using
18+
python3 -m http.server.
19+
20+
-h display this help and exit
21+
-d DIRECTORY directory to share files from
22+
-p port to host the server on
23+
EOF
24+
}
25+
26+
die() {
27+
printf '%s\n' "$1" >&2
28+
exit 1
29+
}
30+
31+
# Initialize all the option variables.
32+
# This ensures we are not contaminated by variables from the environment.
33+
port=8099
34+
directory="."
35+
36+
while :; do
37+
case $1 in
38+
-h|--help)
39+
show_help
40+
exit 0
41+
;;
42+
-d|--dir)
43+
if [[ -d $2 ]]; then
44+
directory="$2"
45+
shift
46+
else
47+
die 'ERROR: $2 is not a directory!'
48+
fi
49+
;;
50+
-p|--port)
51+
port=$2
52+
shift
53+
;;
54+
*)
55+
break
56+
esac
57+
58+
shift
59+
done
2160

2261
#list IPs associated with current hostname
2362
HN="hostname -I"
2463
#put the IPs into a list
2564
res=$(eval $HN)
2665
arrIN=(${res// / })
2766
IP=""
28-
#TODO:Make GetPort function that takes either user input or cmdline argument for custom port
29-
PORT=8099
3067

3168
#if there is more than one IP available, list the first two as options
3269
#TODO: make a way to list all options
@@ -48,33 +85,32 @@ if [ ${#arrIN[@]} -gt 1 ]; then
4885
;;
4986
#Selecting "3" will exit the program
5087
"Quit")
51-
exit 0
88+
break
5289
;;
53-
5490
#Any input other than 1-3 will result in this error message
55-
*) echo "Invalid option: $REPLY"
56-
;;
91+
*) echo "Invalid option: $REPLY";;
5792
esac
5893
done
5994
else
60-
IP=$arrIN
95+
IP=$arrIN
6196

6297
fi
6398
echo ""
6499
echo "IP: "$IP
65100
echo ""
66101
echo -e "Download files using these links:\n"
67-
for entry in `ls`;do
102+
# For each file in the shared directory, create a wget link
103+
for entry in `ls $directory`;do
68104
if [ ! -d $entry ];then
69-
wgetCmd=$(echo "wget http://${IP##*( )}:8099/$entry" | xargs)
105+
wgetCmd=$(echo "wget http://${IP##*( )}:$port/$entry" | xargs)
70106
echo -e "\t$GN$wgetCmd$RES"
71107
fi
72108
done
73109
echo ""
74-
echo -e "\nCurrent Directory Contents:"
75-
ls --color .
110+
echo -e "\nDirectory Contents of $directory"
111+
ls --color $directory
76112
echo ""
77-
echo -e "\nStarting Server..."
113+
echo -e "\nStarting Server"
78114

79-
#Opens HTTP Server in the folder the command is run from on port $PORT
80-
python3 -m http.server $PORT -d .
115+
#Opens HTTP file server from the specified $directory on port $port
116+
python3 -m http.server $port -d $directory

0 commit comments

Comments
 (0)