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