added some more aliases
[dotfiles] / .bash_function
1 #!/bin/sh
2 # my weird function file
3
4 my_pretty_print_json() {
5 python -m json.tool
6 }
7
8 my_not_start_service_on_boot(){
9 # service to not start on boot
10 local service=$1;
11 update-rc.d $service disable;
12 }
13
14 my_install_new_font(){
15 #sudo fc-cache -v -f
16 fc-cache -v -f;
17 }
18
19 my_launch_eth0(){
20 # set eth0 iface up
21 sudo ip link set eth0 up;
22 # ask for an ip via dhcp
23 sudo dhclient eth0;
24 }
25
26 #function to show the current branch
27 my_show_current_branch(){
28 git rev-parse --abbrev-ref HEAD
29 }
30
31 my_pdf_nbpages(){
32 local filename=$1;
33
34 echo $(pdftk $filename dump_data \
35 | grep -i numberofpages \
36 | awk '{print $2}');
37 }
38
39 my_pdf_explosion() {
40 local filename=$1;
41 echo $(pdftk $filename burst);
42 }
43
44 my_pdfs_to_xml() {
45 for i in $(ls | grep .pdf$); do
46 pdftohtml -xml $i;
47 done;
48 }
49
50 # function to show one line of file
51 my_xline_of_file(){
52 local line_number=$1;
53 local filename=$2;
54 local p='p';
55
56 echo $(sed -n $line_number$p $filename);
57 }
58
59 # function to extract in one command
60 my_xtract() {
61 local filename=$1;
62
63 if is_file $filename ; then
64 case $filename in
65 *.tar.xz) tar xvfJ $filename ;;
66 *.tar.bz2) tar xvjf $filename ;;
67 *.tar.gz) tar xvzf $filename ;;
68 *.bz2) bunzip2 $filename ;;
69 *.rar) unrar x $filename ;;
70 *.gz) gunzip $filename ;;
71 *.tar) tar xvf $filename ;;
72 *.tbz2) tar xvjf $filename ;;
73 *.tgz) tar xvzf $filename ;;
74 *.zip) unzip $filename ;;
75 *.Z) uncompress $filename ;;
76 *.7z) 7z x $filename ;;
77 *) echo "'$filename' cannot be extracted via extract" ;;
78 esac
79 else
80 echo "'$filename' is not a valid file"
81 fi
82 }
83
84 # function to list the content of an archive
85 my_list_archive_content() {
86 local filename=$1;
87
88 if is_file $filename ; then
89 case $filename in
90 *.tar.xz) tar tvfJ $filename ;;
91 *.tar.bz2) tar tvjf $filename ;;
92 *.tar.gz) tar tzvf $filename ;;
93 *.bz2) bunzip2 $filename ;;
94 *.gz) gunzip -l $filename ;;
95 *.tar) tar tvf $filename ;;
96 *.tbz2) tar tvjf $filename ;;
97 *.tgz) tar tvzf $filename ;;
98 *.zip) unzip -l $filename ;;
99 *.Z) uncompress -l $filename ;;
100 *.7z) 7z l $filename ;;
101 *) echo "content of '$filename' cannot be listed :(" ;;
102 esac
103 else
104 echo "'$filename' is not a valid file"
105 fi
106 }
107
108 # dump a mysql database
109 my_dump_mysql_db() {
110 local database=$1;
111
112 echo "dumping database '$database' ..."
113 mysqldump -u patrick -ppatrick $database > $databse_dump_$(date +%m%d%Y).sql
114
115 }
116
117 my_dump_mysql_table() {
118 local database=$1;
119 local table=$2;
120
121 echo "dumping table '$table' from database '$database' ..."
122 mysqldump -u patrick -ppatrick $database $table > $table_dump_$(date +%m%d%Y).sql
123 }
124
125 my_uploadfile() {
126 local filename=$1
127 local ftp_url=$2;
128 local user=$3;
129 local password=$4;
130
131 echo "upload of '$filename' to '$ftp_url' by '$user' ..."
132 curl -T $filename $ftp_url --user $user:$password
133 }
134
135 my_find_string() {
136 local string_to_find=$1;
137 local directory=$2;
138
139 echo "recursive looking for the string '$string' inside '$directory' directory"
140 grep -l -R -i -r $string_to_find $directory;
141 }
142
143 my_find_string2() {
144 local string_to_find=$1;
145 local directory=$2;
146
147 echo "recursive looking for the string '$string' inside '$directory' directory"
148 find $directory | xargs grep $string -sl;
149
150 }
151 # extract md5 hash from a string
152 my_xmd5() {
153 local $string=$1;
154
155 echo "md5 hash of '$string' is: "
156 echo -n "$sring" | md5sum;
157 }
158
159 # Creates an archive (*.tar.gz) from given directory.
160 my_maketar() {
161 local $directory=$1;
162
163 tar cvzf "${directory%%/}.tar.gz" "${directory%%/}/";
164 }
165
166 # Create a ZIP archive of a file or folder.
167 my_makezip() {
168 local $file=$1;
169
170 zip -r "${file%%/}.zip" "$file";
171 }
172
173 # Make your directories and files access rights sane.
174 my_sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
175
176 # Get IP adress on ethernet.
177 my_ip() {
178 local my_ip=$( /sbin/ifconfig eth0 \
179 | awk '/inet/ { print $2 } ' \
180 | sed -e s/addr://);
181 local my_gtw="gtw: "$(netstat -nr | awk '{print $2}' | sed -n 3p);
182 echo ${my_ip:-"Not connected"}
183 echo ${my_gtw:-"Not connected"}
184 }
185
186 # Get current host related info.
187 my_host_info() {
188 echo -e "\nYou are logged on ${BRed}$HOST"
189 echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
190 echo -e "\n${BRed}Current date :$NC " ; date; calendar | head -1
191 echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
192 echo -e "\n${BRed}Users logged on:$NC " ; \
193 w -hs \
194 | cut -d " " -f1 \
195 | sort \
196 | uniq
197 echo -e "\n${BRed}Machine stats :$NC " ; uptime
198 echo -e "\n${BRed}Memory stats :$NC " ; free -h
199 echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
200 echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet;
201 echo
202 }
203
204 # Pretty-print of 'df' output. Inspired by 'dfc' utility.
205 my_df() {
206 local fs=$1;
207
208 for fs ; do
209
210 if [ ! -d $fs ]
211 then
212 echo -e $fs" :No such file or directory" ; continue
213 fi
214
215 local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
216 local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
217 local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
218 local out="["
219 for ((j=0;j<20;j++)); do
220 if [ ${j} -lt ${nbstars} ]; then
221 out=$out"*"
222 else
223 out=$out"-"
224 fi
225 done
226 out=${info[2]}" "$out"] ("$free" free on "$fs")"
227 echo -e $out
228 done
229 }
230
231 my_add_todo(){
232 local rememberfile="$HOME/.remember"
233 # if the file not exists, touch it
234 [[ ! -f $rememberfile ]] && touch $rememberfile
235 if [ $# -eq 0 ] ; then
236 echo "Type what you want to remember then, hit ^D: "
237 cat - >> $rememberfile;
238 else
239 #echo $(date +%m%d%Y)" -- $@" >> $rememberfile
240 echo $(date +%F)" -- $@" >> $rememberfile
241 fi
242 }
243
244 my_reminder(){
245 local rememberfile="$HOME/.remember"
246
247 #is_not_file $remberfile && echo 'nothing to do' ||
248 #is_empty $rememberfile && echo 'nothing to do' ||
249 #display_todo "$@"
250 is_not_file $rememberfile || [[ $(cat $rememberfile | wc -l) = 0 ]] && echo "nothing to do" || my_display_todo "$@";
251 }
252
253 my_display_todo(){
254
255 local rememberfile="$HOME/.remember"
256 echo $#;
257 #[[ $# -eq 0 ]] && more $rememberfile || grep -i "$@" $rememberfile | more;
258 [[ $# -eq 0 ]] && is_file $rememberfile && is_not_empty_file $rememberfile && cat $rememberfile || grep -i "$@" $rememberfile;
259 }
260
261
262 my_lsbytesum() {
263 #lsbytesum - sum the number of bytes in a directory listing
264 TotalBytes=0
265 for Bytes in $(ls -l | grep "^-" | awk '{ print $5 }')
266 do
267 let TotalBytes=$TotalBytes+$Bytes
268 done
269 TotalMeg=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc)
270 echo -n "$TotalMeg"
271 }
272
273 # list
274 my_create_list(){
275 local listname=$1;
276 declare -a $listname;
277 }