-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplease
More file actions
executable file
·66 lines (53 loc) · 1.57 KB
/
please
File metadata and controls
executable file
·66 lines (53 loc) · 1.57 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
#!/bin/zsh
NAME="please"
VERSION="2.0.0"
PROGRAM="${1:-MISSING}"
ARGS="${2:-MISSING}"
LOCATION="${$(realpath $2)%/*}"
ERR_MSG_MISSING_ARGUMENT="[$NAME][ERROR] Missing 1 or more arguments!\n\tArgument 1: $PROGRAM\n\tArgument 2: $ARGS"
MSG_NO_PERMISSION="[$NAME] You don't have permission to write to $LOCATION! Trying sudo...\n"
showHelp()
{
cat <<- EOF
Usage: $NAME [-hv] <program> <arguments>
-h, --help print help document
-v, --version print script version
Description:
Used to run a program that interacts with a file/directory (i.e: 'nano', 'rm', 'mkdir', 'git', etc).
If you don't have sufficient permissions for the file/directory you're trying to access, '$NAME' will
automatically re-run the program with 'sudo'.
Examples:
$NAME nano /etc/ssh/sshd_config
$NAME rm /usr/local/bin/something
$NAME v$VERSION
EOF
}
OPTIONS=$(getopt -l "help,version" -o "hv" -a -- "$@")
eval set -- "$OPTIONS"
while true
do
case "$1" in
-h|--help)
showHelp
exit 0
;;
-v|--version)
printf "$NAME $VERSION\n"
exit 0
;;
--)
shift
break
;;
esac
shift
done
[[ -z "$2" ]] && { printf "$ERR_MSG_MISSING_ARGUMENT" && exit 1 }
[[ -f "$ARGS" ]] && SELECTION="$ARGS" || SELECTION="$LOCATION"
if [[ $(test -w "$SELECTION" ; echo $?) -eq 0 && $(test -r "$SELECTION" ; echo $?) -eq 0 ]]; then
"$PROGRAM" "$ARGS"
else
printf "$MSG_NO_PERMISSION"
sleep 1
sudo "$PROGRAM" "$ARGS"
fi