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