-
Notifications
You must be signed in to change notification settings - Fork 17
/
absolutepathit.sh
98 lines (69 loc) · 2.15 KB
/
absolutepathit.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
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
#!/bin/bash
REGEX="^[a-z ]"
USAGE='OsbornePro absolutepathit 2.0 ( https://osbornepro.com )
Usage: absolutepathit [file <string /path/to/script]
OPTIONS:"
-h : Displays the help information for the command.
EXAMPLES:
absolutepathit ~/Documents/Bash/script.sh"
# This example takes the words in script.sh and changes relative paths to absolute paths.
'
function allow_ctrlc {
# Allow Ctrl+C to kill pingsweep
trap '
trap - INT # restore default INT handler
kill -s INT "$$"
' INT
} # End allow_ctrlc
function print_usage {
printf "$USAGE\n" >&2
exit 1
} # End function print_usage
function validate_file {
# Validate script to absoulte path exists
if [ -f "$script" ] && echo "$script file exists. Please wait..." || echo "$script file does not exist. Please define the path to the script you wish to add absolute command values too."; then
declare -a ABSOLUTE_CMDS
else
printf "[x] The file path defined does not exist or you have inadequate permissions."
exit 1
fi
} # End function validate_file
while [ ! -z "$1" ]; do
case "$1" in
-f)
shift
script=$1
;;
*)
print_usage
;;
esac
shift
done
allow_ctrlc
validate_file
# Ignore lines that are commented out
cp "$script" /tmp/absolutepathit_tmpinfo
sed -i -e 's/#.*$//' -e '/^$/d' /tmp/absolutepathit_tmpinfo
# Build an array of possible absolute path values in a script
mapfile -t COMMAND_LIST < /tmp/absolutepathit_tmpinfo
UNIQUE_CMDS=$(echo ${COMMAND_LIST[@]} | tr ' ' '\n' | sort -u | tr '\n' ' ')
# Comment out the below line that sets the word variable if you feel this is overdoing it. This is still a work in progress
word=$(echo $word | rev | cut -f1 -d '(' | rev)
for word in $UNIQUE_CMDS; do
if [[ $word =~ $REGEX ]]; then
if [ -n $word ]; then
THECMD=$(which "$word")
if [ "$THECMD" != "" ]; then
echo "$THECMD is being added to array of commands"
ABSOLUTE_CMDS+=($THECMD)
fi
fi
fi
done
echo "$ABSOLUTE_CMDS"
# Replace the arelative value commands in a script with absolute values
for each_command in ${ABSOLUTE_CMDS[@]}; do
assumed_path=${each_command##*/}
sed -i "s|$assumed_path|$each_command|g" /tmp/absolutepathit_tmpinfo
done