forked from rake74/bash_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootrc
196 lines (164 loc) · 5.87 KB
/
rootrc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
cjacobsRootrcVer='20170100.01'
cjacobsRootrcMD5="$(md5sum $BASH_SOURCE | awk '{print $2" ("$1")"}')"
pupbranch () {
unset ARGLIST
[ "$1" = 'noop' ] && ARGLIST="$ARGLIST --noop" && shift
BranchName="$1"
[ "x$BranchName-" = 'x-' ] && BranchName='production'
echo -e '\n\n\n'
puppet agent --enable
puppet agent -t --environment=$BranchName $ARGLIST
MyExit=$?
[ "$BranchName" != "production" ] && puppet agent --disable "on branch: $BranchName"
echo -e '\n\n\n'
return $MyExit
}
ps1_red="$( tput setaf 1 )"
ps1_green="$( tput setaf 2 )"
ps1_yellow="$( tput setaf 3 )"
ps1_reset="$( tput sgr0 )"
function ps1_vars {
ps1_retval=$?
[ $ps1_retval -ne 0 ] && ps1_retval_c=$ps1_red || ps1_retval_c=$ps1_green
# default PROMPT_COMMAND is below: sets the window title
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
}
PROMPT_COMMAND=ps1_vars
PS1='[\[$ps1_retval_c\]$ps1_retval\[$ps1_reset\]] \u@\h:\w \$ '
function permpath () {
function permpath_clearvars () {
unset -f permpath_usage permpath_walkdir permpath_printrow
unset noheader noreadlink luser lgroup lperms lpath outarr
}
permpath_clearvars
MyName=$FUNCNAME
noheader=0
noreadlink=0
dirs=( )
function permpath_usage () {
echo "
$MyName [space separated path list] [-H|-R|-h]
Walks a path and identify the user, group, and permissions at each step.
May be called with as many paths as desired.
Options:
-H Skip printing header
-r Use readlink to get real/full path
-R Show output from both w/ and w/o readlink for path
-h, --help, -?, --? print this message and exit (no work)
Example:
$MyName /var/lib/ /var/log -R -H"
[ "x$@-" != "x-" ] && echo
echo "$@"
return
}
# read in all args and create array of paths, and looking for -h noheader flag
# if not argument or valid path, skip it
while [[ $# > 0 ]] ; do
arg="$1"
case "$arg" in
"-H" ) noheader=1 ;;
"-r" ) noreadlink=1 ;;
"-R" ) noreadlink=2 ;;
"-h"|"--help"|"-?"|"--?" ) permpath_usage ; return ;;
* ) [ -e "$arg" ] && dirs+=( "$arg" ) ;;
esac
shift
unset arg
done
# set column header default widths
if [ $noheader -eq 1 ] ; then
luser=0 ; lgroup=0 ; lperms=0 ; lpath=0
else
luser=4 ; lgroup=5 ; lperms=5 ; lpath=4
fi
function permpath_walkdir () {
dir="$1"
[ ! -e "$dir" ] && echo "$dir doesn't seem to exist." 1>&2 && return
# split path into array using / as separator
# then postfix every element with /
# e.g.: /home/bob becomes [0]="/" [1]="home/" [2]="bob/"
# this allows for easily rebuilding path as we walk it
OIFS=$IFS
IFS="/" read -r -a dirpath <<< "$dir"
dirpath=( "${dirpath[@]/%//}" )
dirwalk=''
for (( x=0 ; x<${#dirpath[@]} ; x++ )); do
dirwalk="${dirwalk}${dirpath[x]}"
# this next line is kind hacky
# we already know the path exists
# if dirwalk suddenly doesn't, then this is a /file/
# so remove the last slash added above.
[ ! -e "${dirwalk}" ] && dirwalk="$( echo "$dirwalk" | sed 's/\/$//g')"
read auser agroup aperms <<< $( echo "$(stat "$dirwalk" -c "%U %G %A")" )
[ $luser -lt ${#auser} ] && export luser=${#auser}
[ $lgroup -lt ${#agroup} ] && export lgroup=${#agroup}
[ $lperms -lt ${#aperms} ] && export lperms=${#aperms}
[ $lpath -lt ${#dirwalk} ] && export lpath=${#dirwalk}
outarr[${#outarr[@]}]="$auser $agroup $aperms $dirwalk"
done
unset dirpath OIFS dirwalk
}
# build outarr from each identified path arg
for (( d=0 ; d<${#dirs[@]} ; d++ )) ; do
case $noreadlink in
0 ) permpath_walkdir "${dirs[$d]}" ;;
1 ) permpath_walkdir "$( readlink -f "${dirs[$d]}" )" ;;
2 ) permpath_walkdir "${dirs[$d]}"
permpath_walkdir "$( readlink -f "${dirs[$d]}" )" ;;
esac
done
# used later to print outarr
function permpath_printrow () {
auser="$1" ; shift
agroup="$1" ; shift
aperms="$1" ; shift
apath="$@"
printf "%-${luser}s %-${lgroup}s %-${lperms}s %-${lpath}s\n" \
"${auser}" "${agroup}" "${aperms}" "${apath}"
unset auser agroup aperms apath
}
# print header unless told not to
if [ $noheader -eq 0 ] ; then
permpath_printrow user group perms path
permpath_printrow $(printf '%*s' $luser|tr ' ' -) \
$(printf '%*s' $lgroup|tr ' ' -) \
$(printf '%*s' $lperms|tr ' ' -) \
$(printf '%*s' $lpath | tr ' ' -)
fi
# print results
for (( x=0 ; x<${#outarr[@]} ; x++ )) ; do
permpath_printrow ${outarr[$x]}
done
permpath_clearvars
unset -f permpath_clearvars
}
headtail () {
unset headtail_files headtail_num
while [ $# -gt 0 ] ; do
arg=$1 ; shift
if [[ $arg = -n* ]] ; then
if [ $arg = '-n' ] ; then
headtail_num=$1
shift
else
headtail_num=${arg//-n/}
fi
case ${headtail_num#[-+]} in
''|*[!0-9]* ) echo "-n [num] is not an integer: $headtail_num" ; return 1 ;;
* ) ;;
esac
continue
fi
[ ! -f $arg ] && echo "'$arg' is not a file" && return 1
headtail_files[${#headtail_files[@]}]=$arg
done
[ ${#headtail_files[@]} -eq 0 ] && echo 'no files provided' && return 1
headtail_num=${headtail_num:=1}
for headtail_file in ${headtail_files[@]} ; do
grep -nH ^ $headtail_file | sed 's/:/ : /' | head -n$headtail_num
grep -nH ^ $headtail_file | sed 's/:/ : /' | tail -n$headtail_num
done
}
alias vi=vim
echo "loaded $cjacobsRootrcMD5 ver: $cjacobsRootrcVer"