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