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 backup_file ()
928 {
929 split_arguments "$@"
930 local current_time=`date +%Y%m%d`
931 local rootfilename
932 local -i i=0
933 local -i j
934 while [ "${arglist[$i]}" ]
935 do
936 if [ '!' -f "${arglist[$i]}" ]
937 then
938 printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
939 i=$i+1
940 continue
941 fi
942 rootfilename="${arglist[$i]}.$current_time"
943 if [ -e "$rootfilename" ]
944 then
945 j=0
946 while [ "$j" -lt "10" ]
947 do
948 if [ -e "$rootfilename.$j" ]
949 then
950 j=$j+1
951 continue
952 else
953 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
954 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
955 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
956 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
957 break
958 fi
959 done
960 if [ '!' "$j" -lt "10" ]
961 then
962 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
963 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
964 fi
965 else
966 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
967 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
968 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
969 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
970 fi
971 i=$i+1
972 done
973 unset arglist
974 unset prefixlist
975 }
976
977 function keep_sudo_credential ()
978 {
979 if [ "$1" ]
980 then
981 update_sudo_interval="$1"
982 else
983 update_sudo_interval="280"
984 fi
985 while true
986 do
987 sudo -v
988 sleep "$update_sudo_interval"
989 done
990 }
991
992 function get_memory_info ()
993 {
994 if [ "`uname`" = "Linux" ]
995 then
996 local meminfoline="`free -m | \grep -i mem`"
997 local swapinfoline="`free -m | \grep -i swap`"
998 local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
999 local memused="`echo "$meminfoline" | awk '{print $3}'`"
1000 local membuf="`echo "$meminfoline" | awk '{print $6}'`"
1001 local memcache="`echo "$meminfoline" | awk '{print $7}'`"
1002 local memprog=$(($memused-$membuf-$memcache))
1003 local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
1004 local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
1005 echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
1006 echo "Detail:"
1007 echo " Used: `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
1008 echo " Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
1009 echo " Cached: `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
1010 if [ "$swaptotal" = "0" ]
1011 then
1012 echo "Swap: not available"
1013 else
1014 echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
1015 fi
1016 else
1017 echo "Current operating system is not Linux."
1018 fi
1019 }
1020
1021 function set_console_title ()
1022 {
1023 case "$TERM" in
1024 screen)
1025 printf "\033]0;"
1026 echo -n "$*"
1027 printf "\033\\"
1028 ;;
1029 xterm*)
1030 printf "\033]0;"
1031 echo -n "$*"
1032 printf "\007"
1033 ;;
1034 *)
1035 echo "Your terminal may not have the hardstatus line."
1036 echo "Note: TERM=$TERM"
1037 ;;
1038 esac
1039 }
1040
1041 function mvfile ()
1042 {
1043 local nocheck=0
1044 [ "$1" = "-n" ] && nocheck=1 && shift
1045 split_arguments "$@"
1046 local -i i=0
1047 while [ "${arglist[$i]}" ]
1048 do
1049 if [ "$nocheck" = "0" ] && [ '!' -e "${arglist[$i]}" ]
1050 then
1051 printf "\e[33mWarning\e[m: ${arglist[$i]} does not exist. (Use -n to override)\n"
1052 i=$i+1
1053 continue
1054 fi
1055 echo "Old name: ${arglist[$i]}"
1056 read -p "New name: " -e -i "${arglist[$i]}" new_file_name
1057 if [ "$new_file_name" ] && [ "${arglist[$i]}" != "$new_file_name" ]
1058 then
1059 "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
1060 history -s "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
1061 fi
1062 i=$i+1
1063 done
1064 unset arglist
1065 unset prefixlist
1066 unset new_file_name
1067 }
1068
1069 function createdir_askmode ()
1070 {
1071 newdir_mode="$2"
1072 if mkdir -p "$1"
1073 then
1074 echo "Directory $1 is created."
1075 printf "Change the mode of the directory... "
1076 read -i "$newdir_mode" -p ">>> Mode: " -e newdir_mode
1077 chmod "$newdir_mode" "$1"
1078 else
1079 echo "Cannot create directory $1!"
1080 return 1
1081 fi
1082 }
1083
1084
1085
1086 alias helpf='help_function'
1087 alias helpm='help_myself'
1088
1089 function print_iconv ()
1090 {
1091 [ "$1" = "$2" ] && cat && return 0
1092 iconv -f "$1" -t "$2"
1093 }
1094
1095 function help_myself ()
1096 {
1097 echo "argc = $#"
1098 echo "argv[0] = $0"
1099 i=1
1100 while [ "$1" ]
1101 do
1102 echo "argv[$i] = $1"
1103 i=$(($i+1))
1104 shift
1105 done
1106 }
1107
1108 function help_obsolete ()
1109 {
1110 cat << ENDHELPMSG
1111 @@@ Obsolete Group: PATH Editor @@@
1112 old_path_editor
1113 old_ldpath_editor
1114 x split_path
1115 x split_path_core
1116 x update_path
1117 x old_path_editor_core
1118 ENDHELPMSG
1119 }
1120
1121 function help_function ()
1122 {
1123 [ "$#" = "0" ] && {
1124 cat << ENDHELPMSG
1125 <<< Help >>>
1126 help_myself [arguments ...] (helpm)
1127 help_function [functions ...] (helpf)
1128 help_obsolete
1129 x print_iconv
1130 <<< Group: Background >>>
1131 bgrun command [arguments ...] (bgr)
1132 bglist [--full] (bgl, bgls)
1133 bgview [number] (bgv)
1134 bgclean [all | numbers ...] (bgrm)
1135 bgcount (bgc)
1136 bgdu
1137 <<< Group: Trash >>>
1138 trash_mv [filenames ...] [-- sudo_prefix ...] (trash_put, trash_add)
1139 trash_ls (trash_list)
1140 trash_cd number
1141 trash_pushd number
1142 trash_recover numbers ... [-- sudo_prefix ...] (trash_restore, trash_rc)
1143 trash_rm numbers ... (trash_drop, trash_clean)
1144 trash_count (trash_ct)
1145 trash_du [-- sudo_prefix ...]
1146 <<< Group: New PATH Editor >>>
1147 path_editor [variable]
1148 ldpath_editor
1149 x newpath_init
1150 x newpath_gen
1151 <<< Other >>>
1152 backup_file filename ... [-- sudo_prefix ]
1153 check_dmesg seconds
1154 check_system_status seconds
1155 check_important_files
1156 compile_all [-n] filenames ...
1157 convert_to_html filename ...
1158 get_memory_info
1159 keep_sudo_credential [seconds]
1160 mkscreenacl usernames ...
1161 mvfile [-n] filenames ... [-- sudo_prefix]
1162 prehistory_backup
1163 set_console_title
1164 var_set variables ...
1165 x createdir_askmode dirname
1166 x split_arguments [arguments ...]
1167 x get_file_size filename [-- sudo_prefix]
1168
1169 Obsolete functions are not printed. Type \`help_obsolete' to print them.
1170 ENDHELPMSG
1171 } && return 0
1172 local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
1173 local -i i
1174 while [ "$1" ]
1175 do
1176 case "$1" in
1177 help_myself|helpm)
1178 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1179 help_myself
1180 一個測試命令列的小函式
1181 ENDHELPMSG
1182 ;;
1183 help_function|helpf)
1184 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1185 help_function
1186 顯示 .bash_include 提供的額外函式清單
1187 註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
1188 ENDHELPMSG
1189 ;;
1190 bgrun|bgr)
1191 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1192 bgrun command [arguments ...]
1193 執行指令 command 並將輸出導入檔案
1194 註:此函式會自動以目前時間和指令名稱為檔案命名
1195 ENDHELPMSG
1196 ;;
1197 bglist|bgl|bgls)
1198 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1199 bglist [--full]
1200 列出所有使用 bgrun 執行的指令
1201 若加上 --full 選項,則可同時察看時間
1202 ENDHELPMSG
1203 ;;
1204 bgview|bgv)
1205 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1206 bgview [number]
1207 顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
1208 若 number > 0,
1209 表示第 number 個指令 (此數值可由 bglist 函式取得)
1210 若 number <= 0,
1211 表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
1212 ENDHELPMSG
1213 ;;
1214 bgclean|bgrm)
1215 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1216 bgclean [all | numbers ...]
1217 bgclean all 可清除所有指令的輸出檔
1218 bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
1219 ENDHELPMSG
1220 ;;
1221 bgcount|bgc)
1222 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1223 bgcount
1224 顯示指令輸出檔總數
1225 ENDHELPMSG
1226 ;;
1227 bgdu)
1228 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
1229 bgdu
1230 顯示每個指令輸出檔的檔案大小 (單位:KB)
1231 ENDHELPMSG
1232 ;;
1233 *)
1234 echo "Help message for $1 is not found"
1235 ;;
1236 esac
1237 shift
1238 done
1239 }
1240
1241
1242
1243
1244
1245 umask 0022
1246
1247 if [ "$interactive_shell" ]
1248 then
1249 echo "Running interactive shell configuration"
1250 check_important_files
1251 startcolor
1252 prehistory_backup
1253 bind '"\e[A":history-search-backward'
1254 bind '"\e[B":history-search-forward'
1255 if [ -z "$PROMPT_COMMAND" ] && [ -e "$HOME/.bash_title" ]; then
1256 case "$TERM" in
1257 xterm*)
1258 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
1259 ;;
1260 screen)
1261 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
1262 ;;
1263 esac
1264 fi
1265 fi
1266
1267 if [ "`uname`" = "Linux" ]
1268 then
1269 [ "$interactive_shell" ] && echo "Setting special things for Linux"
1270 REALPATH_PROGRAM="readlink -f"
1271 ulimit -c unlimited
1272 fi
1273
1274 [ "$interactive_shell" ] && echo "Setting shell options"
1275
1276 shopt -s histappend
1277 shopt -s checkwinsize
1278 shopt -s checkjobs
1279 shopt -s checkhash
1280 shopt -s cmdhist
1281 shopt -s mailwarn
1282
1283 [ "$interactive_shell" ] && {
1284 echo "Done"
1285 if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
1286 then
1287 printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
1288 fi
1289 }
1290