-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_command_path
More file actions
executable file
·51 lines (50 loc) · 892 Bytes
/
get_command_path
File metadata and controls
executable file
·51 lines (50 loc) · 892 Bytes
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
#!/usr/bin/env bash
if [ "$1" = '--help' ]; then
exit 1
elif [ "$1" = '--version' ]; then
exit 1
fi
declare retVal='--name'
if [[ "$1" =~ -([pdna]|-(path|dir|directory|name|all)) ]]; then
retVal="-$1"
shift
fi
declare -r retVal
if [ -e "$1" ]; then
readonly MY_PATH="$1"
else
readonly MY_PATH=$(realpath "$0")
fi
MY_DIR=''
MY_NAME=''
if [[ "$MY_PATH" =~ ^(.*)/([^/]+)$ ]]; then
readonly MY_DIR="${BASH_REMATCH[1]}" MY_NAME="${BASH_REMATCH[2]}"
case "$retVal" in
(-p | --path)
echo "$MY_PATH"
exit
;;
(-d | --dir | --directory)
echo "$MY_DIR"
exit
;;
(-n | --name)
echo "$MY_NAME"
exit
;;
# (-a | --all)
# declare -A MY=(
# [PATH]="$MY_PATH"
# [DIR]="$MY_DIR"
# [NAME]="$MY_NAME"
# )
# declare -p MY
# exit
# ;;
(*) ;;
esac
else
readonly MY_DIR="$HOME" MY_NAME="$0"
echo "$MY_NAME: Failed to get dir" 1>&2
exit 1
fi