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