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