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