-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_bash_functions
125 lines (114 loc) · 4.03 KB
/
my_bash_functions
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
114
115
116
117
118
119
120
121
122
123
124
125
# bapt() is a function that will run an `apt install` command in the background to free up your terminal
function bapt() {
SCRIPT_ID=$$
SHELL_ID=$BASHPID
MSG_PREFIX="$SHELL_ID -- $SCRIPT_ID : "
declare -a help_msg=()
# dump passed args to array variable
if [[ $# -gt 1 ]]; then
logger -s "$MSG_PREFIX More than 1 argument was passed correctly. Script continuing."
args=("$@") # capturing passed arguments for future usage
else
help_msg+=("$MSG_PREFIX")
help_msg+=("Not enough args passed to function. Need 2 or more arguments.")
help_msg+=("USAGE: bapt <install> <appname>")
help_msg+=("The first arg passed above is the word 'install' which is the same as saying")
help_msg+=("sudo apt install")
help_msg+=("the second arguments and onward are the app names and/or options")
help_msg+=("")
echo "$MSG_PREFIX ${help_msg[@]}"
return 1
fi
# test command argument. ONLY WORKING WITH "INSTALL" cmd for NOW
case "$1" in
install)
logger -s "$MSG_PREFIX" "Running install on ${args[@]:1}"
;;
*)
logger "$MSG_PREFIX That command is unsupported with bapt() at this time."
logger "$MSG_PREFIX HELP MESSAGE: $help_msg[@]"
return 1
;;
esac
sudo bash -c "apt $* -y >/dev/null 2>&1 & disown"
apt_procID=$!
logger "$MSG_PREFIX apt Proces ID: $apt_procID"
if [ ! -n "$apt_procID" ]; then
logger "INFO: $0 : apt_procid = $apt_procID"
stalk "apt" & disown
return 0
else
return 1
fi
}
# monitor process in the background and report when it closes
function stalk() {
if [ $# -eq 1 ]; then
logger "INFO: $0 : SUCCESS starting with $# arguments"
logger "INFO: $0 : starting monitor for $1"
else
logger -s "ERROR: $0 : FAILED to start with $# arguments"
fi
while [ `pgrep "$1"` ]; do
logger "INFO: $0 : monitoring $1 for closure. Sleep 5 seconds."
#sleep 5
done
if [ ! `pgrep "$1"` ]; then
logger -s "INFO: $0 : process not found. monitoring stopped for $1"
fi
}
# Separate way to fail out of a function without exiting the shell
function fail() {
: "${__fail_fast:?$1}"
}
# may not use this and instead use Logger
function echoerr() {
printf "%s\n" "$*" >&2
}
# Function to mkdir and change to it
function mcd() {
mkdir -p $1
cd $1
}
# Extract function that utilizes lots of different archive tools to extract almost any type of archive
function extract() {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
return 1
else
for n in $@; do
if [ -f "$n" ]; then
case "${n%,}" in
*.tar.bz2 | *.tar.gz | *.tar.xz | *.tbz2 | *.tgz | *.txz | *.tar)
tar xvf "$n"
;;
*.lzma) unlzma ./"$n" ;;
*.bz2) bunzip2 ./"$n" ;;
*.rar) unrar x -ad ./"$n" ;;
*.gz) gunzip ./"$n" ;;
*.zip) unzip ./"$n" ;;
*.z) uncompress ./"$n" ;;
*.7z | *.arj | *.cab | *.chm | *.deb | *.dmg | *.iso | *.lzh | *.msi | *.rpm | *.udf | *.wim | *.xar)
7z x ./"$n"
;;
*.xz) unxz ./"$n" ;;
*.exe) cabextract ./"$n" ;;
*)
echo "extract: '$n' - unknown archive method"
return 1
;;
esac
else
echo "'$n' - file does not exist"
return 1
fi
done
fi
}
# history Averager to find useful commands to turn into functions
function histavg() {
results="$(history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10)"
echo "$results"
}