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