-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsym
More file actions
executable file
·113 lines (95 loc) · 2.55 KB
/
sym
File metadata and controls
executable file
·113 lines (95 loc) · 2.55 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
#!/bin/bash
# reverses the order of TARGET and LINK_NAME in command `ln`
# TODO: add -sudo option
declare -A option_descriptions=(
[" "]="What happens if no arg given?"
["help"]="Display a help message"
["debug"]="Opens this script with \$EDITOR"
["<link> to <target>"]="main syntax"
["*"]="Default function"
)
main() {
if [ $# -eq 0 ]; then show_usage; fi
case "$1" in
-h | h?lp ) shift ; show_help ;;
-d | d*bug ) shift ; debug_this_script ;;
-from | from) shift ; default_fn "$@" ;;
*) default_fn "$@" ;;
esac
# Process should not reach this point, if it does, maybe forgot exit
echo -e "\e[31mERROR\e[0m" >&2
exit 1
}
# Now add functions here in alphabetical order **********************************************
debug_this_script() {
micro $0
exit "$?"
}
default_fn() {
# Initialize variables
linkname=""
target=""
# Check for missing arguments
if [[ $# -lt 2 ]]; then
err "Missing arguments"
exit 1
fi
# Handle options
case "$1" in
to | -to)
err "Missing link name"
exit 1
;;
*)
linkname="$1"
;;
esac
case "$2" in
to | -to | '-->')
if [[ -z "$3" ]]; then
err "Missing target for symlink"
exit 1
else
target="$3"
fi
;;
*)
target="$2"
;;
esac
# Create the symlink and capture stderr
ln -siv "$target" "$linkname"
# exitstat="$?"
#
# # Check exit status and log appropriately
# if [[ $exitstat -eq 0 ]]; then
# warn "Created symlink $linkname ---> $target"
# exit 0
# else
# err "ERROR: $stderr"
# exit $exitstat
# fi
exit "$?"
}
# Help function to display usage information and option descriptions
show_help() {
# ANSI color codes for colors without using \e[33m and \e[31m
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo "Usage: $(basename "$0") [OPTIONS]"
echo "Options:"
for option in "${!option_descriptions[@]}"; do
printf " ${GREEN}%-12s${NC} %s\n" "$option" "${option_descriptions[$option]}"
done
exit 0
}
show_usage() {
warn 'symlink <link> to <target>'
exit 1
}
# Messages
warn(){ echo -e "\e[33m$@\e[0m"; }
err() { echo -e "\e[31m$@\e[0m" 1>&2; }
#warnings
main "$@" #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<