-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-it.sh
executable file
·62 lines (54 loc) · 1.66 KB
/
shell-it.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
54
55
56
57
58
59
60
61
62
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
showHelp() {
echo -e "${GREEN}Usage: $0 [DIRECTORY]${NC}"
echo
echo "Lists .sh scripts in the specified DIRECTORY (or the current directory if none is specified)"
echo "and allows the user to select and run one of the scripts."
echo
echo "Options:"
echo " --help Show this help message and exit"
}
if [[ "$1" == "--help" ]]; then
showHelp
exit 0
fi
# Defaults to cwd if no path is provided.
if [ -z "$1" ]; then
target_dir=$(pwd)
else
target_dir="$1"
fi
files_array=()
counter=1
# Make key-value array for each .sh file.
for file in "$target_dir"/*.sh; do
if [ -e "$file" ]; then
# shell-it ignores itself.
if [ "$(basename "$file")" != "shell-it.sh" ]; then
files_array+=("$(basename "$file")")
counter=$((counter + 1))
fi
fi
done
if [ ${#files_array[@]} -eq 0 ]; then
echo -e "${RED}No .sh files found in ${target_dir}.${NC}"
else
echo -e "${GREEN}Listing scripts in ${target_dir}:${NC}";
for i in "${!files_array[@]}"; do
echo "$((i + 1)): ${files_array[$i]}"
done
while true; do
read -p "Enter number from 1 to ${#files_array[@]}: " usr_select
if [[ "$usr_select" =~ ^[0-9]+$ ]] && [ "$usr_select" -ge 1 ] && [ "$usr_select" -le ${#files_array[@]} ]; then
script_to_run="${files_array[$((usr_select - 1))]}"
echo -e "${GREEN}Running script: $(basename "$script_to_run")${NC}"
bash "$script_to_run"
break
else
echo -e "${RED}Invalid input, enter value from 1 to ${#files_array[@]}.${NC}"
fi
done
fi