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