when doing ssh-add privkey, using completion, I should provide the complete path...
[dotfiles] / .bash_function
1 #!/bin/sh
2 # my weird function file
3
4 not_end_by () {
5 local by=$1;
6 # grep -v: inverse match
7 grep -v $by"$"
8 }
9
10 end_by () {
11 local by=$1;
12 grep $by"$"
13 }
14
15 begin_by () {
16 local by=$1;
17 grep "^"$by;
18 }
19
20 not_begin_by () {
21 local by=$1;
22 grep -v "^"$by
23 }
24
25 my_venv_list(){
26 ls $HOME"/envs"
27 }
28
29 my_venv_goto() {
30 local dir=$1
31 local env=$HOME"/envs/"$dir"/source/"
32 cd $env
33 source activate
34 }
35
36 # Taken from http://aaroncrane.co.uk/2009/03/git_branch_prompt/
37 find_git_repo() {
38 local dir=.
39 until [ "$dir" -ef / ]; do
40 if [ -f "$dir/.git/HEAD" ]; then
41 GIT_REPO=`readlink -e $dir`/
42 return
43 fi
44 dir="../$dir"
45 done
46 GIT_REPO=''
47 return
48 }
49
50 find_git_branch() {
51 head=$(< "$1/.git/HEAD")
52 if [[ $head == ref:\ refs/heads/* ]]; then
53 GIT_BRANCH=${head#*/*/}
54 elif [[ $head != '' ]]; then
55 GIT_BRANCH='(detached)'
56 else
57 GIT_BRANCH='(unknown)'
58 fi
59 }
60
61 # Taken from https://github.com/jimeh/git-aware-prompt
62 find_git_dirty() {
63 local status=$(git status --porcelain 2> /dev/null)
64 if [[ "$status" != "" ]]; then
65 GIT_DIRTY='*'
66 else
67 GIT_DIRTY=''
68 fi
69 }
70
71 find_git_stash() {
72 if [ -e "$1/.git/refs/stash" ]; then
73 GIT_STASH='stash'
74 else
75 GIT_STASH=''
76 fi
77 }
78
79 cdls() { cd "$@" && clear && ls; }
80
81
82 cl(){
83 if [ -d "$1" ]; then
84 cd "$1"
85 clear
86 ls -la
87 else
88 cd $HOME
89 clear
90 ls
91 fi
92 }
93
94 mkdircd(){
95 mkdir -p "$@" && eval cd "\"\$$#\"";
96 }
97
98 my_emacs_servers(){
99 local serverdir="${TMPDIR:-/tmp}/emacs${UID}"
100 local -a servers
101 for file in ${serverdir}/*; do
102 if [[ -S ${file} ]]; then
103 servers+=("${file##*/}")
104 fi
105 done
106
107 echo "${servers[@]}"
108 }
109
110 my_fib(){
111 x=${1:-1}
112 y=${2:-1}
113 echo $(($x + $y))
114 fib $y $(($x + $y))
115 }
116
117
118 my_recursiveFibo() {
119 local start=0;
120 n="$1";
121 inc="$2";
122 while ((n < $inc));
123 do
124 result=$((start+n));
125 start=$n;
126 n="$result";
127 printf '%s ' "$result";
128 done;
129 }
130
131 #recursivFibo 1 2048
132
133 my_err() {
134 echo "[$(date + '%Y-%m-%dT%H:%M:%S%z')]: $@ >&2"
135 }
136
137 my_test() {
138 echo 'test';
139 }
140
141 my_test2(){
142 if my_test; then
143 my_err "unable to my_test"
144 exit "${E_DID_NOTHING}"
145 fi
146 }
147
148 my_pretty_print_json() {
149 python -m json.tool
150 }
151
152 my_not_start_service_on_boot(){
153 # service to not start on boot
154 local service=$1;
155 update-rc.d $service disable;
156 }
157
158 my_install_new_font(){
159 #sudo fc-cache -v -f
160 fc-cache -v -f;
161 }
162
163 my_launch_eth0(){
164 # set eth0 iface up
165 sudo ip link set eth0 up;
166 # ask for an ip via dhcp
167 sudo dhclient eth0;
168 }
169
170 #function to show the current branch
171 my_show_current_branch(){
172 git rev-parse --abbrev-ref HEAD
173 }
174
175 my_pdf_nbpages(){
176 local filename=$1;
177
178 echo $(pdftk $filename dump_data \
179 | grep -i numberofpages \
180 | awk '{print $2}');
181 }
182
183 my_pdf_explosion() {
184 local filename=$1;
185 echo $(pdftk $filename burst);
186 }
187
188 my_pdfs_to_xml() {
189 for i in $(ls | grep .pdf$); do
190 pdftohtml -xml $i;
191 done;
192 }
193
194 # function to show one line of file
195 my_xline_of_file(){
196 local line_number=$1;
197 local filename=$2;
198 local p='p';
199
200 echo $(sed -n $line_number$p $filename);
201 }
202
203 # function to extract in one command
204 my_xtract() {
205 local filename=$1;
206
207 if is_file $filename ; then
208 case $filename in
209 *.tar.xz) tar xvfJ $filename ;;
210 *.xz) tar xf $filename ;;
211 *.tar.bz2) tar xvjf $filename ;;
212 *.tar.gz) tar xvzf $filename ;;
213 *.bz2) bunzip2 $filename ;;
214 *.rar) unrar x $filename ;;
215 *.gz) gunzip $filename ;;
216 *.tar) tar xvf $filename ;;
217 *.tbz2) tar xvjf $filename ;;
218 *.tgz) tar xvzf $filename ;;
219 *.zip) unzip $filename ;;
220 *.Z) uncompress $filename ;;
221 *.7z) 7z x $filename ;;
222 *) echo "'$filename' cannot be extracted via extract" ;;
223 esac
224 else
225 echo "'$filename' is not a valid file"
226 fi
227 }
228
229 # function to list the content of an archive
230 my_list_archive_content() {
231 local filename=$1;
232
233 if is_file $filename ; then
234 case $filename in
235 *.tar.xz) tar tvfJ $filename ;;
236 *.tar.bz2) tar tvjf $filename ;;
237 *.tar.gz) tar tzvf $filename ;;
238 *.bz2) bunzip2 $filename ;;
239 *.gz) gunzip -l $filename ;;
240 *.tar) tar tvf $filename ;;
241 *.tbz2) tar tvjf $filename ;;
242 *.tgz) tar tvzf $filename ;;
243 *.zip) unzip -l $filename ;;
244 *.Z) uncompress -l $filename ;;
245 *.7z) 7z l $filename ;;
246 *) echo "content of '$filename' cannot be listed :(" ;;
247 esac
248 else
249 echo "'$filename' is not a valid file"
250 fi
251 }
252
253 # dump a mysql database
254 my_dump_mysql_db() {
255 local database=$1;
256
257 echo "dumping database '$database' ..."
258 mysqldump -u patrick -ppatrick $database > $databse_dump_$(date +%m%d%Y).sql
259
260 }
261
262 my_dump_mysql_table() {
263 local database=$1;
264 local table=$2;
265
266 echo "dumping table '$table' from database '$database' ..."
267 mysqldump -u patrick -ppatrick $database $table > $table_dump_$(date +%m%d%Y).sql
268 }
269
270 my_uploadfile() {
271 local filename=$1
272 local ftp_url=$2;
273 local user=$3;
274 local password=$4;
275
276 echo "upload of '$filename' to '$ftp_url' by '$user' ..."
277 #curl -T $filename ftp://dev.africafilms.tv/movies/ --user dev.africafilms.tv:dev/aftv/DAK2602
278 curl -T $filename $ftp_url --user $user:$password
279 }
280
281 my_find_string() {
282 local string_to_find=$1;
283 local directory=$2;
284
285 echo "recursive looking for the string '$string' inside '$directory' directory"
286 grep -l -R -i -r $string_to_find $directory;
287 }
288
289 my_find_string2() {
290 local string_to_find=$1;
291 local directory=$2;
292
293 echo "recursive looking for the string '$string' inside '$directory' directory"
294 find $directory | xargs grep $string -sl;
295
296 }
297 # extract md5 hash from a string
298 my_xmd5() {
299 local string=$1;
300
301 echo "md5 hash of '$string' is: "
302 echo -n "$string" | md5sum;
303 }
304
305 # Creates an archive (*.tar.gz) from given directory.
306 my_maketar() {
307 local directory=$1;
308
309 tar cvzf "${directory%%/}.tar.gz" "${directory%%/}/";
310 }
311
312 # Create a ZIP archive of a file or folder.
313 my_makezip() {
314 local file=$1;
315
316 zip -r "${file%%/}.zip" "$file";
317 }
318
319 # Make your directories and files access rights sane.
320 my_sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
321
322 # Get IP adress on ethernet.
323 my_ip() {
324 local my_ip=$( /sbin/ifconfig eth0 \
325 | awk '/inet/ { print $2 } ' \
326 | sed -e s/addr://);
327 local my_gtw="gtw: "$(netstat -nr | awk '{print $2}' | sed -n 3p);
328 echo ${my_ip:-"Not connected"}
329 echo ${my_gtw:-"Not connected"}
330 }
331
332 # Get current host related info.
333 my_host_info() {
334 echo -e "\nYou are logged on ${BRed}$HOST"
335 echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
336 echo -e "\n${BRed}Current date :$NC " ; date; calendar | head -1
337 echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
338 echo -e "\n${BRed}Users logged on:$NC " ; \
339 w -hs \
340 | cut -d " " -f1 \
341 | sort \
342 | uniq
343 echo -e "\n${BRed}Machine stats :$NC " ; uptime
344 echo -e "\n${BRed}Memory stats :$NC " ; free -h
345 echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
346 echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet;
347 echo
348 }
349
350 # Pretty-print of 'df' output. Inspired by 'dfc' utility.
351 my_df() {
352 local fs=$1;
353
354 for fs ; do
355
356 if [ ! -d $fs ]
357 then
358 echo -e $fs" :No such file or directory" ; continue
359 fi
360
361 local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
362 local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
363 local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
364 local out="["
365 for ((j=0;j<20;j++)); do
366 if [ ${j} -lt ${nbstars} ]; then
367 out=$out"*"
368 else
369 out=$out"-"
370 fi
371 done
372 out=${info[2]}" "$out"] ("$free" free on "$fs")"
373 echo -e $out
374 done
375 }
376
377 # create_vm(){
378 # debootstrap --verbose \
379 # --variant=minbase \
380 # --arch=i386 --include ifupdown,locales,libui-dialog-perl,dialog,dhcp3-client,netbase,net-tools,iproute,openssh-server \
381 # sid /var/lib/lxc/sid http://ftp.debian.org/debian
382
383 # }
384
385
386 my_add_todo(){
387 local rememberfile="$HOME/.remember"
388 # if the file not exists, touch it
389 [[ ! -f $rememberfile ]] && touch $rememberfile
390 if [ $# -eq 0 ] ; then
391 echo "Type what you want to remember then, hit ^D: "
392 cat - >> $rememberfile;
393 else
394 #echo $(date +%m%d%Y)" -- $@" >> $rememberfile
395 echo $(date +%F)" -- $@" >> $rememberfile
396 fi
397 }
398
399 my_reminder(){
400 local rememberfile="$HOME/.remember"
401
402 #is_not_file $remberfile && echo 'nothing to do' ||
403 #is_empty $rememberfile && echo 'nothing to do' ||
404 #display_todo "$@"
405 is_not_file $rememberfile || [[ $(cat $rememberfile | wc -l) = 0 ]] && echo "nothing to do" || my_display_todo "$@";
406 }
407
408 my_display_todo(){
409
410 local rememberfile="$HOME/.remember"
411 echo $#;
412 #[[ $# -eq 0 ]] && more $rememberfile || grep -i "$@" $rememberfile | more;
413 [[ $# -eq 0 ]] && is_file $rememberfile && is_not_empty_file $rememberfile && cat $rememberfile || grep -i "$@" $rememberfile;
414 }
415
416
417 my_lsbytesum() {
418 #lsbytesum - sum the number of bytes in a directory listing
419 TotalBytes=0
420 for Bytes in $(ls -l | grep "^-" | awk '{ print $5 }')
421 do
422 let TotalBytes=$TotalBytes+$Bytes
423 done
424 TotalMeg=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc)
425 echo -n "$TotalMeg"
426 }
427
428 # list
429 my_create_list(){
430 local listname=$1;
431 declare -a $listname;
432 }
433
434 # sensonet
435
436 sensonet_create_event() {
437 curl -i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "name=$1" http://projects.emerginov.orange.sn/sensonet/resources/events/"$2"
438 }
439
440 sensonet_update_event() {
441 curl -i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "name=$1" http://projects.emerginov.orange.sn/sensonet/resources/events/"$2"
442 }
443
444 sensonet_events() {
445 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/events/ || \
446 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/events/"$1"
447 }
448
449 sensonet_del_event() {
450 curl -i -X "DELETE" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/events/"$1"
451 }
452
453 sensonet_create_sensor() {
454 curl -i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "description=$1" http://projects.emerginov.orange.sn/sensonet/resources/sensors/"$2"
455 }
456
457 sensonet_update_sensor() {
458 curl -i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" -d "description=$1" http://projects.emerginov.orange.sn/sensonet/resources/sensors/"$2"
459 }
460
461 sensonet_del_sensor() {
462 curl -i -X "DELETE" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/sensors/"$1"
463 }
464
465 sensonet_sensors() {
466 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/sensors/ || \
467 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/sensors/"$1"
468 }
469
470 sensonet_create_network(){
471 curl -i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "name=$1" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$2"
472 }
473
474 sensonet_update_network(){
475 curl -i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "name=$1" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$2"
476 }
477
478 sensonet_del_network(){
479 curl -i -X "DELETE" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$1"
480 }
481
482 sensonet_networks(){
483 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/ || \
484 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$1"
485 }
486
487 sensonet_network_probes() {
488 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/networks/"$1"/probes
489 }
490
491 sensonet_create_probe() {
492 curl -i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
493 -d "name=${1}&latitude=0.0&longitude=0.0&monitorMe=1&sleepingTime=0&networkId=univlab" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${2}"
494 }
495
496 sensonet_update_probe() {
497 curl -i -X "PUT" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" \
498 -d "name=${1}&latitude=48.0&longitude=-1.0&monitorMe=1&sleepingTime=0" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${2}"
499 }
500
501 sensonet_probes() {
502 [[ $# -eq 0 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/ || \
503 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"
504 }
505
506 sensonet_del_probe() {
507 curl -i -X "DELETE" -k -H -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${2}"
508 }
509
510 sensonet_probe_neighborhood(){
511 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/neighborhood
512 }
513
514 sensonet_probe_set_sleeping_time(){
515 curl -i -X "POST" -k -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "duration=${2}" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sleepingtime
516 }
517
518 sensonet_probe_sensors(){
519 [[ $# -eq 1 ]] && curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sensors || \
520 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sensors/"${2}"
521 }
522
523 sensonet_probe_sensor_values(){
524 curl -i -X "GET" -k -H "Accept: application/json" http://projects.emerginov.orange.sn/sensonet/resources/probes/%2B221"${1}"/sensors/"${2}"/values
525 }