-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_functions
executable file
·162 lines (141 loc) · 3.83 KB
/
.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
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
mkc() {
mkdir -p -- "$1" &&
cd -P -- "$1"
}
find_files() {
while IFS= read -r f; do
if [[ -e $2/$f ]]; then
printf '%s Exists! \n' "$f"
fi
done < "$1"
}
get_latest_git_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
# Usage
# $ get_latest_release "creationix/nvm"
# v0.31.4
unzip_all() {
d=${1%.*}
mkdir "$d"
echo "Unzipping $1 to $d..."
unzip -q $1 -d $d
}
nf() {
STR=$( echo "#!"`which "$1"` )
if [ ! -f "$2" ]; then
echo "$STR" > "$2"
echo "" >> "$2"
if [ -z ${3+x} ]; then EDT="nano +2"; else EDT="$3 +2"; fi
$EDT "$2"
chmod +x "$2"
else
echo "File already exists!"
fi
}
delete_line () {
if [ $# -ne 2 ]; then
printf "Expected 2 arguments (Line, Filename), received $#"
kill -INT $$
fi
read -p "Delete all lines containing `tput bold`$1`tput sgr0` from `tput bold`$2`tput sgr0`? (y/n)" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
grep -vwE "$1" "$2" >| "$2".deleted
echo "Saved as "$2".deleted"
fi
}
bt_fn_lock () {
if [ "$1" == 0 ] ; then
echo "FnLk On" ;
echo "$1" | sudo tee /sys/bus/hid/devices/*17EF\:604*/fn_lock > /dev/null ;
elif [ "$1" == 1 ] ; then
echo "FnLk Off" ;
echo "$1" | sudo tee /sys/bus/hid/devices/*17EF\:604*/fn_lock > /dev/null ;
else
echo "invalid option" ;
fi
}
show_permissions () {
PASSED="$1";
if [[ -d $PASSED ]]; then
local directory_given=$(readlink -f $PASSED)
for filename in $directory_given/*; do
stat -c "%a %n" $filename
done
elif [[ -f $PASSED ]]; then
stat -c "%a %n" $PASSED
else
echo "$PASSED is not valid"
fi
}
open_at_error () {
while read data; do
echo $(grep -o -P "line [0-9].{0,1}" | awk '{print $2}' | head -1) >| /tmp/error_number;
done;
}
relpath(){ python -c "import os.path; print os.path.relpath('$1','${2:-$PWD}')" ; }
show_latest () {
if [ -z "$1" ]
then
local dir=$PWD
else
local dir="$1"
fi
ls --sort=time --reverse "$dir" | tail -1 ;
}
add_comment () {
if [ -z "$1" ]
then
echo "Usage: add_comment [comment] [filename]"
else
cur_com=$( getfattr -d -m - "$2" | grep user.comment )
if [ -z "$cur_com" ]
then
setfattr -n user.comment -v "$1" "$2" && printf "Added comment "$1" to file "$2"\n"
else
printf "There is already a comment found:\n$( getfattr -d -m - "$2" )\n"
read -p "Overwrite? [y/n] " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
setfattr -n user.comment -v "$1" "$2" && printf "Added comment "$1" to file "$2"\n"
fi
fi
fi
}
view_comment () {
if [ -z "$1" ]
then
echo "You must provide a file"
else
getfattr -d -m - "$@"
fi
}
remove_comment () {
if [ -z "$1" ]
then
echo "You must provide a file"
else
setfattr -x user.comment "$1"
fi
}
list_comments () {
shopt -s dotglob
if [ -z "$1" ]
then
local d=$PWD
else
[[ -d "$1" ]] || local d="$1"
fi
for f in $d/*
do
local col1=$( ls --color=always $( relpath $f $d ) )
local col2=$( view_comment $( relpath $f $d ) | tail -n +2 )
echo "$col1" "${col2#*=}" | column -c 2 -t
done
shopt -u dotglob #This is probably unnecessary as the subshell is separate
}