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=2147483647
55 HISTFILESIZE=2147483647
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 old_path_editor ()
655 {
656 old_path_editor_core
657 }
658
659 function old_ldpath_editor ()
660 {
661 old_path_editor_core ld
662 }
663
664 function old_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
747
748 function ldpath_editor ()
749 {
750 path_editor LD_LIBRARY_PATH
751 }
752
753 function newpath_init ()
754 {
755 unset newpath
756 local pathsp="$pathorgval"
757 local pathentry
758 local -i i
759 eval pathsp='"${pathsp// /\\}"'
760 pathsp="${pathsp//:/ }"
761 i=0
762 for pathentry in $pathsp
763 do
764 newpath[$i]="${pathentry//\\/ }"
765 i=$i+1
766 done
767 }
768
769 function newpath_gen ()
770 {
771 local -i i
772 pathmodval=""
773 i=0
774 while [ "${newpath[$i]}" ]
775 do
776 if [ "$i" != 0 ]; then pathmodval+=":"; fi
777 pathmodval+="${newpath[$i]}"
778 i=$i+1
779 done
780 }
781
782 function path_editor ()
783 {
784 local newpathvarname
785 local badcommand
786 local command
787 local command_sub
788 local command_sub2
789 local should_continue="yes"
790 local -i i
791
792 if [ -z "$1" ]
793 then
794 newpathvarname="PATH"
795 else
796 newpathvarname="$1"
797 fi
798
799 eval pathorgval=\${${newpathvarname}}
800 newpath_init
801
802 while [ "$should_continue" ]
803 do
804 i=0
805 echo -n $'\e[2J\e[0;0H'
806 if [ "$badcommand" = "yes" ]; then
807 printf " \e[33m*** Command \`$command' is unknown ***\e[m \n"
808 badcommand=""
809 fi
810 echo " New PATH Editor for BASH "
811 echo "========================================"
812 echo "Environment Variable: $newpathvarname"
813 echo "----------------------------------------"
814 while [ "${newpath[$i]}" ]
815 do
816 echo "$i: ${newpath[$i]}"
817 i=$i+1
818 done
819 [ "$i" = '0' ] && echo "(Empty or not declared)"
820 echo "========================================"
821 read -e -p "[A]ppend/(I)nsert/(D)elete/(E)dit/(M)ove/(S)wap/(R)eset/(Q)uit ? " command
822 case "$command" in
823 ''|A|a)
824 read -e -p "Type a new entry: " newpath[$i]
825 ;;
826 I|i)
827 read -e -p "Type a new entry: " command_sub
828 if [ -z "$command_sub" ]; then continue; fi
829 newpath_tmp=( "${newpath[@]}" )
830 unset newpath
831 newpath="$command_sub"
832 i=0
833 while [ "${newpath_tmp[$i]}" ]
834 do
835 newpath[$i+1]="${newpath_tmp[$i]}"
836 i=$i+1
837 done
838 unset newpath_tmp
839 ;;
840 D|d)
841 read -e -p "Index: " command_sub
842 if [ -z "$command_sub" ]; then continue; fi
843 i="$command_sub"
844 i=$i+1
845 while [ "${newpath[$i]}" ]
846 do
847 newpath[$i-1]="${newpath[$i]}"
848 i=$i+1
849 done
850 unset newpath[$i-1]
851 ;;
852 E|e)
853 read -e -p "Index: " command_sub
854 read -e -p "Modify this entry: " -i "${newpath[$command_sub]}" newpath[$command_sub]
855 ;;
856 M|m)
857 read -e -p "From: " command_sub
858 read -e -p "To: " command_sub2
859 if [ "$command_sub" -eq "$command_sub2" ]; then continue; fi
860 cmdsubval="${newpath[$command_sub]}"
861 if [ "$command_sub" -gt "$command_sub2" ]; then
862 i="$command_sub"
863 while [ "$i" -gt "$command_sub2" ]
864 do
865 newpath[$i]="${newpath[$i-1]}"
866 i=$i-1
867 done
868 newpath[$command_sub2]="$cmdsubval"
869 else
870 i="$command_sub"
871 while [ "$i" -lt "$command_sub2" ]
872 do
873 newpath[$i]="${newpath[$i+1]}"
874 i=$i+1
875 done
876 newpath[$command_sub2]="$cmdsubval"
877 fi
878 unset cmdsubval
879 ;;
880 S|s)
881 read -e -p "First entry: " command_sub
882 read -e -p "Second entry: " command_sub2
883 swaptmp="${newpath[$command_sub]}"
884 newpath[$command_sub]="${newpath[$command_sub2]}"
885 newpath[$command_sub2]="$swaptmp"
886 unset swaptmp
887 ;;
888 R|r)
889 read -e -p "Discard all changes [y/N] ? " command_sub
890 if [ "$command_sub" = "y" ]; then
891 eval pathorgval=\${${newpathvarname}}
892 newpath_init
893 fi
894 ;;
895 Q|q)
896 newpath_gen
897 eval export ${newpathvarname}=\"$pathmodval\"
898 echo "${newpathvarname}=$pathmodval"
899 history -s "${newpathvarname}=$pathmodval"
900 should_continue=''
901 ;;
902 *)
903 badcommand="yes"
904 ;;
905 esac
906 done
907 unset newpath
908 unset pathorgval
909 unset pathmodval
910 }
911
912
913
914 function varset ()
915 {
916 local varoldvalue
917 local varnewvalue
918 while [ "$1" ]
919 do
920 eval varoldvalue=\${$1}
921 read -r -e -p "$1=" -i "$varoldvalue" varnewvalue
922 eval $1=\"$varnewvalue\"
923 shift
924 done
925 }
926
927 function is_file_type ()
928 {
929 local filename="$1"
930 local typename="$2"
931 shift 2
932 [ "`"$@" find "$filename" -maxdepth 0 -type "$typename"`" ] && return 0
933 return 1
934 }
935
936 function editlink ()
937 {
938 local newdest
939 local orgdest
940 split_arguments "$@"
941 local -i i=0
942 while [ "${arglist[$i]}" ]
943 do
944 if is_file_type "${arglist[$i]}" "l" "${prefixlist[@]}"; then
945 orgdest="`${prefixlist[@]} readlink "${arglist[$i]}"`"
946 read -e -p "EditLink: ${arglist[$i]} -> " -i "$orgdest" newdest
947 else
948 printf "\e[1;33mWarning\e[m: ${arglist[$i]} is not a symbolic link.\n"
949 i=$i+1
950 continue
951 fi
952 if [ "$newdest" ] && [ "$newdest" '!=' "$orgdest" ]; then
953 "${prefixlist[@]}" rm -f "${arglist[$i]}"
954 "${prefixlist[@]}" ln -sv "$newdest" "${arglist[$i]}"
955 fi
956 i=$i+1
957 done
958 unset arglist
959 unset prefixlist
960 }
961
962 function backup_file ()
963 {
964 split_arguments "$@"
965 local current_time=`date +%Y%m%d`
966 local rootfilename
967 local -i i=0
968 local -i j
969 while [ "${arglist[$i]}" ]
970 do
971 if [ '!' -f "${arglist[$i]}" ]
972 then
973 printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
974 i=$i+1
975 continue
976 fi
977 rootfilename="${arglist[$i]}.$current_time"
978 if [ -e "$rootfilename" ]
979 then
980 j=0
981 while [ "$j" -lt "10" ]
982 do
983 if [ -e "$rootfilename.$j" ]
984 then
985 j=$j+1
986 continue
987 else
988 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
989 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
990 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
991 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
992 break
993 fi
994 done
995 if [ '!' "$j" -lt "10" ]
996 then
997 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
998 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
999 fi
1000 else
1001 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
1002 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
1003 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
1004 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
1005 fi
1006 i=$i+1
1007 done
1008 unset arglist
1009 unset prefixlist
1010 }
1011
1012 function keep_sudo_credential ()
1013 {
1014 if [ "$1" ]
1015 then
1016 update_sudo_interval="$1"
1017 else
1018 update_sudo_interval="280"
1019 fi
1020 while true
1021 do
1022 sudo -v
1023 sleep "$update_sudo_interval"
1024 done
1025 }
1026
1027 function get_memory_info ()
1028 {
1029 case "`uname`" in
1030 "Linux")
1031 local meminfoline="`free -m | \grep -i mem`"
1032 local swapinfoline="`free -m | \grep -i swap`"
1033 local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
1034 local memused="`echo "$meminfoline" | awk '{print $3}'`"
1035 local membuf="`echo "$meminfoline" | awk '{print $6}'`"
1036 local memcache="`echo "$meminfoline" | awk '{print $7}'`"
1037 local memprog=$(($memused-$membuf-$memcache))
1038 local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
1039 local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
1040 echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
1041 echo "Detail:"
1042 echo " Used: `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
1043 echo " Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
1044 echo " Cached: `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
1045 if [ "$swaptotal" = "0" ]
1046 then
1047 echo "Swap: not available"
1048 else
1049 echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
1050 fi
1051 ;;
1052 "FreeBSD")
1053 local mempagesize="`sysctl -n hw.pagesize`"
1054 local mempagecount="`sysctl -n hw.availpages`"
1055 local memactive="`sysctl -n vm.stats.vm.v_active_count`"
1056 local meminactive="`sysctl -n vm.stats.vm.v_inactive_count`"
1057 local memwire="`sysctl -n vm.stats.vm.v_wire_count`"
1058 local memcache="`sysctl -n vm.stats.vm.v_cache_count`"
1059 local memfree="`sysctl -n vm.stats.vm.v_free_count`"
1060 local swapenabled="`sysctl -n vm.swap_enabled`"
1061 echo "Memory (Active): `printf %5d $(($memactive*$mempagesize/1048576))` MB (`printf %2d $(($memactive*100/$mempagecount))`%)"
1062 echo "Memory (Inactive): `printf %5d $(($meminactive*$mempagesize/1048576))` MB (`printf %2d $(($meminactive*100/$mempagecount))`%)"
1063 echo "Memory (Wired): `printf %5d $(($memwire*$mempagesize/1048576))` MB (`printf %2d $(($memwire*100/$mempagecount))`%)"
1064 echo "Memory (Cache): `printf %5d $(($memcache*$mempagesize/1048576))` MB (`printf %2d $(($memcache*100/$mempagecount))`%)"
1065 echo "Memory (Free): `printf %5d $(($memfree*$mempagesize/1048576))` MB (`printf %2d $(($memfree*100/$mempagecount))`%)"
1066 echo "Total Memory: `printf %5d $(($mempagecount*$mempagesize/1048576))` MB"
1067 if [ "$swapenabled" = "1" ]; then
1068 echo ""
1069 echo "Swap devices:"
1070 swapinfo -m
1071 else
1072 echo "Swap: not enabled"
1073 fi
1074 ;;
1075 *)
1076 echo "Unsupported operating system."
1077 ;;
1078 esac
1079 }
1080
1081 function set_console_title ()
1082 {
1083 case "$TERM" in
1084 screen)
1085 printf "\033]0;"
1086 echo -n "$*"
1087 printf "\033\\"
1088 ;;
1089 xterm*)
1090 printf "\033]0;"
1091 echo -n "$*"
1092 printf "\007"
1093 ;;
1094 *)
1095 echo "Your terminal may not have the hardstatus line."
1096 echo "Note: TERM=$TERM"
1097 ;;
1098 esac
1099 }
1100
1101 function mvfile ()
1102 {
1103 local nocheck=0
1104 [ "$1" = "-n" ] && nocheck=1 && shift
1105 split_arguments "$@"
1106 local -i i=0
1107 while [ "${arglist[$i]}" ]
1108 do
1109 if [ "$nocheck" = "0" ] && [ '!' -e "${arglist[$i]}" ]
1110 then
1111 printf "\e[33mWarning\e[m: ${arglist[$i]} does not exist. (Use -n to override)\n"
1112 i=$i+1
1113 continue
1114 fi
1115 echo "Old name: ${arglist[$i]}"
1116 read -p "New name: " -e -i "${arglist[$i]}" new_file_name
1117 if [ "$new_file_name" ] && [ "${arglist[$i]}" != "$new_file_name" ]
1118 then
1119 "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
1120 history -s "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
1121 fi
1122 i=$i+1
1123 done
1124 unset arglist
1125 unset prefixlist
1126 unset new_file_name
1127 }
1128
1129 function createdir_askmode ()
1130 {
1131 newdir_mode="$2"
1132 if mkdir -p "$1"
1133 then
1134 echo "Directory $1 is created."
1135 printf "Change the mode of the directory... "
1136 read -i "$newdir_mode" -p ">>> Mode: " -e newdir_mode
1137 chmod "$newdir_mode" "$1"
1138 else
1139 echo "Cannot create directory $1!"
1140 return 1
1141 fi
1142 }
1143
1144
1145
1146 alias helpf='help_function'
1147 alias helpm='help_myself'
1148
1149 function print_iconv ()
1150 {
1151 [ "$1" = "$2" ] && cat && return 0
1152 iconv -f "$1" -t "$2"
1153 }
1154
1155 function help_myself ()
1156 {
1157 echo "argc = $#"
1158 echo "argv[0] = $0"
1159 i=1
1160 while [ "$1" ]
1161 do
1162 echo "argv[$i] = $1"
1163 i=$(($i+1))
1164 shift
1165 done
1166 }
1167
1168 function help_obsolete ()
1169 {
1170 cat << ENDHELPMSG
1171 @@@ Obsolete Group: PATH Editor @@@
1172 old_path_editor
1173 old_ldpath_editor
1174 x split_path
1175 x split_path_core
1176 x update_path
1177 x old_path_editor_core
1178 ENDHELPMSG
1179 }
1180
1181 function help_function ()
1182 {
1183 [ "$#" = "0" ] && {
1184 cat << ENDHELPMSG
1185 <<< Help >>>
1186 help_myself [arguments ...] (helpm)
1187 help_function [functions ...] (helpf)
1188 help_obsolete
1189 x print_iconv
1190 <<< Group: Background >>>
1191 bgrun command [arguments ...] (bgr)
1192 bglist [--full] (bgl, bgls)
1193 bgview [number] (bgv)
1194 bgclean [all | numbers ...] (bgrm)
1195 bgcount (bgc)
1196 bgdu
1197 <<< Group: Trash >>>
1198 trash_mv [filenames ...] [-- sudo_prefix ...] (trash_put, trash_add)
1199 trash_ls (trash_list)
1200 trash_cd number
1201 trash_pushd number
1202 trash_recover numbers ... [-- sudo_prefix ...] (trash_restore, trash_rc)
1203 trash_rm numbers ... (trash_drop, trash_clean)
1204 trash_count (trash_ct)
1205 trash_du [-- sudo_prefix ...]
1206 <<< Group: New PATH Editor >>>
1207 path_editor [variable]
1208 ldpath_editor
1209 x newpath_init
1210 x newpath_gen
1211 <<< Other >>>
1212 backup_file filename ... [-- sudo_prefix ...]
1213 check_dmesg seconds
1214 check_system_status seconds
1215 check_important_files
1216 compile_all [-n] filenames ...
1217 convert_to_html filename ...
1218 editlink filenames ... [-- sudo_prefix ...]
1219 get_memory_info
1220 keep_sudo_credential [seconds]
1221 mkscreenacl usernames ...
1222 mvfile [-n] filenames ... [-- sudo_prefix ...]
1223 prehistory_backup
1224 set_console_title
1225 varset variables ...
1226 x createdir_askmode dirname
1227 x is_file_type filename type [-- sudo_prefix ...]
1228 x get_file_size filename [-- sudo_prefix ...]
1229 x split_arguments [arguments ...]
1230
1231 Obsolete functions are not printed. Type \`help_obsolete' to print them.
1232 ENDHELPMSG
1233 } && return 0
1234 local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
1235 local -i i
1236 while [ "$1" ]
1237 do
1238 case "$1" in
1239 help_myself|helpm)
1240 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1241 help_myself
1242 一個測試命令列的小函式
1243 ENDHELPMSG
1244 ;;
1245 help_function|helpf)
1246 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1247 help_function
1248 顯示 .bash_include 提供的額外函式清單
1249 註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
1250 ENDHELPMSG
1251 ;;
1252 bgrun|bgr)
1253 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1254 bgrun command [arguments ...]
1255 執行指令 command 並將輸出導入檔案
1256 註:此函式會自動以目前時間和指令名稱為檔案命名
1257 ENDHELPMSG
1258 ;;
1259 bglist|bgl|bgls)
1260 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1261 bglist [--full]
1262 列出所有使用 bgrun 執行的指令
1263 若加上 --full 選項,則可同時察看時間
1264 ENDHELPMSG
1265 ;;
1266 bgview|bgv)
1267 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1268 bgview [number]
1269 顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
1270 若 number > 0,
1271 表示第 number 個指令 (此數值可由 bglist 函式取得)
1272 若 number <= 0,
1273 表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
1274 ENDHELPMSG
1275 ;;
1276 bgclean|bgrm)
1277 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1278 bgclean [all | numbers ...]
1279 bgclean all 可清除所有指令的輸出檔
1280 bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
1281 ENDHELPMSG
1282 ;;
1283 bgcount|bgc)
1284 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1285 bgcount
1286 顯示指令輸出檔總數
1287 ENDHELPMSG
1288 ;;
1289 bgdu)
1290 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1291 bgdu
1292 顯示每個指令輸出檔的檔案大小 (單位:KB)
1293 ENDHELPMSG
1294 ;;
1295 *)
1296 echo "Help message for $1 is not found"
1297 ;;
1298 esac
1299 shift
1300 done
1301 }
1302
1303
1304
1305
1306
1307 umask 0022
1308
1309 if [ "$interactive_shell" ]
1310 then
1311 echo "Running interactive shell configuration"
1312 check_important_files
1313 startcolor
1314 prehistory_backup
1315 bind '"\e[A":history-search-backward'
1316 bind '"\e[B":history-search-forward'
1317 if [ -z "$PROMPT_COMMAND" ] && [ -e "$HOME/.bash_title" ]; then
1318 case "$TERM" in
1319 xterm*)
1320 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
1321 ;;
1322 screen)
1323 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
1324 ;;
1325 esac
1326 fi
1327 fi
1328
1329 if [ "`uname`" = "Linux" ]
1330 then
1331 [ "$interactive_shell" ] && echo "Setting special things for Linux"
1332 REALPATH_PROGRAM="readlink -f"
1333 ulimit -c unlimited
1334 fi
1335
1336 [ "$interactive_shell" ] && echo "Setting shell options"
1337
1338 shopt -s histappend
1339 shopt -s checkwinsize
1340 shopt -s checkjobs
1341 shopt -s checkhash
1342 shopt -s cmdhist
1343 shopt -s mailwarn
1344
1345 [ "$interactive_shell" ] && {
1346 echo "Done"
1347 if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
1348 then
1349 printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
1350 fi
1351 }
1352