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