1 #!/bin/bash
  2 #
  3 # /home/lantw44/.bash_include
  4 #
  5 # -- UTF-8 --
  6 #
  7 
  8 #### if tty -s; then interactive_shell=1; fi
  9 if [ "`echo "$-" | grep "i" `" ]; then interactive_shell=1; fi
 10 
 11 [ "$interactive_shell" ] && echo "Running .bash_include"
 12 [ "$interactive_shell" ] && default_tty_setting="`stty -g`"
 13 
 14 # Internal Variables
 15 
 16 colorprompting='\[\e[1;31m\]\!\[\e[m\] [\[\e[1;33m\]\u\[\e[m\]@\[\e[1;32m\]\h\[\e[m\] \[\e[1;36m\]\w\[\e[m\]]\$ '
 17 nocolorprompting='\! [\u@\h \w]\$ '
 18 
 19 historycountfile="$HOME/.bash_history.count"
 20 historybackupfile="$HOME/.bash_history.bak"
 21 bgrunfiledir="$HOME/tmp/bgrun-`whoami`"
 22 trashdir="$HOME/trash"
 23 
 24 HISTSIZE=1000000
 25 HISTFILESIZE=1000000
 26 HISTCONTROL=ignoredups:ignorespace
 27 
 28 REALPATH_PROGRAM="realpath"
 29 
 30 CFLAGS="-Wall -pipe -g"
 31 
 32 
 33 # Environment Variables
 34 
 35 export EDITOR=vim
 36 export FCEDIT=vim
 37 export PAGER=less
 38 
 39 
 40 
 41 
 42 # Aliases
 43 
 44 alias startcolor='PS1=$colorprompting'
 45 alias stopcolor='PS1=$nocolorprompting'
 46 
 47 alias ll='ls -l'
 48 alias grep='grep --color=always'
 49 alias rm='rm -i'
 50 alias cp='cp -pi'
 51 alias mv='mv -i'
 52 alias jobs='jobs -l'
 53 alias less='less -RS'
 54 
 55 alias cccc='LANG=C;LC_ALL=C'
 56 alias enus='LANG=en_US.UTF-8;LC_ALL=en_US.UTF-8'
 57 alias big5='LANG=zh_TW.Big5;LC_ALL=zh_TW.Big5'
 58 alias zhtw='LANG=zh_TW.UTF-8;LC_ALL=zh_TW.UTF-8'
 59 alias utf8='LANG=zh_TW.UTF-8;LC_ALL=zh_TW.UTF-8'
 60 
 61 alias savetty='default_tty_setting=`stty -g`'
 62 alias resetty='stty $default_tty_setting'
 63 
 64 
 65 
 66 
 67 # Functions
 68 
 69 function compile_all ()
 70 {
 71     local noask=0
 72     [ "$1" = '' ] && echo "Which file(s) do you want to compile? " && return 1
 73     [ "$1" = "-n" ] && noask=1
 74     if [ "$noask" = "0" ]; then
 75         read -e -p "CFLAGS: " -i "$CFLAGS" NEWCFLAGS
 76         read -e -p "LDFLAGS: " -i "$LDFLAGS" NEWLDFLAGS
 77         [ "$NEWCFLAGS" '!=' '' ] && CFLAGS=$NEWCFLAGS
 78         [ "$NEWLDFLAGS" '!=' '' ] && LDFLAGS=$NEWLDFLAGS
 79     else
 80         shift
 81     fi
 82     while [ "$1" '!=' '' ]
 83     do
 84         local TARGETFILE="`echo "$1" | cut -d . -f 1`"
 85         local SUFFIX="`echo "$1" | cut -d . -f 2`"
 86         if [ -f "$1" ]; then
 87             true
 88         else
 89             printf\
 90             "\e[1;33mWarning\e[0m: $1 Non-existent file or not a regular file\n"
 91             shift ; continue
 92         fi
 93         [ "$TARGETFILE" = "$1" ] && shift && continue
 94         if [ "$SUFFIX" = "c" ]; then
 95             echo "[CC] $1 -> $TARGETFILE"
 96             gcc $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
 97         elif [ "$SUFFIX" = "cpp" ]; then
 98             echo "[CXX] $1 -> $TARGETFILE"
 99             g++ $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
100         else
101             printf "$1: Unknown suffix (\e[1;33mskipped\e[0m)\n"
102         fi
103         [ "$?" '!=' "0" ] && printf\
104             '\e[1;31mError\e[0m while compiling file\n'
105         shift
106     done
107     return 0
108 }
109 
110 
111 function convert_to_html ()
112 {
113     while [ "$1" '!=' '' ]
114     do
115         for i in "$1"
116         do
117             vim $i -c 'set background=dark' \
118                 -c 'highlight PreProc ctermfg=darkcyan' \
119                 -c "$BEFORE_CONVERT_TO_HTML" \
120                 -c "$BEFORE_CONVERT_TO_HTML1" \
121                 -c "$BEFORE_CONVERT_TO_HTML2" \
122                 -c TOhtml \
123                 -c :w \
124                 -c :qa
125         done
126         shift
127     done
128 }
129 
130 function mkscreenacl ()
131 {
132     PERMIT_COMMAND="select windowlist other meta detach reset hardcopy info redisplay lastmsg next prev xon xoff windows suspend help colon copy paste writebuf readbuf displays stuff attach"
133     while [ "$1" '!=' '' ]
134     do
135         for i in $PERMIT_COMMAND
136         do
137             echo "aclchg $1 +x $i"
138         done
139         echo "aclchg $1 -rw \"#?\"" 
140         shift
141     done
142 }
143 
144 ########## Background ##########
145 
146 alias bgr=bgrun
147 alias bgv=bgview
148 alias bgl=bglist
149 alias bgc=bgcount
150 alias bgls=bglist
151 alias bgrm=bgclean
152 
153 function bgrun ()
154 {
155     [ "$#" = "0" ] && return 1
156     [ '!' -d "$bgrunfiledir" ] && mkdir -p "$bgrunfiledir"
157 #   [ '!' -d "$bgrunfiledir/pid" ] && mkdir -p "$bgrunfiledir/pid"
158     local current_time=`date "+%Y%m%d-%H%M%S"`
159     local cmdname=`echo "$1" | sed -e 's/-/_/g' -e 's/\\//_/g' -e 's/ /_/g'`
160     if [ "`echo "$cmdname" | cut -c 1`" == "_" ]
161     then
162         cmdname=`echo "$cmdname" | cut -c 2-`
163     fi
164     local filename="$bgrunfiledir/$current_time-$cmdname"
165     echo "Writing to $filename"
166     {
167         echo -n "$BASHPID " > "$filename"
168         echo "$@" >> "$filename"
169         exec "$@" &>> "$filename"
170     } &
171 }
172 
173 function bglist ()
174 {
175     local viewtime=0
176     [ "$1" = "--full" ] && viewtime=1
177     {
178         for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort`
179         do
180             [ "$viewtime" = "1" ] && echo "$i"
181             head -n 1 "$i" | {
182                 local procpid
183                 local cmdline
184                 read -d ' ' procpid
185                 read cmdline
186                 printf "(%5d) %s\n" "$procpid" "$cmdline"
187             }
188         done
189     } | {
190         if [ "$viewtime" = "1" ]
191         then
192             echo " INDEX         TIME          PID   COMMAND"
193             local readstat=0
194             local -i i=1
195             while true
196             do
197                 local dateandtime_long
198                 local cmdline
199                 read dateandtime_long
200                 read cmdline
201                 [ "$?" '!=' "0" ] && break
202                 local dateandtime=`basename "$dateandtime_long"`
203     ####        local part_year=`echo $dateandtime | cut -c 1-4`
204     ####        local part_month=`echo $dateandtime | cut -c 5-6`
205     ####        local part_date=`echo $dateandtime | cut -c 7-8`
206     ####        local part_hour=`echo $dateandtime | cut -c 10-11`
207     ####        local part_minute=`echo $dateandtime | cut -c 12-13`
208     ####        local part_second=`echo $dateandtime | cut -c 14-15`
209                 echo "$dateandtime" | {
210                     read -n 4 part_year
211                     read -n 2 part_month
212                     read -n 2 part_date
213                     read -n 1 drop_this_char; unset drop_this_char
214                     read -n 2 part_hour
215                     read -n 2 part_minute
216                     read -n 2 part_second
217                     printf '%6d' "$i"
218                     echo " $part_year-$part_month-$part_date $part_hour:$part_minute:$part_second $cmdline"
219                 }
220                 i=$i+1
221             done
222         else
223             echo " INDEX    PID   COMMAND"
224             cat -n
225         fi
226     } | $PAGER
227 }
228 
229 function bgview ()
230 {
231     local -i yourchoice
232     if [ "$1" = "" ]
233     then
234         yourchoice=`bgcount`
235     else
236         if [ "$1" -le "0" ]
237         then
238             yourchoice=$((`bgcount`+$1))
239         else
240             yourchoice=$1
241         fi
242     fi
243     echo "Your choice is $yourchoice."
244     local realfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${yourchoice}p`
245     head -n 1 "$realfilename" | {
246         read -d ' ' procpid
247         read cmdline
248         echo "PID: $procpid"
249         echo "Command Line: $cmdline"
250     }
251     read -e -p "View '$realfilename' ? " confirm
252     if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]
253     then
254         return 1
255     fi
256     {
257         printf "===> Process Information: "
258         cat "$realfilename"
259     } | $PAGER
260 }
261 
262 function bgcount ()
263 {
264     find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | wc | awk '{print $2}'
265 }
266 
267 function bgclean ()
268 {
269     if [ "$1" = "all" ]
270     then
271         echo "Removing the directory $bgrunfiledir"
272         rm -rf "$bgrunfiledir" &
273         return 0
274     else
275         split_arguments "$@"
276         local -i i=0
277         while [ "${arglist[$i]}" ]
278         do
279             arglist[$i]="-e ${arglist[$i]}p"
280             i=$i+1
281         done
282         local oneline
283         find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${arglist[*]} | {
284             while read oneline
285             do
286                 echo "Removing $oneline"
287                 rm -f "$oneline"
288             done
289         }
290     fi
291     unset arglist
292     unset prefixlist
293 }
294 
295 function bgdu () 
296 {
297     du -a "$bgrunfiledir"
298 }
299 
300 ########## Background End ##########
301 
302 function check_dmesg ()
303 {
304     [ "$#" = "0" ] && return 1
305 
306     while true
307     do
308         PREVIOS_DMESG_BUF="$DMESG_BUF"
309         DMESG_BUF="`dmesg`"
310         [ "$PREVIOS_DMESG_BUF" '!=' "$DMESG_BUF" ] && [ "$FIRST_RUN" = "0" ] && echo '===> You should check the system message buffer <==='
311         sleep $1
312         [ "$?" '!=' "0" ] && return 1
313         FIRST_RUN=0
314     done
315 }
316 
317 function prehistory_backup ()
318 {
319     echo "Checking your current history file"
320     local currentcount=`wc "$HISTFILE" | awk '{print $1}'`
321     [ '!' -f "$historycountfile" ] && touch "$historycountfile"
322     local -i previoushistorycount
323     previoushistorycount=`cat "$historycountfile"`
324     if [ "$currentcount" -lt "$previoushistorycount" ]
325     then
326         printf "\e[1;31mWarning\e[m: Your $HISTFILE may be TRUNCATED OR OVERWRITTEN BY OTHER PROGRAMS!\n"
327         printf "Note: \e[1;33m$currentcount\e[m < $previoushistorycount\n"
328         echo "Your $historycountfile and $historybackupfile will not be overwritten until this problem is fixed."
329         echo " 1. Check your $HISTFILE."
330         echo " 2. Edit your $HISTFILE manually if some unexpected changes are found." 
331         echo "    (You may need $historybackupfile to do it) "
332         echo " 3. Remove the file $historycountfile."
333         echo " 4. Run the command \`prehistory_backup' again."
334         return 3
335     fi
336     echo -n "Backing up your current history file ($previoushistorycount -> $currentcount, "
337     if [ "$previoushistorycount" = "$currentcount" ]
338     then
339         echo "no modification)"
340     else
341         echo "+$(($currentcount-$previoushistorycount)))"
342     fi
343     echo "$currentcount" > "$historycountfile"
344     \cp -f "$HISTFILE" "$historybackupfile"
345 }
346 
347 ########## Trash Manager ##########
348 
349 alias trash_put=trash_mv
350 alias trash_add=trash_mv
351 alias trash_list=trash_ls
352 alias trash_ct=trash_count
353 alias trash_restore=trash_recover
354 alias trash_rc=trash_recover
355 alias trash_drop=trash_rm
356 alias trash_clean=trash_rm
357 
358 function trash_mv ()
359 {
360     [ "$#" = "0" ] && return 1
361     [ '!' -d "$trashdir" ] && mkdir -p "$trashdir"
362     local original_path
363     local current_time
364     local -i i=0
365     split_arguments "$@"
366     while [ "${arglist[$i]}" ]
367     do
368         original_path="`$REALPATH_PROGRAM "${arglist[$i]}"`"
369         current_time=`date "+%Y%m%d-%H%M%S"`
370         better_time=`date "+%Y-%m-%d %H:%M:%S"`
371         dirname="`basename "${arglist[$i]}" | sed -e 's/-/_/g' -e 's/ /_/g'`"
372         fulldirname="$trashdir/$current_time-$dirname"
373         mkdir -p "$fulldirname"
374         echo "Move: ${arglist[$i]} -> $fulldirname"
375         "${prefixlist[@]}" mv "${arglist[$i]}" "$fulldirname"
376         if [ "$?" = "0" ]
377         then
378             echo "$better_time" > "$fulldirname/information.date"
379             echo "$original_path" > "$fulldirname/information.path"
380         else
381             rmdir "$fulldirname"
382         fi
383         i=$i+1
384         shift
385     done
386     unset arglist
387     unset prefixlist
388 }
389 
390 function trash_rm ()
391 {
392     split_arguments "$@"
393     local -i i=0
394     while [ "${arglist[$i]}" ]
395     do
396         arglist[$i]="-e ${arglist[$i]}p"
397         i=$i+1
398     done
399     trash_dirname=`find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} `
400     echo 'Type rm -rf $trash_dirname to remove them.'
401     unset arglist
402     unset prefixlist
403 }
404 
405 function trash_ls ()
406 {
407     local -i i=1
408     local oneline
409     find "$trashdir" -mindepth 1 -maxdepth 1 | sort | {
410         while read oneline
411         do
412             printf "%6d %s %s\n" "$i" \
413                 "`cat "$oneline/information.date"`" \
414                 "`cat "$oneline/information.path"`"
415             i=$i+1
416         done
417     } | $PAGER
418 }
419 
420 function trash_pushd ()
421 {
422     [ -z "$1" ] && return 1
423     pushd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
424 }
425 
426 function trash_cd ()
427 {
428     [ -z "$1" ] && return 1
429     cd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
430 }
431 
432 function trash_recover ()
433 {
434     [ -z "$1" ] && return 1
435     split_arguments "$@"
436     local -i i=0
437     while [ "${arglist[$i]}" ]
438     do
439         arglist[$i]="-e ${arglist[$i]}p"
440         i=$i+1
441     done
442     find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} | {
443         while read oneline
444         do
445             local fromfile="$oneline/`basename "$(cat "$oneline/information.path")"`"
446             local tofile="`dirname "$(cat "$oneline/information.path")"`"
447             if [ -e "`cat "$oneline/information.path"`" ]
448             then
449                 echo "Destination file exists."
450                 continue
451             fi
452             echo "Move: $fromfile -> $tofile"
453             "${prefixlist[@]}" mv -f "$fromfile" "$tofile"
454             if [ "$?" = "0" ]
455             then
456                 echo "Remove: $oneline"
457                 \rm -rf "$oneline"
458             fi
459         done
460     }
461     unset arglist
462     unset prefixlist
463 }
464 
465 function trash_count () 
466 {
467     find "$trashdir" -mindepth 1 -maxdepth 1 | wc | awk '{print $2}'
468 }
469 
470 function trash_du ()
471 {
472     split_arguments "$@"
473     local oneline
474     find "$trashdir" -maxdepth 1 -mindepth 1 | sort | {
475         while read oneline
476         do
477             echo "'$oneline'"
478         done
479     } | xargs -n 10 "${prefixlist[@]}" du -s
480     unset arglist
481     unset prefixlist
482 }
483 
484 ########## Trash Manager End ##########
485 
486 function split_arguments ()
487 {
488     local argcount=$#
489     local -i i=0
490     local prefix_start=0
491     while [ "$1" ]
492     do
493         if [ "$prefix_start" == "0" ]
494         then
495             if [ "$1" = "--" ]
496             then
497                 prefix_start=1
498                 i=0
499                 shift
500                 continue
501             else
502                 arglist[$i]="$1"
503             fi
504         else
505             prefixlist[$i]="$1"
506         fi
507         i=$i+1
508         shift
509     done
510 }
511 
512 function check_important_files ()
513 {
514     IMPORTANT_FILES="$HOME/.screenrc $HOME/.vimrc"
515     for i in $IMPORTANT_FILES
516     do
517         [ '!' -f "$i" ] && printf "\e[1;31mWarning\e[m: \e[1;33m$i\e[m does not exist.\n"
518     done
519 }
520 
521 ########## PATH Editor ##########
522 
523 function split_path_core ()
524 {
525     echo "$current_path" | {
526         while read -d : oneline
527         do
528             [ '!' "$oneline" = '^' ] && echo "$oneline"
529         done
530         [ '!' "$oneline" = '^' ] && echo "$oneline"
531     }
532     unset oneline
533 }
534 
535 function split_path () 
536 {
537     coproc split_path_core
538     readarray -t -u ${COPROC[0]} patharr
539     wait $COPROC_PID
540 }
541 
542 function update_path ()
543 {
544     current_path=''
545     local -i i=0
546     local firsttime="yes"
547     while [ "${patharr[$i]}" ]
548     do
549         if [ '!' "${patharr[$i]}" = "^" ]
550         then
551             if [ "$firsttime" ]
552             then
553                 firsttime=''
554             else
555                 current_path+=':'
556             fi
557             current_path+="${patharr[$i]}"
558         fi
559         i=$i+1
560     done
561 }
562 
563 function path_editor ()
564 {
565     path_editor_core
566 }
567 
568 function ldpath_editor ()
569 {
570     path_editor_core ld
571 }
572 
573 function path_editor_core ()
574 {
575     if [ "$1" = "ld" ]
576     then
577         export current_path="$LD_LIBRARY_PATH"
578     else
579         export current_path="$PATH"
580     fi
581     local should_continue="yes"
582     local command
583     local command_sub
584     local command_sub2
585     local -i i
586     while [ "$should_continue" ]
587     do
588         split_path
589         i=0
590         echo "========================================"
591         while [ "${patharr[$i]}" ]
592         do
593             echo "$i: ${patharr[$i]}"
594             i=$i+1
595         done
596         [ "$i" = '0' ] && echo "(Empty or not declared)"
597         echo "========================================"
598         read -e -p "[A]ppend/(D)elete/(E)dit/(M)ove/(R)eset/(Q)uit ? " command
599         case "$command" in
600             ''|A|a)
601                 read -e -p "Type a new entry: " patharr[$i]
602                 update_path
603                 ;;
604             D|d)
605                 read -e -p "Index: " command_sub
606                 patharr[$command_sub]='^'
607                 update_path
608                 ;;
609             E|e)
610                 read -e -p "Index: " command_sub
611                 read -e -p "Modify this entry: " -i "${patharr[$command_sub]}" patharr[$command_sub]
612                 update_path
613                 ;;
614             M|m)
615                 read -e -p "From: " command_sub
616                 read -e -p "To: " command_sub2
617                 swaptmp="${patharr[$command_sub]}"
618                 patharr[$command_sub]="${patharr[$command_sub2]}"
619                 patharr[$command_sub2]="$swaptmp"
620                 unset swaptmp
621                 update_path
622                 ;;
623             R|r)
624                 if [ "$1" = "ld" ]
625                 then
626                     current_path="$LD_LIBRARY_PATH"
627                 else
628                     current_path="$PATH"
629                 fi
630                 ;;
631             Q|q)
632                 if [ "$1" = "ld" ]
633                 then
634                     export LD_LIBRARY_PATH="$current_path"
635                     echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
636                     history -s "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
637                 else
638                     export PATH="$current_path"
639                     echo "PATH=$PATH"
640                     history -s "PATH=$PATH"
641                 fi
642                 should_continue=''
643                 ;;
644             *)
645                 printf " \e[33m*** Unknown command ***\e[m \n"
646                 ;;
647         esac
648     done
649     unset patharr
650     unset current_path
651 }
652 
653 ########## PATH Editor End ##########
654 
655 function backup_file ()
656 {
657     split_arguments "$@"
658     local current_time=`date +%Y%m%d`
659     local rootfilename
660     local -i i=0
661     local -i j
662     while [ "${arglist[$i]}" ]
663     do
664         if [ '!' -f "${arglist[$i]}" ]
665         then
666             printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
667             i=$i+1
668             continue
669         fi
670         rootfilename="${arglist[$i]}.$current_time"
671         if [ -e "$rootfilename" ]
672         then
673             j=0
674             while [ "$j" -lt "10" ]
675             do
676                 if [ -e "$rootfilename.$j" ]
677                 then
678                     j=$j+1
679                     continue
680                 else
681                     "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
682                     history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
683                     "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
684                     history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
685                     break
686                 fi
687             done
688             if [ '!' "$j" -lt "10" ]
689             then
690                 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
691                 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
692             fi
693         else
694             "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
695             history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
696             "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
697             history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
698         fi
699         i=$i+1
700     done
701     unset arglist
702     unset prefixlist
703 }
704 
705 function keep_sudo_credential ()
706 {
707     if [ "$1" ]
708     then
709         update_sudo_interval="$1"
710     else
711         update_sudo_interval="280"
712     fi
713     while true
714     do
715         sudo -v
716         sleep "$update_sudo_interval"
717     done
718 }
719 
720 function get_memory_info ()
721 {
722     if [ "`uname`" = "Linux" ]
723     then
724         local meminfoline="`free -m | \grep -i mem`"
725         local swapinfoline="`free -m | \grep -i swap`"
726         local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
727         local memused="`echo "$meminfoline" | awk '{print $3}'`"
728         local membuf="`echo "$meminfoline" | awk '{print $6}'`"
729         local memcache="`echo "$meminfoline" | awk '{print $7}'`"
730         local memprog=$(($memused-$membuf-$memcache))
731         local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
732         local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
733         echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
734         echo "Detail:"
735         echo "    Used:    `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
736         echo "    Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
737         echo "    Cached:  `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
738         if [ "$swaptotal" = "0" ]
739         then
740             echo "Swap: not available"
741         else
742             echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
743         fi
744     else
745         echo "Current operating system is not Linux."
746     fi
747 }
748 
749 function set_console_title ()
750 {
751     case "$TERM" in
752         screen)
753             printf "\033]0;"
754             echo -n "$*"
755             printf "\033\\"
756             ;;
757         xterm*)
758             printf "\033]0;"
759             echo -n "$*"
760             printf "\007"
761             ;;
762         *)
763             echo "Your terminal may not have the hardstatus line."
764             echo "Note: TERM=$TERM"
765             ;;
766     esac
767 }
768 
769 function mvfile ()
770 {
771     split_arguments "$@"
772     local -i i=0
773     while [ "${arglist[$i]}" ] 
774     do
775         if [ '!' -e "${arglist[$i]}" ]
776         then
777             printf "\e[33mWarning\e[m: ${arglist[$i]} does not exist.\n"
778             i=$i+1
779             continue
780         fi
781         echo "Old name: ${arglist[$i]}"
782         read -p "New name: " -e -i "${arglist[$i]}" new_file_name
783         if [ "$new_file_name" ] && [ "${arglist[$i]}" != "$new_file_name" ]
784         then
785             "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
786             history -s "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
787         fi
788         i=$i+1
789     done
790     unset arglist
791     unset prefixlist
792     unset new_file_name
793 }
794 
795 ########## Help ##########
796 
797 alias helpf='help_function'
798 alias helpm='help_myself'
799 
800 function print_iconv ()
801 {
802     [ "$1" = "$2" ] && cat && return 0
803     iconv -f "$1" -t "$2"
804 }
805 
806 function help_myself () 
807 {
808     echo "argc = $#"
809     echo "argv[0] = $0"
810     i=1
811     while [ "$1" ]
812     do
813         echo "argv[$i] = $1"
814         i=$(($i+1))
815         shift
816     done
817 }
818 
819 function help_function ()
820 {
821     [ "$#" = "0" ] && {
822         cat << ENDHELPMSG
823  <<< Help >>>
824     help_myself [arguments ...]                            (helpm)
825     help_function [functions ...]                          (helpf)
826  <<< Group: Background >>>
827     bgrun command [arguments ...]                          (bgr)
828     bglist [--full]                                        (bgl, bgls)
829     bgview [number]                                        (bgv)
830     bgclean [all | numbers ...]                            (bgrm)
831     bgcount                                                (bgc)
832     bgdu
833  <<< Group: Trash >>>
834     trash_mv [filename ...] [-- sudo_prefix ...]      (trash_put, trash_add)
835     trash_ls                                          (trash_list)
836     trash_cd [number]
837     trash_pushd [number]
838     trash_recover [number] [-- sudo_prefix ...]       (trash_restore, trash_rc)
839     trash_rm numbers ...                              (trash_drop, trash_clean)
840     trash_count                                       (trash_ct)
841     trash_du [-- sudo_prefix ...]
842  <<< Group: PATH Editor >>>
843     path_editor
844     ldpath_editor
845  x  split_path
846  x  split_path_core
847  x  update_path 
848  x  path_editor_core
849  <<< Other >>>
850     backup_file filename ... [-- sudo_prefix ]
851     check_dmesg seconds
852     check_important_files
853     compile_all [-n] filename ...
854     convert_to_html filename ...
855     keep_sudo_credential [seconds]
856     mkscreenacl username ...
857     mvfile filename ... [-- sudo_prefix]
858     prehistory_backup
859     set_console_title
860  x  split_arguments [arguments ...]
861 ENDHELPMSG
862     } && return 0
863     local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
864     local -i i
865     while [ "$1" ]
866     do
867         case "$1" in
868             help_myself|helpm)
869                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
870 help_myself
871     一個測試命令列的小函式
872 ENDHELPMSG
873                 ;;
874             help_function|helpf)
875                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
876 help_function
877     顯示 .bash_include 提供的額外函式清單
878     註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
879 ENDHELPMSG
880                 ;;
881             bgrun|bgr)
882                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
883 bgrun command [arguments ...]
884     執行指令 command 並將輸出導入檔案
885     註:此函式會自動以目前時間和指令名稱為檔案命名
886 ENDHELPMSG
887                 ;;
888             bglist|bgl|bgls)
889                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
890 bglist [--full]
891     列出所有使用 bgrun 執行的指令
892     若加上 --full 選項,則可同時察看時間
893 ENDHELPMSG
894                 ;;
895             bgview|bgv)
896                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
897 bgview [number]
898     顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
899     若 number >  0,
900         表示第 number 個指令 (此數值可由 bglist 函式取得)
901     若 number <= 0,
902         表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
903 ENDHELPMSG
904                 ;;
905             bgclean|bgrm)
906                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
907 bgclean [all | numbers ...]
908     bgclean all 可清除所有指令的輸出檔
909     bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
910 ENDHELPMSG
911                 ;;
912             bgcount|bgc)
913                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
914 bgcount
915     顯示指令輸出檔總數
916 ENDHELPMSG
917                 ;;
918             bgdu)
919                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
920 bgdu
921     顯示每個指令輸出檔的檔案大小 (單位:KB)
922 ENDHELPMSG
923                 ;;
924             *)
925                 echo "Help message for $1 is not found"
926                 ;;
927         esac
928         shift
929     done
930 }
931 
932 ########## Help End ##########
933 
934 # Doing something
935 
936 umask 0022
937 
938 if [ "$interactive_shell" ]
939 then
940     echo "Running interactive shell configuration"
941     check_important_files
942     startcolor
943     prehistory_backup
944     bind '"\e[A":history-search-backward'
945     bind '"\e[B":history-search-forward'
946     if [ -z "$PROMPT_COMMAND" ] && [ -e "$HOME/.bash_title" ]; then
947         case "$TERM" in
948             xterm*)
949                 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
950                 ;;
951             screen)
952                 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
953                 ;;
954         esac
955     fi
956 fi
957 
958 if [ "`uname`" = "Linux" ]
959 then
960     [ "$interactive_shell" ] && echo "Setting special things for Linux"
961     REALPATH_PROGRAM="readlink -f"
962     ulimit -c unlimited
963 fi
964 
965 [ "$interactive_shell" ] && echo "Setting shell options"
966 
967 shopt -s histappend
968 shopt -s checkwinsize
969 shopt -s checkjobs
970 shopt -s checkhash
971 shopt -s cmdhist
972 shopt -s mailwarn
973 
974 [ "$interactive_shell" ] && {
975     echo "Done"
976     if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
977     then
978         printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
979     fi
980 }
981