-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·127 lines (97 loc) · 2.68 KB
/
main.sh
File metadata and controls
executable file
·127 lines (97 loc) · 2.68 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
INDEX_URL="https://raw.githubusercontent.com/base2code/linux-scripts/main/index.json"
BASE_URL="https://raw.githubusercontent.com/base2code/linux-scripts/main/scripts/"
NUMBER_REGEX='^[0-9]+$'
# Check if jq (json serialization) is installed
if ! command -v jq &> /dev/null
then
echo "jq could not be found"
exit
fi
# Check if curl ist installed
if ! command -v curl &> /dev/null
then
echo "curl could not be found"
exit
fi
# Download index.json
index=$(curl -sSL $INDEX_URL)
if ! [ $? -eq 0 ]; then
echo "Could not download index"
echo $index
exit
fi
declare -a script_names
script_names=($(echo $index | jq -r ".[] | .name"))
given_name=$1
if [ -z "$given_name" ]; then
# initialize counter for selection menu
declare -i i
i=0
echo ""
echo "Please choose a script to execute:"
echo ""
for script in ${script_names[@]}; do
echo "[${i}] ${script} - $(echo $index | jq -r ".[${i}] | .description")"
i=i+1
done
elements=i-1
echo "Please type in a number from 0 to ${elements}:"
read -r selection
if ! [[ $selection =~ $NUMBER_REGEX ]] ; then
echo "error: Not a number"
exit
fi
if (( $elements < $selection )); then
echo "selection is greater than availiable scripts"
exit
elif (( $selection < 0 )); then
echo "selection is smaller than 0"
exit
fi
selected_name=${script_names[$((${selection}))]}
else
echo "Seach term "$given_name" has beed detected"
regex_search="${given_name}*"
for script in ${script_names[@]}; do
if [[ $script =~ $regex_search ]]; then
selected_name=$script
fi
done
if [ -z $selected_name ]; then
echo "Specified script could not be found."
exit 1
fi
fi
echo ""
echo "Selected script: ${selected_name}"
echo ""
final_url="${BASE_URL}${selected_name}"
read -r -p "Should I download the script and execute it? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
echo "Downloading and executing script..."
echo ""
curl -sSL $final_url | sh
if ! [ $? -eq 0 ]; then
echo "Error during script download!"
echo "This is the script URL: ${final_url}"
exit 1
fi
exit 0
;;
esac
read -r -p "Where should I download the script? [.] " output
output=${output:-.}
location="${output}/${selected_name}"
curl -sSL -o $location $final_url
if ! [ $? -eq 0 ]; then
echo "Error during script download!"
echo "This is the script URL: ${final_url}"
echo "Target: ${location}"
exit 1
fi
echo "Downloaded the script to: ${location}"
echo "Do not forget to make script executable."
echo ""
echo "Script finished successfully"