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 
334 
335 function trash_mv ()
336 {
337     [ "$#" = "0" ] && return 1
338     [ '!' -d "$trashdir" ] && mkdir -p "$trashdir"
339     local original_path
340     local current_time
341     local -i i=0
342     split_arguments "$@"
343     while [ "${arglist[$i]}" ]
344     do
345         original_path="`$REALPATH_PROGRAM "${arglist[$i]}"`"
346         current_time=`date "+%Y%m%d-%H%M%S"`
347         better_time=`date "+%Y-%m-%d %H:%M:%S"`
348         dirname="`basename "${arglist[$i]}" | sed -e 's/-/_/g' -e 's/ /_/g'`"
349         fulldirname="$trashdir/$current_time-$dirname"
350         mkdir -p "$fulldirname"
351         echo "Move: ${arglist[$i]} -> $fulldirname"
352         "${prefixlist[@]}" mv "${arglist[$i]}" "$fulldirname"
353         echo "$better_time" > "$fulldirname/information.date"
354         echo "$original_path" > "$fulldirname/information.path"
355         i=$i+1
356         shift
357     done
358     unset arglist
359     unset prefixlist
360 }
361 
362 function trash_rm ()
363 {
364     echo bb
365 }
366 
367 function trash_ls ()
368 {
369     echo cc
370 }
371 
372 function trash_cd ()
373 {
374     echo dd
375 }
376 
377 function trash_recover ()
378 {
379     echo yy
380 }
381 
382 function trash_count () 
383 {
384     echo zz
385 }
386 
387 function trash_du ()
388 {
389     split_arguments "$@"
390     local oneline
391     find "$trashdir" -maxdepth 1 -mindepth 1 | sort | {
392         while read oneline
393         do
394             echo "'$oneline'"
395         done
396     } | xargs -n 10 "${prefixlist[@]}" du -s
397     unset arglist
398     unset prefixlist
399 }
400 
401 ########## Trash Manager End ##########
402 
403 ########## Help ##########
404 
405 alias helpf='help_function'
406 alias helpm='help_myself'
407 
408 function print_iconv ()
409 {
410     [ "$1" = "$2" ] && cat && return 0
411     iconv -f "$1" -t "$2"
412 }
413 
414 function help_function ()
415 {
416     [ "$#" = "0" ] && {
417         cat << ENDHELPMSG
418  <<< Help >>>
419     help_myself [arguments ...]
420     help_function [functions ...]
421  <<< Group: Background >>>
422     bgrun command [arguments ...]
423     bglist [--full]
424     bgview [number]
425     bgclean [all | numbers ...]
426     bgcount
427     bgdu
428  <<< Group: Trash >>>
429     trash_mv [filename ...] [-- sudo_prefix ...]
430     trash_ls
431     trash_cd [number]
432     trash_recover [number] [-- sudo_prefix ...]
433     trash_rm [all | numbers ...] [-- sudo_prefix ...]
434     trash_count
435     trash_du [-- sudo_prefix ...]
436  <<< Group: PATH Editor >>>
437     path_editor
438     ldpath_editor
439  x  split_path
440  x  split_path_core
441  x  update_path 
442  x  path_editor_core
443  <<< Other >>>
444     compile_all [-n] filename ...
445     convert_to_html filename ...
446     mkscreenacl username ...
447     check_dmesg seconds
448     prehistory_backup
449     check_important_files
450     backup_file filename ... [-- sudo_prefix ]
451     keep_sudo_credential [seconds]
452  x  split_arguments [arguments ...]
453 ENDHELPMSG
454     } && return 0
455     local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
456     local -i i
457     while [ "$1" ]
458     do
459         case "$1" in
460             "help_myself")
461                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
462 help_myself
463     一個測試命令列的小函式
464 ENDHELPMSG
465                 ;;
466             "help_function")
467                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
468 help_function
469     顯示 .bash_include 提供的額外函式清單
470     註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
471 ENDHELPMSG
472                 ;;
473             "bgrun")
474                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
475 bgrun command [arguments ...]
476     執行指令 command 並將輸出導入檔案
477     註:此函式會自動以目前時間和指令名稱為檔案命名
478 ENDHELPMSG
479                 ;;
480             "bglist")
481                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
482 bglist [--full]
483     列出所有使用 bgrun 執行的指令
484     若加上 --full 選項,則可同時察看時間
485 ENDHELPMSG
486                 ;;
487             "bgview")
488                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
489 bgview [number]
490     顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
491     若 number >  0,
492         表示第 number 個指令 (此數值可由 bglist 函式取得)
493     若 number <= 0,
494         表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
495 ENDHELPMSG
496                 ;;
497             "bgclean")
498                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
499 bgclean [all | numbers ...]
500     bgclean all 可清除所有指令的輸出檔
501     bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
502 ENDHELPMSG
503                 ;;
504             "bgcount")
505                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
506 bgcount
507     顯示指令輸出檔總數
508 ENDHELPMSG
509                 ;;
510             "bgdu")
511                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
512 bgdu
513     顯示每個指令輸出檔的檔案大小 (單位:KB)
514 ENDHELPMSG
515                 ;;
516             *)
517                 echo "Help message for $1 is not found"
518                 ;;
519         esac
520         shift
521     done
522 }
523 
524 function help_myself () 
525 {
526     echo "argc = $#"
527     echo "argv[0] = $0"
528     i=1
529     while [ "$1" ]
530     do
531         echo "argv[$i] = $1"
532         i=$(($i+1))
533         shift
534     done
535 }
536 
537 ########## Help End ##########
538 
539 function split_arguments ()
540 {
541     local argcount=$#
542     local -i i=0
543     local prefix_start=0
544     while [ "$1" ]
545     do
546         if [ "$prefix_start" == "0" ]
547         then
548             if [ "$1" = "--" ]
549             then
550                 prefix_start=1
551                 i=0
552                 shift
553                 continue
554             else
555                 arglist[$i]="$1"
556             fi
557         else
558             prefixlist[$i]="$1"
559         fi
560         i=$i+1
561         shift
562     done
563 }
564 
565 function check_important_files ()
566 {
567     IMPORTANT_FILES="$HOME/.screenrc $HOME/.vimrc"
568     for i in $IMPORTANT_FILES
569     do
570         [ '!' -f "$i" ] && printf "\e[1;31mWarning\e[m: \e[1;33m$i\e[m does not exist.\n"
571     done
572 }
573 
574 ########## PATH Editor ##########
575 
576 function split_path_core ()
577 {
578     echo "$current_path" | {
579         while read -d : oneline
580         do
581             [ '!' "$oneline" = '^' ] && echo "$oneline"
582         done
583         [ '!' "$oneline" = '^' ] && echo "$oneline"
584     }
585     unset oneline
586 }
587 
588 function split_path () 
589 {
590     coproc split_path_core
591     readarray -t -u ${COPROC[0]} patharr
592     wait $COPROC_PID
593 }
594 
595 function update_path ()
596 {
597     current_path=''
598     local -i i=0
599     local firsttime="yes"
600     while [ "${patharr[$i]}" ]
601     do
602         if [ '!' "${patharr[$i]}" = "^" ]
603         then
604             if [ "$firsttime" ]
605             then
606                 firsttime=''
607             else
608                 current_path+=':'
609             fi
610             current_path+="${patharr[$i]}"
611         fi
612         i=$i+1
613     done
614 }
615 
616 function path_editor ()
617 {
618     path_editor_core
619 }
620 
621 function ldpath_editor ()
622 {
623     path_editor_core ld
624 }
625 
626 function path_editor_core ()
627 {
628     if [ "$1" = "ld" ]
629     then
630         export current_path="$LD_LIBRARY_PATH"
631     else
632         export current_path="$PATH"
633     fi
634     local should_continue="yes"
635     local command
636     local command_sub
637     local command_sub2
638     local -i i
639     while [ "$should_continue" ]
640     do
641         split_path
642         i=0
643         echo "========================================"
644         while [ "${patharr[$i]}" ]
645         do
646             echo "$i: ${patharr[$i]}"
647             i=$i+1
648         done
649         [ "$i" = '0' ] && echo "(Empty or not declared)"
650         echo "========================================"
651         read -e -p "[A]ppend/(D)elete/(E)dit/(M)ove/(R)eset/(Q)uit ? " command
652         case "$command" in
653             ''|A|a)
654                 read -e -p "Type new entry: " patharr[$i]
655                 update_path
656                 ;;
657             D|d)
658                 read -e -p "Index: " command_sub
659                 patharr[$command_sub]='^'
660                 update_path
661                 ;;
662             E|e)
663                 read -e -p "Index: " command_sub
664                 read -e -p "Modify this entry: " -i "${patharr[$command_sub]}" patharr[$command_sub]
665                 update_path
666                 ;;
667             M|m)
668                 read -e -p "From: " command_sub
669                 read -e -p "To: " command_sub2
670                 swaptmp="${patharr[$command_sub]}"
671                 patharr[$command_sub]="${patharr[$command_sub2]}"
672                 patharr[$command_sub2]="$swaptmp"
673                 unset swaptmp
674                 update_path
675                 ;;
676             R|r)
677                 if [ "$1" = "ld" ]
678                 then
679                     current_path="$LD_LIBRARY_PATH"
680                 else
681                     current_path="$PATH"
682                 fi
683                 ;;
684             Q|q)
685                 if [ "$1" = "ld" ]
686                 then
687                     export LD_LIBRARY_PATH="$current_path"
688                     echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
689                     history -s "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
690                 else
691                     export PATH="$current_path"
692                     echo "PATH=$PATH"
693                     history -s "PATH=$PATH"
694                 fi
695                 should_continue=''
696                 ;;
697             *)
698                 printf " \e[33m*** Unknown command ***\e[m \n"
699                 ;;
700         esac
701     done
702     unset patharr
703     unset current_path
704 }
705 
706 ########## PATH Editor End ##########
707 
708 function backup_file ()
709 {
710     split_arguments "$@"
711     local current_time=`date +%Y%m%d`
712     local rootfilename
713     local -i i=0
714     local -i j
715     while [ "${arglist[$i]}" ]
716     do
717         if [ '!' -f "${arglist[$i]}" ]
718         then
719             printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
720             i=$i+1
721             continue
722         fi
723         rootfilename="${arglist[$i]}.$current_time"
724         if [ -e "$rootfilename" ]
725         then
726             j=0
727             while [ "$j" -lt "10" ]
728             do
729                 if [ -e "$rootfilename.$j" ]
730                 then
731                     j=$j+1
732                     continue
733                 else
734                     "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
735                     history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
736                     "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
737                     history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
738                     break
739                 fi
740             done
741             if [ '!' "$j" -lt "10" ]
742             then
743                 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
744                 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
745             fi
746         else
747             "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
748             history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
749             "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
750             history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
751         fi
752         i=$i+1
753     done
754     unset arglist
755     unset prefixlist
756 }
757 
758 function keep_sudo_credential ()
759 {
760     if [ "$1" ]
761     then
762         update_sudo_interval="$1"
763     else
764         update_sudo_interval="280"
765     fi
766     while true
767     do
768         sudo -v
769         sleep "$update_sudo_interval"
770     done
771 }
772 
773 function get_memory_info ()
774 {
775     if [ "`uname`" = "Linux" ]
776     then
777         local meminfoline="`free -m | \grep -i mem`"
778         local swapinfoline="`free -m | \grep -i swap`"
779         local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
780         local memused="`echo "$meminfoline" | awk '{print $3}'`"
781         local membuf="`echo "$meminfoline" | awk '{print $6}'`"
782         local memcache="`echo "$meminfoline" | awk '{print $7}'`"
783         local memprog=$(($memused-$membuf-$memcache))
784         local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
785         local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
786         echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
787         echo "Detail:"
788         echo "    Used:    `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
789         echo "    Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
790         echo "    Cached:  `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
791         if [ "$swaptotal" = "0" ]
792         then
793             echo "Swap: not available"
794         else
795             echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
796         fi
797     else
798         echo "Current operating system is not Linux."
799     fi
800 }
801 
802 function set_console_title ()
803 {
804     case "$TERM" in
805         screen)
806             printf "\033]0;"
807             echo -n "$*"
808             printf "\033\\"
809             ;;
810         xterm*)
811             printf "\033]0;"
812             echo -n "$*"
813             printf "\007"
814             ;;
815         *)
816             echo "Your terminal may not have the hardstatus line."
817             echo "Note: TERM=$TERM"
818             ;;
819     esac
820 }
821 
822 function mvfile ()
823 {
824     while [ "$1" ] 
825     do
826         if [ '!' -e "$1" ]
827         then
828             printf "\e[33mWarning\e[m: $1 does not exist.\n"
829             shift
830             continue
831         fi
832         echo "Old name: $1"
833         read -p "New name: " -e -i "$1" new_file_name
834         if [ "$new_file_name" ] && [ "$1" != "$new_file_name" ]
835         then
836             \mv -iv "$1" "$new_file_name"
837             history -s mv -iv "$1" "$new_file_name"
838         fi
839         shift
840     done
841     unset new_file_name
842 }
843 
844 # Doing something
845 
846 umask 0022
847 
848 if [ "$interactive_shell" ]
849 then
850     echo "Running interactive shell configuration"
851     check_important_files
852     startcolor
853     prehistory_backup
854     bind '"\e[A":history-search-backward'
855     bind '"\e[B":history-search-forward'
856     if [ -z "$PROMPT_COMMAND" ]; then
857         case "$TERM" in
858             xterm*)
859                 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
860                 ;;
861             screen)
862                 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
863                 ;;
864         esac
865     fi
866 fi
867 
868 if [ "`uname`" = "Linux" ]
869 then
870     [ "$interactive_shell" ] && echo "Setting special things for Linux"
871     REALPATH_PROGRAM="readlink -f"
872     ulimit -c unlimited
873 fi
874 
875 [ "$interactive_shell" ] && echo "Setting shell options"
876 
877 shopt -s histappend
878 shopt -s checkwinsize
879 shopt -s checkjobs
880 shopt -s checkhash
881 shopt -s cmdhist
882 shopt -s mailwarn
883 
884 [ "$interactive_shell" ] && {
885     echo "Done"
886     if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
887     then
888         printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
889     fi
890 }
891