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