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