-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-script.sh
53 lines (37 loc) · 1.17 KB
/
bash-script.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
#!/usr/bin/env bash
# Current directory and file.
[ -z $( which realpath ) ] && realpath() { cd "$1" >/dev/null 2>&1 && pwd; }
__FILE__="${BASH_SOURCE[0]}"
__DIR__="$( realpath "$( dirname "$__FILE__" )" )"
BASE_DIR="$( realpath "${__DIR__}/.." )"
BASE_DIR="$( realpath "$( dirname "${BASH_SOURCE[0]}" )" )"
_**Note:** Apply `realpath` first to follow/resolve symlinks (i.e. `$(dirname $(realpath $path))`)._
# Usage check.
if (( $# < 1 )); then
echo "Usage: $( basename "$__FILE__" ) <param1> [<optional param2>]"
exit 1
fi
# Must be sourced.
if [ "${BASH_SOURCE}" == "$0" ]; then
echo -e "\e[1;31mThis file must be sourced, not executed.\e[0m"
exit 1
fi
# Script args.
PARAM1=$1
PARAM2=$2
# Function args.
function my_function() { # dir, [options]
# $dir with trailing "/"!
local dir="${1/%\//}/" options="$2"
}
# Keep admin privileges active.
sudo -v
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Trap Ctrl+C
trap ctrl_c INT
function ctrl_c() {
echo "Ctrl + C trapped."
}
# Force exiting the whole script; not just the current long running process,
# while still executing the next commands in the script.
trap exit SIGINT