-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestroy
More file actions
executable file
·62 lines (48 loc) · 1.4 KB
/
destroy
File metadata and controls
executable file
·62 lines (48 loc) · 1.4 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
#!/bin/bash
# Function to print an error message and exit
error_exit() {
echo -e "\033[31mError: $1\033[0m" >&2
exit 1
}
# Function to print a warning message
warning_message() {
echo -e "\033[33mWarning: $1\033[0m" >&2
}
# Function to print a success message
success_message() {
echo -e "\033[32mSuccess: $1\033[0m" >&2
}
# Function to print usage message
print_usage() {
echo -e "\033[34mUsage: destroy <file_path>\033[0m" >&2
}
# Validate the argument and get the file size
validate_and_get_size() {
local file_path="$1"
# Check if the file exists
[ -e "$file_path" ] || { error_exit "File '$file_path' does not exist."; return 1; }
# Get the size of the file in bytes
local file_size=$(du -sh "$file_path" | cut -f1)
echo "$file_size"
}
# Main script
# Check if an argument is provided
if [ $# -eq 0 ]; then
print_usage
exit 1 # Exit the script if no argument is provided
fi
file_path="$1"
# Validate the file path and get its size
if ! file_size=$(validate_and_get_size "$file_path"); then
exit 1
fi
# Prompt the user for confirmation
read -rp "Are you sure you want to destroy '$file_path' which contains $file_size? (Y/N): " answer
# Check if the answer is yes
if [[ "$answer" =~ ^[Yy]$ ]]; then
# Remove the file
rm -rfv "$file_path" && success_message "'$file_path' has been destroyed."
else
warning_message "Operation aborted."
exit 1
fi