2 # my weird function file
3 # lots of this coming from http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming
5 my_copy_to_clipboard
() {
7 [ -f $file ] && xclip
-sel clip
< $filecontent
13 usage: $PROGNAME options
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
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
25 -h --help show this help
26 --help-config configuration help
34 $PROGNAME --test test_string.sh
37 $PROGNAME --config /path/to/config/$PROGNAME.conf
39 Just show what you are going to do:
40 $PROGNAME -vn -c /path/to/config/$PROGNAME.conf
45 my_change_owner_of_files
() {
58 my_template_function
() {
60 local PROGNAME
=$
(basename $0)
61 local PROGDIR
=$
(readlink
-m $
(dirname $0))
66 #readonly local second_arg=$2;
68 # if no args, print usage
69 is_empty_string
$ARGS && my_usage
76 # grep -v: inverse match
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"
108 # Taken from http://aaroncrane.co.uk/2009/03/git_branch_prompt/
111 until [ "$dir" -ef / ]; do
112 if [ -f "$dir/.git/HEAD" ]; then
113 GIT_REPO
=`readlink -e $dir`/
123 head=$
(< "$1/.git/HEAD")
124 if [[ $head == ref
:\ refs
/heads
/* ]]; then
125 GIT_BRANCH
=${head#*/*/}
126 elif [[ $head != '' ]]; then
127 GIT_BRANCH
='(detached)'
129 GIT_BRANCH
='(unknown)'
133 # Taken from https://github.com/jimeh/git-aware-prompt
135 local status
=$
(git status
--porcelain 2> /dev
/null
)
136 if [[ "$status" != "" ]]; then
144 if [ -e "$1/.git/refs/stash" ]; then
151 cdls
() { cd "$@" && clear && ls; }
167 mkdir
-p "$@" && eval cd "\"\$$#\"";
171 local serverdir
="${TMPDIR:-/tmp}/emacs${UID}"
173 for file in ${serverdir}/*; do
174 if [[ -S ${file} ]]; then
175 servers
+=("${file##*/}")
199 printf '%s ' "$result";
206 echo "[$(date + '%Y-%m-%dT%H:%M:%S%z')]: $@ >&2"
215 my_err
"unable to my_test"
216 exit "${E_DID_NOTHING}"
220 my_pretty_print_json
() {
224 my_not_start_service_on_boot
(){
225 # service to not start on boot
227 update-rc.d
$service disable
;
230 my_install_new_font
(){
237 sudo ip link
set eth0 up
;
238 # ask for an ip via dhcp
242 #function to show the current branch
243 my_show_current_branch
(){
244 git rev-parse
--abbrev-ref HEAD
250 echo $
(pdftk
$filename dump_data \
251 |
grep -i numberofpages \
257 echo $
(pdftk
$filename burst
);
261 for i
in $
(ls |
grep .pdf$
); do
266 # function to show one line of file
268 local line_number
=$1;
272 echo $
(sed -n $line_number$p $filename);
275 # function to extract in one command
279 if is_file
$filename ; then
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" ;;
297 echo "'$filename' is not a valid file"
301 # function to list the content of an archive
302 my_list_archive_content
() {
305 if is_file
$filename ; then
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 :(" ;;
321 echo "'$filename' is not a valid file"
325 # dump a mysql database
329 echo "dumping database '$database' ..."
330 mysqldump
-u patrick
-ppatrick $database > $databse_dump_$
(date +%m
%d
%Y
).sql
334 my_dump_mysql_table
() {
338 echo "dumping table '$table' from database '$database' ..."
339 mysqldump
-u patrick
-ppatrick $database $table > $table_dump_$
(date +%m
%d
%Y
).sql
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
354 local string_to_find
=$1;
357 echo "recursive looking for the string '$string' inside '$directory' directory"
358 grep -l -R -i -r $string_to_find $directory;
362 local string_to_find
=$1;
365 echo "recursive looking for the string '$string' inside '$directory' directory"
366 find $directory |
xargs grep $string -sl;
369 # extract md5 hash from a string
373 echo "md5 hash of '$string' is: "
374 echo -n "$string" |
md5sum;
377 # Creates an archive (*.tar.gz) from given directory.
381 tar cvzf
"${directory%%/}.tar.gz" "${directory%%/}/";
384 # Create a ZIP archive of a file or folder.
388 zip -r "${file%%/}.zip" "$file";
391 # Make your directories and files access rights sane.
392 my_sanitize
() { chmod -R u
=rwX
,g
=rX
,o
= "$@" ;}
394 # Get IP adress on ethernet.
396 local my_ip
=$
( /sbin
/ifconfig eth0 \
397 |
awk '/inet/ { print $2 } ' \
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"}
404 # Get current host related 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 " ; \
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;
422 # Pretty-print of 'df' output. Inspired by 'dfc' utility.
430 echo -e $fs" :No such file or directory" ; continue
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]} ))
437 for ((j
=0;j
<20;j
++)); do
438 if [ ${j} -lt ${nbstars} ]; then
444 out
=${info[2]}" "$out"] ("$free" free on "$fs")"
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
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;
466 #echo $(date +%m%d%Y)" -- $@" >> $rememberfile
467 echo $
(date +%F
)" -- $@" >> $rememberfile
472 local rememberfile
="$HOME/.remember"
474 #is_not_file $remberfile && echo 'nothing to do' ||
475 #is_empty $rememberfile && echo 'nothing to do' ||
477 is_not_file
$rememberfile ||
[[ $
(cat $rememberfile |
wc -l) = 0 ]] && echo "nothing to do" || my_display_todo
"$@";
482 local rememberfile
="$HOME/.remember"
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;
490 #lsbytesum - sum the number of bytes in a directory listing
492 for Bytes
in $
(ls -l |
grep "^-" |
awk '{ print $5 }')
494 let TotalBytes
=$TotalBytes+$Bytes
496 TotalMeg
=$
(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" |
bc)
503 declare -a $listname;
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"
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"
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"
521 sensonet_del_event
() {
522 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/events
/"$1"
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"
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"
533 sensonet_del_sensor
() {
534 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/sensors
/"$1"
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"
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"
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"
550 sensonet_del_network
(){
551 curl
-i -X "DELETE" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"
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"
559 sensonet_network_probes
() {
560 curl
-i -X "GET" -k -H "Accept: application/json" http
://projects.emerginov.orange.sn
/sensonet
/resources
/networks
/"$1"/probes
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}"
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}"
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}"
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}"
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
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
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}"
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