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