1 #!/bin/bash
   2 #
   3 # /home/lantw44/.bash_include
   4 #
   5 # -- UTF-8 --
   6 #
   7 # 設定用外部檔案:
   8 #     (1) 若 .bash_title 存在且未設定自動修改視窗標題,則依照本檔案內定規則修改
   9 #     (2) 若 .bash_addps 存在,則第一行的的內容會加到 colorprompting 變數,第二
  10 #         行的內容會加到 nocolorprompting 變數
  11 
  12 #### if tty -s; then interactive_shell=1; fi
  13 if [[ "$-" == *i* ]] ; then interactive_shell=1; fi
  14 
  15 [ "$interactive_shell" ] && echo "Running .bash_include"
  16 [ "$interactive_shell" ] && default_tty_setting="`stty -g`"
  17 
  18 # Internal Variables
  19 
  20 colorprompting='\[\e[1;31m\]\!\[\e[m\] [\[\e[1;33m\]\u\[\e[m\]@\[\e[1;32m\]\h\[\e[m\] \[\e[1;36m\]\w\[\e[m\]]'
  21 nocolorprompting='\! [\u@\h \w]'
  22 
  23 if [ "$SSH_CONNECTION" ]
  24 then
  25     colorprompting="\[\e[1;44m\]*\[\e[m\]$colorprompting"
  26     nocolorprompting="*$nocolorprompting"
  27 fi
  28 
  29 if [ -f "$HOME/.bash_addps" ]
  30 then
  31     exec 3< "$HOME/.bash_addps"
  32     read -u 3 -r oneline
  33     colorprompting="$oneline $colorprompting"
  34     read -u 3 -r oneline
  35     nocolorprompting="$oneline $nocolorprompting"
  36     exec 3<&-
  37     unset oneline
  38 fi
  39 
  40 if [ "$WINDOW" ]
  41 then
  42     colorprompting="$colorprompting<$WINDOW>"
  43     nocolorprompting="$nocolorprompting<$WINDOW>"
  44 fi
  45 
  46 if [ "$TMUX_PANE" ]
  47 then
  48     colorprompting="$colorprompting$TMUX_PANE"
  49     nocolorprompting="$nocolorprompting$TMUX_PANE"
  50 fi
  51 
  52 colorprompting="${colorprompting}\\$ "
  53 nocolorprompting="${nocolorprompting}\\$ "
  54 
  55 historycountfile="$HOME/.bash_history.count"
  56 historybackupfile="$HOME/.bash_history.bak"
  57 bgrunfiledir="$HOME/tmp/bgrun-`whoami`"
  58 trashdir="$HOME/trash"
  59 
  60 HISTSIZE=2147483647
  61 HISTFILESIZE=2147483647
  62 HISTCONTROL=ignoredups:ignorespace
  63 HISTTIMEFORMAT="%F %T "
  64 
  65 REALPATH_PROGRAM="realpath"
  66 
  67 CFLAGS="-Wall -pipe -g"
  68 
  69 
  70 # Environment Variables
  71 
  72 export EDITOR=vim
  73 export FCEDIT=vim
  74 export PAGER=less
  75 
  76 
  77 
  78 
  79 # Aliases
  80 
  81 alias startcolor='PS1=$colorprompting'
  82 alias stopcolor='PS1=$nocolorprompting'
  83 
  84 alias ll='ls -l'
  85 alias grep='grep --color=always'
  86 alias rm='rm -i'
  87 alias cp='cp -pi'
  88 alias mv='mv -i'
  89 alias jobs='jobs -l'
  90 alias less='less -RS'
  91 
  92 alias cccc='LANG=C;LANGUAGE=C;LC_ALL=C'
  93 alias enus='LANG=en_US.UTF-8;LANGUAGE=en_US:en;LC_ALL=en_US.UTF-8'
  94 alias big5='LANG=zh_TW.Big5;LANGUAGE=zh_TW:zh;LC_ALL=zh_TW.Big5'
  95 alias zhtw='LANG=zh_TW.UTF-8;LANGUAGE=zh_TW:zh;LC_ALL=zh_TW.UTF-8'
  96 
  97 alias savetty='default_tty_setting=`stty -g`'
  98 alias resetty='stty $default_tty_setting'
  99 
 100 alias vimhtml='vim -c "set ts=2" -c "set sw=2"'
 101 alias screen256='screen -T screen-256color'
 102 
 103 
 104 # Functions
 105 
 106 function compile_all ()
 107 {
 108     local noask=0
 109     local mycc="${CC}"
 110     local mycxx="${CPP}"
 111     [ "$1" = '' ] && echo "Which file(s) do you want to compile? " && return 1
 112     [ "$1" = "-n" ] && noask=1
 113     if [ "$noask" = "0" ]; then
 114         read -e -p "CFLAGS: " -i "$CFLAGS" NEWCFLAGS
 115         read -e -p "LDFLAGS: " -i "$LDFLAGS" NEWLDFLAGS
 116         [ "$NEWCFLAGS" '!=' '' ] && CFLAGS=$NEWCFLAGS
 117         [ "$NEWLDFLAGS" '!=' '' ] && LDFLAGS=$NEWLDFLAGS
 118     else
 119         shift
 120     fi
 121     [ -z "${mycc}" ] && mycc=cc
 122     [ -z "${mycxx}" ] && mycxx=c++
 123     while [ "$1" '!=' '' ]
 124     do
 125         local TARGETFILE="`echo "$1" | cut -d . -f 1`"
 126         local SUFFIX="`echo "$1" | cut -d . -f 2`"
 127         if [ -f "$1" ]; then
 128             true
 129         else
 130             printf\
 131             "\e[1;33mWarning\e[0m: $1 Non-existent file or not a regular file\n"
 132             shift ; continue
 133         fi
 134         [ "$TARGETFILE" = "$1" ] && shift && continue
 135         if [ "$SUFFIX" = "c" ]; then
 136             echo "[${mycc}] $1 -> $TARGETFILE"
 137             ${mycc} $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
 138         elif [ "$SUFFIX" = "cpp" ]; then
 139             echo "[${mycxx}] $1 -> $TARGETFILE"
 140             ${mycxx} $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
 141         else
 142             printf "$1: Unknown suffix (\e[1;33mskipped\e[0m)\n"
 143         fi
 144         [ "$?" '!=' "0" ] && printf\
 145             '\e[1;31mError\e[0m while compiling file\n'
 146         shift
 147     done
 148     return 0
 149 }
 150 
 151 
 152 function convert_to_html ()
 153 {
 154     while [ "$1" '!=' '' ]
 155     do
 156         for i in "$1"
 157         do
 158             vim $i -c 'set background=dark' \
 159                 -c 'highlight PreProc ctermfg=darkcyan' \
 160                 -c "$BEFORE_CONVERT_TO_HTML" \
 161                 -c "$BEFORE_CONVERT_TO_HTML1" \
 162                 -c "$BEFORE_CONVERT_TO_HTML2" \
 163                 -c TOhtml \
 164                 -c :w \
 165                 -c :qa
 166         done
 167         shift
 168     done
 169 }
 170 
 171 function mkscreenacl ()
 172 {
 173     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"
 174     while [ "$1" '!=' '' ]
 175     do
 176         for i in $PERMIT_COMMAND
 177         do
 178             echo "aclchg $1 +x $i"
 179         done
 180         echo "aclchg $1 -rw \"\#?\""
 181         shift
 182     done
 183 }
 184 
 185 function get_file_size ()
 186 {
 187     split_arguments "$@"
 188     "${prefixlist[@]}" du -s "${arglist[@]}" | {
 189         read -d "   " dirsize
 190         echo "$dirsize"
 191         read throwaway
 192     }
 193     unset arglist
 194     unset prefixlist
 195 }
 196 
 197 ########## Background ##########
 198 
 199 alias bgr=bgrun
 200 alias bgv=bgview
 201 alias bgl=bglist
 202 alias bgc=bgcount
 203 alias bgls=bglist
 204 alias bgrm=bgclean
 205 
 206 function bgrun ()
 207 {
 208     [ "$#" = "0" ] && return 1
 209     [ '!' -d "$bgrunfiledir" ] && createdir_askmode "$bgrunfiledir" 0750
 210     local current_time=`date "+%Y%m%d-%H%M%S"`
 211     local cmdname=`echo "$1" | sed -e 's/-/_/g' -e 's/\\//_/g' -e 's/ /_/g'`
 212     if [ "`echo "$cmdname" | cut -c 1`" == "_" ]
 213     then
 214         cmdname=`echo "$cmdname" | cut -c 2-`
 215     fi
 216     local filename="$bgrunfiledir/$current_time-$cmdname"
 217     echo "Writing to $filename"
 218     {
 219         echo -n "$BASHPID " > "$filename"
 220         echo "$@" >> "$filename"
 221         exec "$@" &>> "$filename"
 222     } &
 223 }
 224 
 225 function bglist ()
 226 {
 227     local viewtime=0
 228     [ "$1" = "--full" ] && viewtime=1
 229     {
 230         for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort`
 231         do
 232             [ "$viewtime" = "1" ] && echo "$i"
 233             head -n 1 "$i" | {
 234                 local procpid
 235                 local cmdline
 236                 read -d ' ' procpid
 237                 read cmdline
 238                 printf "(%5d) %s\n" "$procpid" "$cmdline"
 239             }
 240         done
 241     } | {
 242         if [ "$viewtime" = "1" ]
 243         then
 244             echo " INDEX         TIME          PID   COMMAND"
 245             local readstat=0
 246             local -i i=1
 247             while true
 248             do
 249                 local dateandtime_long
 250                 local cmdline
 251                 read dateandtime_long
 252                 read cmdline
 253                 [ "$?" '!=' "0" ] && break
 254                 local dateandtime=`basename "$dateandtime_long"`
 255     ####        local part_year=`echo $dateandtime | cut -c 1-4`
 256     ####        local part_month=`echo $dateandtime | cut -c 5-6`
 257     ####        local part_date=`echo $dateandtime | cut -c 7-8`
 258     ####        local part_hour=`echo $dateandtime | cut -c 10-11`
 259     ####        local part_minute=`echo $dateandtime | cut -c 12-13`
 260     ####        local part_second=`echo $dateandtime | cut -c 14-15`
 261                 echo "$dateandtime" | {
 262                     read -n 4 part_year
 263                     read -n 2 part_month
 264                     read -n 2 part_date
 265                     read -n 1 drop_this_char; unset drop_this_char
 266                     read -n 2 part_hour
 267                     read -n 2 part_minute
 268                     read -n 2 part_second
 269                     printf '%6d' "$i"
 270                     echo " $part_year-$part_month-$part_date $part_hour:$part_minute:$part_second $cmdline"
 271                 }
 272                 i=$i+1
 273             done
 274         else
 275             echo " INDEX    PID   COMMAND"
 276             cat -n
 277         fi
 278     } | $PAGER
 279 }
 280 
 281 function bgview ()
 282 {
 283     local -i yourchoice
 284     if [ "$1" = "" ]
 285     then
 286         yourchoice=`bgcount`
 287     else
 288         if [ "$1" -le "0" ]
 289         then
 290             yourchoice=$((`bgcount`+$1))
 291         else
 292             yourchoice=$1
 293         fi
 294     fi
 295     echo "Your choice is $yourchoice."
 296     local realfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${yourchoice}p`
 297     head -n 1 "$realfilename" | {
 298         read -d ' ' procpid
 299         read cmdline
 300         echo "PID: $procpid"
 301         echo "Command Line: $cmdline"
 302     }
 303     read -e -p "View '$realfilename' ? " confirm
 304     if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]
 305     then
 306         return 1
 307     fi
 308     {
 309         printf "===> Process Information: "
 310         cat "$realfilename"
 311     } | $PAGER
 312 }
 313 
 314 function bgcount ()
 315 {
 316     find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | wc | awk '{print $2}'
 317 }
 318 
 319 function bgclean ()
 320 {
 321     if [ "$1" = "all" ]
 322     then
 323         echo "Removing the directory $bgrunfiledir"
 324         rm -rf "$bgrunfiledir" &
 325         return 0
 326     else
 327         split_arguments "$@"
 328         local -i i=0
 329         while [ "${arglist[$i]}" ]
 330         do
 331             arglist[$i]="-e ${arglist[$i]}p"
 332             i=$i+1
 333         done
 334         local oneline
 335         find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${arglist[*]} | {
 336             while read oneline
 337             do
 338                 echo "Removing $oneline"
 339                 rm -f "$oneline"
 340             done
 341         }
 342     fi
 343     unset arglist
 344     unset prefixlist
 345 }
 346 
 347 function bgdu ()
 348 {
 349     local -i j=1
 350     {
 351         echo " INDEX     SIZE   PID   COMMAND"
 352         for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort`
 353         do
 354             head -n 1 "$i" | {
 355                 local procpid
 356                 local cmdline
 357                 read -d ' ' procpid
 358                 read cmdline
 359                 printf "%6d %8d (%5d) %s\n" "$j" \
 360                     "`get_file_size "$i"`" \
 361                     "$procpid" "$cmdline"
 362             }
 363             j=$j+1
 364         done
 365     } | $PAGER
 366 }
 367 
 368 ########## Background End ##########
 369 
 370 function check_dmesg ()
 371 {
 372     [ "$#" = "0" ] && return 1
 373 
 374     while true
 375     do
 376         PREVIOS_DMESG_BUF="$DMESG_BUF"
 377         DMESG_BUF="`dmesg`"
 378         [ "$PREVIOS_DMESG_BUF" '!=' "$DMESG_BUF" ] && [ "$FIRST_RUN" = "0" ] && echo '===> You should check the system message buffer <==='
 379         sleep $1
 380         [ "$?" '!=' "0" ] && return 1
 381         FIRST_RUN=0
 382     done
 383 }
 384 
 385 function check_system_status ()
 386 {
 387     [ "$#" = "0" ] && return 1
 388 
 389     filename_mail="$MAIL"
 390     filename_messages="/var/log/messages"
 391     filename_audit="/var/log/audit/audit.log"
 392 
 393     while true
 394     do
 395         previous_dmesg_buf="$current_dmesg_buf"
 396         current_dmesg_buf="`dmesg`"
 397         previous_mail_info="$current_mail_info"
 398         current_mail_info="`ls -l "$filename_mail"`"
 399         previous_messages_info="$current_messages_info"
 400         current_messages_info="`ls -l "$filename_messages"`"
 401         previous_audit_info="$current_audit_info"
 402         current_audit_info="`ls -l "$filename_audit"`"
 403         if [ "$first_run" = "0" ]
 404         then
 405             [ "$previous_dmesg_buf" '!=' "$current_dmesg_buf" ] && echo "===> The system message buffer is modified (dmesg) <==="
 406             [ "$previous_mail_info" '!=' "$current_mail_info" ] && echo "===> Your mailbox $filename_mail is modified <==="
 407             [ "$previous_messages_info" '!=' "$current_messages_info" ] && echo "===> $filename_messages is modified <==="
 408             [ "$previous_audit_info" '!=' "$current_audit_info" ] && echo "===> $filename_audit is modified <==="
 409         fi
 410         sleep $1
 411         first_run=0
 412     done
 413 }
 414 
 415 function prehistory_backup ()
 416 {
 417     echo "Checking your current history file"
 418     local -i currentcount=`cat "$HISTFILE" | wc -l`
 419     [ '!' -f "$historycountfile" ] && touch "$historycountfile"
 420     local -i previoushistorycount
 421     previoushistorycount=`cat "$historycountfile"`
 422     if [ "$currentcount" -lt "$previoushistorycount" ]
 423     then
 424         printf "\e[1;31mWarning\e[m: Your $HISTFILE may be TRUNCATED OR OVERWRITTEN BY OTHER PROGRAMS!\n"
 425         printf "Note: \e[1;33m$currentcount\e[m < $previoushistorycount\n"
 426         echo "Your $historycountfile and $historybackupfile will not be overwritten until this problem is fixed."
 427         echo " 1. Check your $HISTFILE."
 428         echo " 2. Edit your $HISTFILE manually if some unexpected changes are found."
 429         echo "    (You may need $historybackupfile to do it) "
 430         echo " 3. Remove the file $historycountfile."
 431         echo " 4. Run the command \`prehistory_backup' again."
 432         return 3
 433     fi
 434     echo -n "Backing up your current history file ($previoushistorycount -> $currentcount, "
 435     if [ "$previoushistorycount" = "$currentcount" ]
 436     then
 437         echo "no modification)"
 438     else
 439         echo "+$(($currentcount-$previoushistorycount)))"
 440     fi
 441     echo "$currentcount" > "$historycountfile"
 442     \cp -f "$HISTFILE" "$historybackupfile"
 443 }
 444 
 445 ########## Trash Manager ##########
 446 
 447 alias trash_put=trash_mv
 448 alias trash_add=trash_mv
 449 alias trash_list=trash_ls
 450 alias trash_ct=trash_count
 451 alias trash_restore=trash_recover
 452 alias trash_rc=trash_recover
 453 alias trash_drop=trash_rm
 454 alias trash_clean=trash_rm
 455 
 456 function trash_mv ()
 457 {
 458     [ "$#" = "0" ] && return 1
 459     [ '!' -d "$trashdir" ] && createdir_askmode "$trashdir" 0700
 460     local original_path
 461     local current_time
 462     local -i i=0
 463     split_arguments "$@"
 464     while [ "${arglist[$i]}" ]
 465     do
 466         original_path="`"${prefixlist[@]}" $REALPATH_PROGRAM "${arglist[$i]}"`"
 467         current_time=`date "+%Y%m%d-%H%M%S"`
 468         better_time=`date "+%Y-%m-%d %H:%M:%S"`
 469         dirname="`basename "${arglist[$i]}" | sed -e 's/-/_/g' -e 's/ /_/g'`"
 470         fulldirname="$trashdir/$current_time-$dirname"
 471         mkdir -p "$fulldirname"
 472         echo "Move: ${arglist[$i]} -> $fulldirname"
 473         "${prefixlist[@]}" mv "${arglist[$i]}" "$fulldirname"
 474         if [ "$?" = "0" ]
 475         then
 476             echo "$better_time" > "$fulldirname/information.date"
 477             echo "$original_path" > "$fulldirname/information.path"
 478         else
 479             rmdir "$fulldirname"
 480         fi
 481         i=$i+1
 482         shift
 483     done
 484     unset arglist
 485     unset prefixlist
 486 }
 487 
 488 function trash_rm ()
 489 {
 490     split_arguments "$@"
 491     local -i i=0
 492     while [ "${arglist[$i]}" ]
 493     do
 494         arglist[$i]="-e ${arglist[$i]}p"
 495         i=$i+1
 496     done
 497     trash_dirname=`find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} `
 498     echo 'Type rm -rf $trash_dirname to remove them.'
 499     unset arglist
 500     unset prefixlist
 501 }
 502 
 503 function trash_ls ()
 504 {
 505     local -i i=1
 506     local oneline
 507     find "$trashdir" -mindepth 1 -maxdepth 1 | sort | {
 508         while read oneline
 509         do
 510             printf "%6d %s %s\n" "$i" \
 511                 "`cat "$oneline/information.date"`" \
 512                 "`cat "$oneline/information.path"`"
 513             i=$i+1
 514         done
 515     } | $PAGER
 516 }
 517 
 518 function trash_pushd ()
 519 {
 520     [ -z "$1" ] && return 1
 521     pushd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
 522 }
 523 
 524 function trash_cd ()
 525 {
 526     [ -z "$1" ] && return 1
 527     cd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
 528 }
 529 
 530 function trash_recover ()
 531 {
 532     [ -z "$1" ] && return 1
 533     split_arguments "$@"
 534     local -i i=0
 535     while [ "${arglist[$i]}" ]
 536     do
 537         arglist[$i]="-e ${arglist[$i]}p"
 538         i=$i+1
 539     done
 540     find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} | {
 541         while read oneline
 542         do
 543             local fromfile="$oneline/`basename "$(cat "$oneline/information.path")"`"
 544             local tofile="`dirname "$(cat "$oneline/information.path")"`"
 545             if [ -e "`cat "$oneline/information.path"`" ]
 546             then
 547                 echo "Destination file exists."
 548                 continue
 549             fi
 550             echo "Move: $fromfile -> $tofile"
 551             "${prefixlist[@]}" mv -f "$fromfile" "$tofile"
 552             if [ "$?" = "0" ]
 553             then
 554                 echo "Remove: $oneline"
 555                 \rm -rf "$oneline"
 556             fi
 557         done
 558     }
 559     unset arglist
 560     unset prefixlist
 561 }
 562 
 563 function trash_count ()
 564 {
 565     find "$trashdir" -mindepth 1 -maxdepth 1 | wc | awk '{print $2}'
 566 }
 567 
 568 function trash_du ()
 569 {
 570     split_arguments "$@"
 571     local oneline
 572     local -i i=1
 573     find "$trashdir" -maxdepth 1 -mindepth 1 | sort | {
 574         while read oneline
 575         do
 576             printf "%6d %8d %s\n" "$i" \
 577                 "`get_file_size "$oneline" -- "${prefixlist[@]}"`" \
 578                 "`cat "$oneline/information.path"`"
 579             i=$i+1
 580         done
 581     } | $PAGER
 582     unset arglist
 583     unset prefixlist
 584 }
 585 
 586 ########## Trash Manager End ##########
 587 
 588 function split_arguments ()
 589 {
 590     local argcount=$#
 591     local -i i=0
 592     local prefix_start=0
 593     while [ "$1" ]
 594     do
 595         if [ "$prefix_start" == "0" ]
 596         then
 597             if [ "$1" = "--" ]
 598             then
 599                 prefix_start=1
 600                 i=0
 601                 shift
 602                 continue
 603             else
 604                 arglist[$i]="$1"
 605             fi
 606         else
 607             prefixlist[$i]="$1"
 608         fi
 609         i=$i+1
 610         shift
 611     done
 612 }
 613 
 614 function check_important_files ()
 615 {
 616     IMPORTANT_FILES="$HOME/.screenrc $HOME/.vimrc"
 617     for i in $IMPORTANT_FILES
 618     do
 619         [ '!' -f "$i" ] && printf "\e[1;31mWarning\e[m: \e[1;33m$i\e[m does not exist.\n"
 620     done
 621 }
 622 
 623 ########## PATH Editor ##########
 624 
 625 function split_path_core ()
 626 {
 627     echo "$current_path" | {
 628         while read -d : oneline
 629         do
 630             [ '!' "$oneline" = '^' ] && echo "$oneline"
 631         done
 632         [ '!' "$oneline" = '^' ] && echo "$oneline"
 633     }
 634     unset oneline
 635 }
 636 
 637 function split_path ()
 638 {
 639     coproc split_path_core
 640     readarray -t -u ${COPROC[0]} patharr
 641     wait $COPROC_PID
 642 }
 643 
 644 function update_path ()
 645 {
 646     current_path=''
 647     local -i i=0
 648     local firsttime="yes"
 649     while [ "${patharr[$i]}" ]
 650     do
 651         if [ '!' "${patharr[$i]}" = "^" ]
 652         then
 653             if [ "$firsttime" ]
 654             then
 655                 firsttime=''
 656             else
 657                 current_path+=':'
 658             fi
 659             current_path+="${patharr[$i]}"
 660         fi
 661         i=$i+1
 662     done
 663 }
 664 
 665 function old_path_editor ()
 666 {
 667     old_path_editor_core
 668 }
 669 
 670 function old_ldpath_editor ()
 671 {
 672     old_path_editor_core ld
 673 }
 674 
 675 function old_path_editor_core ()
 676 {
 677     if [ "$1" = "ld" ]
 678     then
 679         export current_path="$LD_LIBRARY_PATH"
 680     else
 681         export current_path="$PATH"
 682     fi
 683     local should_continue="yes"
 684     local command
 685     local command_sub
 686     local command_sub2
 687     local -i i
 688     while [ "$should_continue" ]
 689     do
 690         split_path
 691         i=0
 692         echo "========================================"
 693         while [ "${patharr[$i]}" ]
 694         do
 695             echo "$i: ${patharr[$i]}"
 696             i=$i+1
 697         done
 698         [ "$i" = '0' ] && echo "(Empty or not declared)"
 699         echo "========================================"
 700         read -e -p "[A]ppend/(D)elete/(E)dit/(M)ove/(R)eset/(Q)uit ? " command
 701         case "$command" in
 702             ''|A|a)
 703                 read -e -p "Type a new entry: " patharr[$i]
 704                 update_path
 705                 ;;
 706             D|d)
 707                 read -e -p "Index: " command_sub
 708                 patharr[$command_sub]='^'
 709                 update_path
 710                 ;;
 711             E|e)
 712                 read -e -p "Index: " command_sub
 713                 read -e -p "Modify this entry: " -i "${patharr[$command_sub]}" patharr[$command_sub]
 714                 update_path
 715                 ;;
 716             M|m)
 717                 read -e -p "From: " command_sub
 718                 read -e -p "To: " command_sub2
 719                 swaptmp="${patharr[$command_sub]}"
 720                 patharr[$command_sub]="${patharr[$command_sub2]}"
 721                 patharr[$command_sub2]="$swaptmp"
 722                 unset swaptmp
 723                 update_path
 724                 ;;
 725             R|r)
 726                 if [ "$1" = "ld" ]
 727                 then
 728                     current_path="$LD_LIBRARY_PATH"
 729                 else
 730                     current_path="$PATH"
 731                 fi
 732                 ;;
 733             Q|q)
 734                 if [ "$1" = "ld" ]
 735                 then
 736                     export LD_LIBRARY_PATH="$current_path"
 737                     echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
 738                     history -s "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
 739                 else
 740                     export PATH="$current_path"
 741                     echo "PATH=$PATH"
 742                     history -s "PATH=$PATH"
 743                 fi
 744                 should_continue=''
 745                 ;;
 746             *)
 747                 printf " \e[33m*** Unknown command ***\e[m \n"
 748                 ;;
 749         esac
 750     done
 751     unset patharr
 752     unset current_path
 753 }
 754 
 755 ########## PATH Editor End ##########
 756 
 757 ########## New PATH Editor ##########
 758 
 759 function ldpath_editor ()
 760 {
 761     path_editor LD_LIBRARY_PATH
 762 }
 763 
 764 function newpath_init ()
 765 {
 766     unset newpath
 767     local pathsp="$pathorgval"
 768     local pathentry
 769     local -i i
 770     eval pathsp='"${pathsp// /\\}"'
 771     pathsp="${pathsp//:/ }"
 772     i=0
 773     for pathentry in $pathsp
 774     do
 775         newpath[$i]="${pathentry//\\/ }"
 776         i=$i+1
 777     done
 778 }
 779 
 780 function newpath_gen ()
 781 {
 782     local -i i
 783     pathmodval=""
 784     i=0
 785     while [ "${newpath[$i]}" ]
 786     do
 787         if [ "$i" != 0 ]; then pathmodval+=":"; fi
 788         pathmodval+="${newpath[$i]}"
 789         i=$i+1
 790     done
 791 }
 792 
 793 function path_editor ()
 794 {
 795     local newpathvarname
 796     local badcommand
 797     local command
 798     local command_sub
 799     local command_sub2
 800     local should_continue="yes"
 801     local -i i
 802 
 803     if [ -z "$1" ]
 804     then
 805         newpathvarname="PATH"
 806     else
 807         newpathvarname="$1"
 808     fi
 809 
 810     eval pathorgval=\${${newpathvarname}}
 811     newpath_init
 812 
 813     while [ "$should_continue" ]
 814     do
 815         i=0
 816         echo -n $'\e[2J\e[0;0H'
 817         if [ "$badcommand" = "yes" ]; then
 818             printf " \e[33m*** Command \`$command' is unknown ***\e[m \n"
 819             badcommand=""
 820         fi
 821         echo "        New PATH Editor for BASH        "
 822         echo "========================================"
 823         echo "Environment Variable: $newpathvarname"
 824         echo "----------------------------------------"
 825         while [ "${newpath[$i]}" ]
 826         do
 827             echo "$i: ${newpath[$i]}"
 828             i=$i+1
 829         done
 830         [ "$i" = '0' ] && echo "(Empty or not declared)"
 831         echo "========================================"
 832         read -e -p "[A]ppend/(I)nsert/(D)elete/(E)dit/(M)ove/(S)wap/(R)eset/(Q)uit ? " command
 833         case "$command" in
 834             ''|A|a)
 835                 read -e -p "Type a new entry: " newpath[$i]
 836                 ;;
 837             I|i)
 838                 read -e -p "Type a new entry: " command_sub
 839                 if [ -z "$command_sub" ]; then continue; fi
 840                 newpath_tmp=( "${newpath[@]}" )
 841                 unset newpath
 842                 newpath="$command_sub"
 843                 i=0
 844                 while [ "${newpath_tmp[$i]}" ]
 845                 do
 846                     newpath[$i+1]="${newpath_tmp[$i]}"
 847                     i=$i+1
 848                 done
 849                 unset newpath_tmp
 850                 ;;
 851             D|d)
 852                 read -e -p "Index: " command_sub
 853                 if [ -z "$command_sub" ]; then continue; fi
 854                 i="$command_sub"
 855                 i=$i+1
 856                 while [ "${newpath[$i]}" ]
 857                 do
 858                     newpath[$i-1]="${newpath[$i]}"
 859                     i=$i+1
 860                 done
 861                 unset newpath[$i-1]
 862                 ;;
 863             E|e)
 864                 read -e -p "Index: " command_sub
 865                 read -e -p "Modify this entry: " -i "${newpath[$command_sub]}" newpath[$command_sub]
 866                 ;;
 867             M|m)
 868                 read -e -p "From: " command_sub
 869                 read -e -p "To: " command_sub2
 870                 if [ "$command_sub" -eq "$command_sub2" ]; then continue; fi
 871                 cmdsubval="${newpath[$command_sub]}"
 872                 if [ "$command_sub" -gt "$command_sub2" ]; then
 873                     i="$command_sub"
 874                     while [ "$i" -gt "$command_sub2" ]
 875                     do
 876                         newpath[$i]="${newpath[$i-1]}"
 877                         i=$i-1
 878                     done
 879                     newpath[$command_sub2]="$cmdsubval"
 880                 else
 881                     i="$command_sub"
 882                     while [ "$i" -lt "$command_sub2" ]
 883                     do
 884                         newpath[$i]="${newpath[$i+1]}"
 885                         i=$i+1
 886                     done
 887                     newpath[$command_sub2]="$cmdsubval"
 888                 fi
 889                 unset cmdsubval
 890                 ;;
 891             S|s)
 892                 read -e -p "First entry: " command_sub
 893                 read -e -p "Second entry: " command_sub2
 894                 swaptmp="${newpath[$command_sub]}"
 895                 newpath[$command_sub]="${newpath[$command_sub2]}"
 896                 newpath[$command_sub2]="$swaptmp"
 897                 unset swaptmp
 898                 ;;
 899             R|r)
 900                 read -e -p "Discard all changes [y/N] ? " command_sub
 901                 if [ "$command_sub" = "y" ]; then
 902                     eval pathorgval=\${${newpathvarname}}
 903                     newpath_init
 904                 fi
 905                 ;;
 906             Q|q)
 907                 newpath_gen
 908                 eval export ${newpathvarname}=\"$pathmodval\"
 909                 echo "${newpathvarname}=$pathmodval"
 910                 history -s "${newpathvarname}=$pathmodval"
 911                 should_continue=''
 912                 ;;
 913             *)
 914                 badcommand="yes"
 915                 ;;
 916         esac
 917     done
 918     unset newpath
 919     unset pathorgval
 920     unset pathmodval
 921 }
 922 
 923 ########## New PATH Editor End ##########
 924 
 925 ########## Configuration File ##########
 926 
 927 function configfile_fetch ()
 928 {
 929     local mirror_list="
 930         http://www.tfcis.org/~lantw44/configfile/
 931         http://phantom.tfcis.org/~lantw44/mirror/forum_tfcis_org/configfile/
 932         http://master.lant.com.tw/~lantw44/configfile/
 933         http://www.csie.ntu.edu.tw/~b01902062/configfile/"
 934     local url_append
 935     local file_name
 936     local file_version
 937 
 938     if [ "$3" ]; then
 939         url_append="$3"
 940     else
 941         url_append="Unix-like/"
 942     fi
 943     if [ "$1" ]; then
 944         file_name="$1"
 945     else
 946         file_name="bash_include vimrc screenrc"
 947     fi
 948     if [ "$2" ]; then
 949         file_version="-$2"
 950     else
 951         url_append="${url_append}latest/"
 952     fi
 953 
 954     for file in $file_name
 955     do
 956         for site in $mirror_list
 957         do
 958             if fetch_remote_file "$HOME/.${file}.new" \
 959                 "${site}${url_append}${file}${file_version}"
 960             then
 961                 mv -vf "$HOME/.${file}.new" "$HOME/.${file}"
 962                 break
 963             fi
 964         done
 965     done
 966 }
 967 
 968 function configfile_initial_setup ()
 969 {
 970     cat >> ~/.bashrc << EOF
 971 if [ -f ~/.bash_include ]; then
 972     . ~/.bash_include
 973 fi
 974 EOF
 975     if [ "`uname`" = "FreeBSD" ]; then
 976         cat >> ~/.bash_login << EOF
 977 GET_TTY_NAME=`tty | cut -c 9`
 978 [ "$GET_TTY_NAME" = 'v' ] && echo "Login from local virtual terminal: `tty`"
 979 [ "$GET_TTY_NAME" = 'p' ] && echo "Login from pseudo terminal: `tty`"
 980 [ "$GET_TTY_NAME" = '/' ] && echo "Login from pseudo terminal: `tty`"
 981 unset GET_TTY_NAME
 982 EOF
 983     fi
 984     cat >> ~/.bash_login << EOF
 985 if [ -f ~/.bash_include ]; then
 986     . ~/.bashrc
 987 fi
 988 EOF
 989 }
 990 
 991 ########## Configuration File End  ##########
 992 
 993 function varset ()
 994 {
 995     local varoldvalue
 996     local varnewvalue
 997     while [ "$1" ]
 998     do
 999         eval varoldvalue=\${$1}
1000         read -r -e -p "$1=" -i "$varoldvalue" varnewvalue
1001         eval $1=\"$varnewvalue\"
1002         shift
1003     done
1004 }
1005 
1006 function is_file_type ()
1007 {
1008     local filename="$1"
1009     local typename="$2"
1010     shift 2
1011     [ "`"$@" find "$filename" -maxdepth 0 -type "$typename"`" ] && return 0
1012     return 1
1013 }
1014 
1015 function editlink ()
1016 {
1017     local newdest
1018     local orgdest
1019     split_arguments "$@"
1020     local -i i=0
1021     while [ "${arglist[$i]}" ]
1022     do
1023         if is_file_type "${arglist[$i]}" "l" "${prefixlist[@]}"; then
1024             orgdest="`${prefixlist[@]} readlink "${arglist[$i]}"`"
1025             read -e -p "EditLink: ${arglist[$i]} -> " -i "$orgdest" newdest
1026         else
1027             printf "\e[1;33mWarning\e[m: ${arglist[$i]} is not a symbolic link.\n"
1028             i=$i+1
1029             continue
1030         fi
1031         if [ "$newdest" ] && [ "$newdest" '!=' "$orgdest" ]; then
1032             "${prefixlist[@]}" rm -f "${arglist[$i]}"
1033             "${prefixlist[@]}" ln -sv "$newdest" "${arglist[$i]}"
1034         fi
1035         i=$i+1
1036     done
1037     unset arglist
1038     unset prefixlist
1039 }
1040 
1041 function backup_file ()
1042 {
1043     split_arguments "$@"
1044     local current_time=`date +%Y%m%d`
1045     local rootfilename
1046     local -i i=0
1047     local -i j
1048     while [ "${arglist[$i]}" ]
1049     do
1050         if [ '!' -f "${arglist[$i]}" ]
1051         then
1052             printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
1053             i=$i+1
1054             continue
1055         fi
1056         rootfilename="${arglist[$i]}.$current_time"
1057         if [ -e "$rootfilename" ]
1058         then
1059             j=0
1060             while [ "$j" -lt "10" ]
1061             do
1062                 if [ -e "$rootfilename.$j" ]
1063                 then
1064                     j=$j+1
1065                     continue
1066                 else
1067                     "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
1068                     history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
1069                     "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
1070                     history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
1071                     break
1072                 fi
1073             done
1074             if [ '!' "$j" -lt "10" ]
1075             then
1076                 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
1077                 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
1078             fi
1079         else
1080             "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
1081             history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
1082             "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
1083             history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
1084         fi
1085         i=$i+1
1086     done
1087     unset arglist
1088     unset prefixlist
1089 }
1090 
1091 function keep_sudo_credential ()
1092 {
1093     if [ "$1" ]
1094     then
1095         update_sudo_interval="$1"
1096     else
1097         update_sudo_interval="280"
1098     fi
1099     while true
1100     do
1101         sudo -v
1102         sleep "$update_sudo_interval"
1103     done
1104 }
1105 
1106 function get_memory_info ()
1107 {
1108     case "`uname`" in
1109         "Linux"|"GNU")
1110             local meminfoline="`free -m | \grep -i mem`"
1111             local swapinfoline="`free -m | \grep -i swap`"
1112             local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
1113             local memused="`echo "$meminfoline" | awk '{print $3}'`"
1114             local membuf="`echo "$meminfoline" | awk '{print $6}'`"
1115             local memcache="`echo "$meminfoline" | awk '{print $7}'`"
1116             local memprog=$(($memused-$membuf-$memcache))
1117             local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
1118             local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
1119             echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
1120             echo "Detail:"
1121             echo "    Used:    `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
1122             echo "    Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
1123             echo "    Cached:  `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
1124             if [ "$swaptotal" = "0" ]
1125             then
1126                 echo "Swap: not available"
1127             else
1128                 echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
1129             fi
1130             ;;
1131         "FreeBSD")
1132             local mempagesize="`sysctl -n hw.pagesize`"
1133             local mempagecount="`sysctl -n hw.availpages`"
1134             local memactive="`sysctl -n vm.stats.vm.v_active_count`"
1135             local meminactive="`sysctl -n vm.stats.vm.v_inactive_count`"
1136             local memwire="`sysctl -n vm.stats.vm.v_wire_count`"
1137             local memcache="`sysctl -n vm.stats.vm.v_cache_count`"
1138             local memfree="`sysctl -n vm.stats.vm.v_free_count`"
1139             local swapenabled="`sysctl -n vm.swap_enabled`"
1140             echo "Memory (Active):   `printf %5d $(($memactive*$mempagesize/1048576))` MB (`printf %2d $(($memactive*100/$mempagecount))`%)"
1141             echo "Memory (Inactive): `printf %5d $(($meminactive*$mempagesize/1048576))` MB (`printf %2d $(($meminactive*100/$mempagecount))`%)"
1142             echo "Memory (Wired):    `printf %5d $(($memwire*$mempagesize/1048576))` MB (`printf %2d $(($memwire*100/$mempagecount))`%)"
1143             echo "Memory (Cache):    `printf %5d $(($memcache*$mempagesize/1048576))` MB (`printf %2d $(($memcache*100/$mempagecount))`%)"
1144             echo "Memory (Free):     `printf %5d $(($memfree*$mempagesize/1048576))` MB (`printf %2d $(($memfree*100/$mempagecount))`%)"
1145             echo "Total Memory:      `printf %5d $(($mempagecount*$mempagesize/1048576))` MB"
1146             if [ "$swapenabled" = "1" ]; then
1147                 echo ""
1148                 echo "Swap devices:"
1149                 swapinfo -m
1150             else
1151                 echo "Swap: not enabled"
1152             fi
1153             ;;
1154         *)
1155             echo "Unsupported operating system."
1156             ;;
1157     esac
1158 }
1159 
1160 function set_console_title ()
1161 {
1162     case "$TERM" in
1163         screen*)
1164             printf "\033]0;"
1165             echo -n "$*"
1166             printf "\033\\"
1167             ;;
1168         xterm*)
1169             printf "\033]0;"
1170             echo -n "$*"
1171             printf "\007"
1172             ;;
1173         *)
1174             echo "Your terminal may not have the hardstatus line."
1175             echo "Note: TERM=$TERM"
1176             ;;
1177     esac
1178 }
1179 
1180 function mvfile ()
1181 {
1182     local nocheck=0
1183     [ "$1" = "-n" ] && nocheck=1 && shift
1184     split_arguments "$@"
1185     local -i i=0
1186     while [ "${arglist[$i]}" ]
1187     do
1188         if [ "$nocheck" = "0" ] && [ '!' -e "${arglist[$i]}" ]
1189         then
1190             printf "\e[33mWarning\e[m: ${arglist[$i]} does not exist. (Use -n to override)\n"
1191             i=$i+1
1192             continue
1193         fi
1194         echo "Old name: ${arglist[$i]}"
1195         read -p "New name: " -e -i "${arglist[$i]}" new_file_name
1196         if [ "$new_file_name" ] && [ "${arglist[$i]}" != "$new_file_name" ]
1197         then
1198             "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
1199             history -s "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
1200         fi
1201         i=$i+1
1202     done
1203     unset arglist
1204     unset prefixlist
1205     unset new_file_name
1206 }
1207 
1208 function createdir_askmode ()
1209 {
1210     newdir_mode="$2"
1211     if mkdir -p "$1"
1212     then
1213         echo "Directory $1 is created."
1214         printf "Change the mode of the directory... "
1215         read -i "$newdir_mode" -p ">>> Mode: " -e newdir_mode
1216         chmod "$newdir_mode" "$1"
1217     else
1218         echo "Cannot create directory $1!"
1219         return 1
1220     fi
1221 }
1222 
1223 function get_terminal_size ()
1224 {
1225     # ESC 7              = 儲存游標位置和屬性
1226     # ESC [r             = 啟用全螢幕捲動
1227     # ESC [{row};{col}H  = 移動游標
1228     # ESC 6n             = 回報目前游標位置
1229     # ESC 8              = 還原游標位置和屬性
1230     echo -n $'\e7\e[r\e[999;999H\e[6n\e8' 1>&2
1231     read -s -d R getsize
1232     echo $getsize | sed 's#..\([0-9]*\);\([0-9]*\)#LINES=\1 COLUMNS=\2#'
1233 }
1234 
1235 function set_terminal_size ()
1236 {
1237     eval "export `get_terminal_size`"
1238     stty cols $COLUMNS rows $LINES
1239 }
1240 
1241 function unzip_nomac ()
1242 {
1243     unzip "$@" -x '__MACOSX/*' '*.DS_Store'
1244     return $?
1245 }
1246 
1247 function check_command_existent ()
1248 {
1249     which "$1" &> /dev/null
1250     return $?
1251 }
1252 
1253 function fetch_remote_file ()
1254 {
1255     local rval
1256     printf "==> Fetch remote file \e[1;33m$2\e[m as \e[1;35m$1\e[m ...\n"
1257     if check_command_existent curl; then
1258         curl -f -o "$1" "$2"
1259         rval=$?
1260     elif check_command_existent fetch; then
1261         fetch -o "$1" "$2"
1262         rval=$?
1263     elif check_command_existent wget; then
1264         wget --progress=dot -O "$1" "$2"
1265         rval=$?
1266     else
1267         echo "<== Sorry, I don't know how to fetch remote files on your system"
1268         return 1
1269     fi
1270     if [ "$rval" = "0" ]; then
1271         printf "<== \e[1;32mDone\e[m\n"
1272     else
1273         printf "<== \e[1;31mFailed\e[m\n"
1274     fi
1275     return $rval
1276 }
1277 
1278 ########## Help ##########
1279 
1280 alias helpf='help_function'
1281 alias helpm='help_myself'
1282 
1283 function print_iconv ()
1284 {
1285     [ "$1" = "$2" ] && cat && return 0
1286     iconv -f "$1" -t "$2"
1287 }
1288 
1289 function help_myself ()
1290 {
1291     echo "argc = $#"
1292     echo "argv[0] = $0"
1293     i=1
1294     while [ "$1" ]
1295     do
1296         echo "argv[$i] = $1"
1297         i=$(($i+1))
1298         shift
1299     done
1300 }
1301 
1302 function help_obsolete ()
1303 {
1304     cat << ENDHELPMSG
1305  @@@ Obsolete Group: PATH Editor @@@
1306     old_path_editor
1307     old_ldpath_editor
1308  x  split_path
1309  x  split_path_core
1310  x  update_path 
1311  x  old_path_editor_core
1312 ENDHELPMSG
1313 }
1314 
1315 function help_function ()
1316 {
1317     [ "$#" = "0" ] && {
1318         cat << ENDHELPMSG
1319  <<< Help >>>
1320     help_myself [arguments ...]                            (helpm)
1321     help_function [functions ...]                          (helpf)
1322     help_obsolete
1323  x  print_iconv
1324  <<< Group: Background >>>
1325     bgrun command [arguments ...]                          (bgr)
1326     bglist [--full]                                        (bgl, bgls)
1327     bgview [number]                                        (bgv)
1328     bgclean [all | numbers ...]                            (bgrm)
1329     bgcount                                                (bgc)
1330     bgdu
1331  <<< Group: Trash >>>
1332     trash_mv [filenames ...] [-- sudo_prefix ...]     (trash_put, trash_add)
1333     trash_ls                                          (trash_list)
1334     trash_cd number
1335     trash_pushd number
1336     trash_recover numbers ... [-- sudo_prefix ...]    (trash_restore, trash_rc)
1337     trash_rm numbers ...                              (trash_drop, trash_clean)
1338     trash_count                                       (trash_ct)
1339     trash_du [-- sudo_prefix ...]
1340  <<< Group: New PATH Editor >>>
1341     path_editor [variable]
1342     ldpath_editor
1343  x  newpath_init
1344  x  newpath_gen
1345  <<< Group: Configuration File >>>
1346     configfile_fetch [file_name_list [file_version [system_type]]]
1347     configfile_initial_setup
1348  x  fetch_remote_file local_file_name remote_url
1349  <<< Other >>>
1350     backup_file filename ... [-- sudo_prefix ...]
1351     check_dmesg seconds
1352     check_system_status seconds
1353     check_important_files
1354     compile_all [-n] filenames ...
1355     convert_to_html filename ...
1356     editlink filenames ... [-- sudo_prefix ...]
1357     get_memory_info
1358     keep_sudo_credential [seconds]
1359     mkscreenacl usernames ...
1360     mvfile [-n] filenames ... [-- sudo_prefix ...]
1361     prehistory_backup
1362     set_console_title
1363     set_terminal_size
1364     varset variables ...
1365     unzip_nomac filenames ...
1366  x  createdir_askmode dirname
1367  x  is_file_type filename type [-- sudo_prefix ...]
1368  x  get_file_size filename [-- sudo_prefix ...]
1369  x  get_terminal_size
1370  x  split_arguments [arguments ...]
1371 
1372 Obsolete functions are not printed. Type \`help_obsolete' to print them.
1373 ENDHELPMSG
1374     } && return 0
1375     local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
1376     local -i i
1377     while [ "$1" ]
1378     do
1379         case "$1" in
1380             help_myself|helpm)
1381                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1382 help_myself
1383     一個測試命令列的小函式
1384 ENDHELPMSG
1385                 ;;
1386             help_function|helpf)
1387                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1388 help_function
1389     顯示 .bash_include 提供的額外函式清單
1390     註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
1391 ENDHELPMSG
1392                 ;;
1393             bgrun|bgr)
1394                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1395 bgrun command [arguments ...]
1396     執行指令 command 並將輸出導入檔案
1397     註:此函式會自動以目前時間和指令名稱為檔案命名
1398 ENDHELPMSG
1399                 ;;
1400             bglist|bgl|bgls)
1401                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1402 bglist [--full]
1403     列出所有使用 bgrun 執行的指令
1404     若加上 --full 選項,則可同時察看時間
1405 ENDHELPMSG
1406                 ;;
1407             bgview|bgv)
1408                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1409 bgview [number]
1410     顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
1411     若 number >  0,
1412         表示第 number 個指令 (此數值可由 bglist 函式取得)
1413     若 number <= 0,
1414         表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
1415 ENDHELPMSG
1416                 ;;
1417             bgclean|bgrm)
1418                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1419 bgclean [all | numbers ...]
1420     bgclean all 可清除所有指令的輸出檔
1421     bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
1422 ENDHELPMSG
1423                 ;;
1424             bgcount|bgc)
1425                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1426 bgcount
1427     顯示指令輸出檔總數
1428 ENDHELPMSG
1429                 ;;
1430             bgdu)
1431                 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1432 bgdu
1433     顯示每個指令輸出檔的檔案大小 (單位:KB)
1434 ENDHELPMSG
1435                 ;;
1436             *)
1437                 echo "Help message for $1 is not found"
1438                 ;;
1439         esac
1440         shift
1441     done
1442 }
1443 
1444 ########## Help End ##########
1445 
1446 # Doing something
1447 
1448 umask 0022
1449 
1450 if [ "$interactive_shell" ]
1451 then
1452     echo "Running interactive shell configuration"
1453     check_important_files
1454     startcolor
1455     prehistory_backup
1456     bind '"\e[A":history-search-backward'
1457     bind '"\e[B":history-search-forward'
1458     if [ -z "$PROMPT_COMMAND" ] && [ -e "$HOME/.bash_title" ]; then
1459         case "$TERM" in
1460             xterm*)
1461                 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
1462                 ;;
1463             screen*)
1464                 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
1465                 ;;
1466         esac
1467     fi
1468     [ "$TERM" = xterm ] && TERM=xterm-256color
1469     [ "$TERM" = screen ] && TERM=screen-256color
1470 fi
1471 
1472 if [ "`uname`" = "Linux" ]
1473 then
1474     [ "$interactive_shell" ] && echo "Setting special things for Linux"
1475     REALPATH_PROGRAM="readlink -f"
1476     ulimit -c unlimited
1477 fi
1478 
1479 [ "$interactive_shell" ] && echo "Setting shell options"
1480 
1481 shopt -s histappend
1482 shopt -s checkwinsize
1483 shopt -s checkjobs
1484 shopt -s checkhash
1485 shopt -s cmdhist
1486 shopt -s mailwarn
1487 
1488 [ "$interactive_shell" ] && {
1489     if [ "$WINDOW" ] && type screen &> /dev/null; then
1490         if [[ "`screen --version`" == *4.01* ]]; then
1491             echo "Setting options for GNU screen 4.1.0"
1492             screen -X cjkwidth off
1493         fi
1494     fi
1495 }
1496 
1497 [ "$interactive_shell" ] && {
1498     echo "Done"
1499     if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
1500     then
1501         printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
1502     fi
1503 }
1504