-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts
More file actions
executable file
·202 lines (178 loc) · 5.09 KB
/
scripts
File metadata and controls
executable file
·202 lines (178 loc) · 5.09 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# script-manager - A utility to manage bash scripts
# Features:
# - List all scripts in specified directory
# - Run scripts with arguments
# - View script contents
# - Create new scripts
# - Delete scripts
# - Edit scripts using default editor
# - Add execution permissions automatically
# Default directory for scripts
SCRIPT_DIR="$HOME/.scripts"
# Create scripts directory if it doesn't exist
if [ ! -d "$SCRIPT_DIR" ]; then
mkdir -p "$SCRIPT_DIR"
echo "Created scripts directory at $SCRIPT_DIR"
fi
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Print help message
show_help() {
echo -e "${BLUE}Script Manager - Usage:${NC}"
echo " -l, --list : List all scripts"
echo " -r, --run : Run a script"
echo " -v, --view : View script contents"
echo " -c, --create : Create a new script"
echo " -d, --delete : Delete a script"
echo " -e, --edit : Edit a script"
echo " -h, --help : Show this help message"
}
# List all scripts in the directory
list_scripts() {
echo -e "${BLUE}Available scripts in $SCRIPT_DIR:${NC}"
if [ -d "$SCRIPT_DIR" ]; then
find "$SCRIPT_DIR" -type f -executable -not -name "README.md" -exec basename {} \;
else
echo -e "${RED}Scripts directory does not exist!${NC}"
exit 1
fi
}
# Run a script with arguments
run_script() {
local script_name="$1"
shift # Remove script name from arguments
if [ -f "$SCRIPT_DIR/$script_name" ]; then
echo -e "${GREEN}Running $script_name...${NC}"
chmod +x "$SCRIPT_DIR/$script_name"
"$SCRIPT_DIR/$script_name" "$@"
else
echo -e "${RED}Script $script_name not found!${NC}"
exit 1
fi
}
# View script contents
view_script() {
local script_name="$1"
if [ -f "$SCRIPT_DIR/$script_name" ]; then
echo -e "${BLUE}Contents of $script_name:${NC}"
echo "----------------------------------------"
cat "$SCRIPT_DIR/$script_name"
echo "----------------------------------------"
else
echo -e "${RED}Script $script_name not found!${NC}"
exit 1
fi
}
# Create a new script
create_script() {
local script_name="$1"
if [ -z "$script_name" ]; then
echo -e "${RED}Please provide a script name!${NC}"
exit 1
fi
if [[ ! "$script_name" =~ \.(sh|bash)$ ]]; then
script_name="$script_name"
fi
if [ -f "$SCRIPT_DIR/$script_name" ]; then
echo -e "${RED}Script $script_name already exists!${NC}"
exit 1
fi
# Create script with basic template
cat > "$SCRIPT_DIR/$script_name" << 'EOF'
#!/bin/bash
# Script: script_name
# Description: Add your description here
# Author: Add your name here
# Date: $(date +%Y-%m-%d)
# Your code here
EOF
chmod +x "$SCRIPT_DIR/$script_name"
echo -e "${GREEN}Created script $script_name${NC}"
echo "Would you like to edit it now? (y/n)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
edit_script "$script_name"
fi
}
# Delete a script
delete_script() {
local script_name="$1"
if [ -f "$SCRIPT_DIR/$script_name" ]; then
echo -e "${RED}Are you sure you want to delete $script_name? (y/n)${NC}"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
rm "$SCRIPT_DIR/$script_name"
echo -e "${GREEN}Deleted $script_name${NC}"
fi
else
echo -e "${RED}Script $script_name not found!${NC}"
exit 1
fi
}
# Edit a script using default editor
edit_script() {
local script_name="$1"
if [ -f "$SCRIPT_DIR/$script_name" ]; then
if [ -n "$EDITOR" ]; then
$EDITOR "$SCRIPT_DIR/$script_name"
elif command -v nano >/dev/null 2>&1; then
nano "$SCRIPT_DIR/$script_name"
elif command -v vim >/dev/null 2>&1; then
vim "$SCRIPT_DIR/$script_name"
else
echo -e "${RED}No suitable editor found!${NC}"
exit 1
fi
else
echo -e "${RED}Script $script_name not found!${NC}"
exit 1
fi
}
# Main script logic
case "$1" in
-l|--list)
list_scripts
;;
-r|--run)
if [ -z "$2" ]; then
echo -e "${RED}Please provide a script name to run!${NC}"
exit 1
fi
run_script "$2" "${@:3}"
;;
-v|--view)
if [ -z "$2" ]; then
echo -e "${RED}Please provide a script name to view!${NC}"
exit 1
fi
view_script "$2"
;;
-c|--create)
if [ -z "$2" ]; then
echo -e "${RED}Please provide a name for the new script!${NC}"
exit 1
fi
create_script "$2"
;;
-d|--delete)
if [ -z "$2" ]; then
echo -e "${RED}Please provide a script name to delete!${NC}"
exit 1
fi
delete_script "$2"
;;
-e|--edit)
if [ -z "$2" ]; then
echo -e "${RED}Please provide a script name to edit!${NC}"
exit 1
fi
edit_script "$2"
;;
-h|--help|*)
show_help
;;
esac