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