1
2
3
4
5
6
7
8
9 if [[ "$-" == *i* ]] ; then interactive_shell=1; fi
10
11 [ "$interactive_shell" ] && echo "Running .bash_include"
12 [ "$interactive_shell" ] && default_tty_setting="`stty -g`"
13
14
15
16 colorprompting='\[\e[1;31m\]\!\[\e[m\] [\[\e[1;33m\]\u\[\e[m\]@\[\e[1;32m\]\h\[\e[m\] \[\e[1;36m\]\w\[\e[m\]]\$ '
17 nocolorprompting='\! [\u@\h \w]\$ '
18
19 if [ "$SSH_CONNECTION" ]
20 then
21 colorprompting="\[\e[1;44m\]*\[\e[m\]$colorprompting"
22 nocolorprompting="*$nocolorprompting"
23 fi
24
25 historycountfile="$HOME/.bash_history.count"
26 historybackupfile="$HOME/.bash_history.bak"
27 bgrunfiledir="$HOME/tmp/bgrun-`whoami`"
28 trashdir="$HOME/trash"
29
30 HISTSIZE=1000000
31 HISTFILESIZE=1000000
32 HISTCONTROL=ignoredups:ignorespace
33 HISTTIMEFORMAT="%F %T "
34
35 REALPATH_PROGRAM="realpath"
36
37 CFLAGS="-Wall -pipe -g"
38
39
40
41
42 export EDITOR=vim
43 export FCEDIT=vim
44 export PAGER=less
45
46
47
48
49
50
51 alias startcolor='PS1=$colorprompting'
52 alias stopcolor='PS1=$nocolorprompting'
53
54 alias ll='ls -l'
55 alias grep='grep --color=always'
56 alias rm='rm -i'
57 alias cp='cp -pi'
58 alias mv='mv -i'
59 alias jobs='jobs -l'
60 alias less='less -RS'
61
62 alias cccc='LANG=C;LANGUAGE=C;LC_ALL=C'
63 alias enus='LANG=en_US.UTF-8;LANGUAGE=en_US:en;LC_ALL=en_US.UTF-8'
64 alias big5='LANG=zh_TW.Big5;LANGUAGE=zh_TW:zh;LC_ALL=zh_TW.Big5'
65 alias zhtw='LANG=zh_TW.UTF-8;LANGUAGE=zh_TW:zh;LC_ALL=zh_TW.UTF-8'
66
67 alias savetty='default_tty_setting=`stty -g`'
68 alias resetty='stty $default_tty_setting'
69
70
71
72
73
74
75 function compile_all ()
76 {
77 local noask=0
78 [ "$1" = '' ] && echo "Which file(s) do you want to compile? " && return 1
79 [ "$1" = "-n" ] && noask=1
80 if [ "$noask" = "0" ]; then
81 read -e -p "CFLAGS: " -i "$CFLAGS" NEWCFLAGS
82 read -e -p "LDFLAGS: " -i "$LDFLAGS" NEWLDFLAGS
83 [ "$NEWCFLAGS" '!=' '' ] && CFLAGS=$NEWCFLAGS
84 [ "$NEWLDFLAGS" '!=' '' ] && LDFLAGS=$NEWLDFLAGS
85 else
86 shift
87 fi
88 while [ "$1" '!=' '' ]
89 do
90 local TARGETFILE="`echo "$1" | cut -d . -f 1`"
91 local SUFFIX="`echo "$1" | cut -d . -f 2`"
92 if [ -f "$1" ]; then
93 true
94 else
95 printf\
96 "\e[1;33mWarning\e[0m: $1 Non-existent file or not a regular file\n"
97 shift ; continue
98 fi
99 [ "$TARGETFILE" = "$1" ] && shift && continue
100 if [ "$SUFFIX" = "c" ]; then
101 echo "[CC] $1 -> $TARGETFILE"
102 gcc $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
103 elif [ "$SUFFIX" = "cpp" ]; then
104 echo "[CXX] $1 -> $TARGETFILE"
105 g++ $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
106 else
107 printf "$1: Unknown suffix (\e[1;33mskipped\e[0m)\n"
108 fi
109 [ "$?" '!=' "0" ] && printf\
110 '\e[1;31mError\e[0m while compiling file\n'
111 shift
112 done
113 return 0
114 }
115
116
117 function convert_to_html ()
118 {
119 while [ "$1" '!=' '' ]
120 do
121 for i in "$1"
122 do
123 vim $i -c 'set background=dark' \
124 -c 'highlight PreProc ctermfg=darkcyan' \
125 -c "$BEFORE_CONVERT_TO_HTML" \
126 -c "$BEFORE_CONVERT_TO_HTML1" \
127 -c "$BEFORE_CONVERT_TO_HTML2" \
128 -c TOhtml \
129 -c :w \
130 -c :qa
131 done
132 shift
133 done
134 }
135
136 function mkscreenacl ()
137 {
138 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"
139 while [ "$1" '!=' '' ]
140 do
141 for i in $PERMIT_COMMAND
142 do
143 echo "aclchg $1 +x $i"
144 done
145 echo "aclchg $1 -rw \"#?\""
146 shift
147 done
148 }
149
150
151
152 alias bgr=bgrun
153 alias bgv=bgview
154 alias bgl=bglist
155 alias bgc=bgcount
156 alias bgls=bglist
157 alias bgrm=bgclean
158
159 function bgrun ()
160 {
161 [ "$#" = "0" ] && return 1
162 [ '!' -d "$bgrunfiledir" ] && createdir_askmode "$bgrunfiledir" 0750
163 local current_time=`date "+%Y%m%d-%H%M%S"`
164 local cmdname=`echo "$1" | sed -e 's/-/_/g' -e 's/\\//_/g' -e 's/ /_/g'`
165 if [ "`echo "$cmdname" | cut -c 1`" == "_" ]
166 then
167 cmdname=`echo "$cmdname" | cut -c 2-`
168 fi
169 local filename="$bgrunfiledir/$current_time-$cmdname"
170 echo "Writing to $filename"
171 {
172 echo -n "$BASHPID " > "$filename"
173 echo "$@" >> "$filename"
174 exec "$@" &>> "$filename"
175 } &
176 }
177
178 function bglist ()
179 {
180 local viewtime=0
181 [ "$1" = "--full" ] && viewtime=1
182 {
183 for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort`
184 do
185 [ "$viewtime" = "1" ] && echo "$i"
186 head -n 1 "$i" | {
187 local procpid
188 local cmdline
189 read -d ' ' procpid
190 read cmdline
191 printf "(%5d) %s\n" "$procpid" "$cmdline"
192 }
193 done
194 } | {
195 if [ "$viewtime" = "1" ]
196 then
197 echo " INDEX TIME PID COMMAND"
198 local readstat=0
199 local -i i=1
200 while true
201 do
202 local dateandtime_long
203 local cmdline
204 read dateandtime_long
205 read cmdline
206 [ "$?" '!=' "0" ] && break
207 local dateandtime=`basename "$dateandtime_long"`
208
209
210
211
212
213
214 echo "$dateandtime" | {
215 read -n 4 part_year
216 read -n 2 part_month
217 read -n 2 part_date
218 read -n 1 drop_this_char; unset drop_this_char
219 read -n 2 part_hour
220 read -n 2 part_minute
221 read -n 2 part_second
222 printf '%6d' "$i"
223 echo " $part_year-$part_month-$part_date $part_hour:$part_minute:$part_second $cmdline"
224 }
225 i=$i+1
226 done
227 else
228 echo " INDEX PID COMMAND"
229 cat -n
230 fi
231 } | $PAGER
232 }
233
234 function bgview ()
235 {
236 local -i yourchoice
237 if [ "$1" = "" ]
238 then
239 yourchoice=`bgcount`
240 else
241 if [ "$1" -le "0" ]
242 then
243 yourchoice=$((`bgcount`+$1))
244 else
245 yourchoice=$1
246 fi
247 fi
248 echo "Your choice is $yourchoice."
249 local realfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${yourchoice}p`
250 head -n 1 "$realfilename" | {
251 read -d ' ' procpid
252 read cmdline
253 echo "PID: $procpid"
254 echo "Command Line: $cmdline"
255 }
256 read -e -p "View '$realfilename' ? " confirm
257 if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]
258 then
259 return 1
260 fi
261 {
262 printf "===> Process Information: "
263 cat "$realfilename"
264 } | $PAGER
265 }
266
267 function bgcount ()
268 {
269 find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | wc | awk '{print $2}'
270 }
271
272 function bgclean ()
273 {
274 if [ "$1" = "all" ]
275 then
276 echo "Removing the directory $bgrunfiledir"
277 rm -rf "$bgrunfiledir" &
278 return 0
279 else
280 split_arguments "$@"
281 local -i i=0
282 while [ "${arglist[$i]}" ]
283 do
284 arglist[$i]="-e ${arglist[$i]}p"
285 i=$i+1
286 done
287 local oneline
288 find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${arglist[*]} | {
289 while read oneline
290 do
291 echo "Removing $oneline"
292 rm -f "$oneline"
293 done
294 }
295 fi
296 unset arglist
297 unset prefixlist
298 }
299
300 function bgdu ()
301 {
302 du -a "$bgrunfiledir"
303 }
304
305
306
307 function check_dmesg ()
308 {
309 [ "$#" = "0" ] && return 1
310
311 while true
312 do
313 PREVIOS_DMESG_BUF="$DMESG_BUF"
314 DMESG_BUF="`dmesg`"
315 [ "$PREVIOS_DMESG_BUF" '!=' "$DMESG_BUF" ] && [ "$FIRST_RUN" = "0" ] && echo '===> You should check the system message buffer <==='
316 sleep $1
317 [ "$?" '!=' "0" ] && return 1
318 FIRST_RUN=0
319 done
320 }
321
322 function prehistory_backup ()
323 {
324 echo "Checking your current history file"
325 local -i currentcount=`cat "$HISTFILE" | wc -l`
326 [ '!' -f "$historycountfile" ] && touch "$historycountfile"
327 local -i previoushistorycount
328 previoushistorycount=`cat "$historycountfile"`
329 if [ "$currentcount" -lt "$previoushistorycount" ]
330 then
331 printf "\e[1;31mWarning\e[m: Your $HISTFILE may be TRUNCATED OR OVERWRITTEN BY OTHER PROGRAMS!\n"
332 printf "Note: \e[1;33m$currentcount\e[m < $previoushistorycount\n"
333 echo "Your $historycountfile and $historybackupfile will not be overwritten until this problem is fixed."
334 echo " 1. Check your $HISTFILE."
335 echo " 2. Edit your $HISTFILE manually if some unexpected changes are found."
336 echo " (You may need $historybackupfile to do it) "
337 echo " 3. Remove the file $historycountfile."
338 echo " 4. Run the command \`prehistory_backup' again."
339 return 3
340 fi
341 echo -n "Backing up your current history file ($previoushistorycount -> $currentcount, "
342 if [ "$previoushistorycount" = "$currentcount" ]
343 then
344 echo "no modification)"
345 else
346 echo "+$(($currentcount-$previoushistorycount)))"
347 fi
348 echo "$currentcount" > "$historycountfile"
349 \cp -f "$HISTFILE" "$historybackupfile"
350 }
351
352
353
354 alias trash_put=trash_mv
355 alias trash_add=trash_mv
356 alias trash_list=trash_ls
357 alias trash_ct=trash_count
358 alias trash_restore=trash_recover
359 alias trash_rc=trash_recover
360 alias trash_drop=trash_rm
361 alias trash_clean=trash_rm
362
363 function trash_mv ()
364 {
365 [ "$#" = "0" ] && return 1
366 [ '!' -d "$trashdir" ] && createdir_askmode "$trashdir" 0700
367 local original_path
368 local current_time
369 local -i i=0
370 split_arguments "$@"
371 while [ "${arglist[$i]}" ]
372 do
373 original_path="`$REALPATH_PROGRAM "${arglist[$i]}"`"
374 current_time=`date "+%Y%m%d-%H%M%S"`
375 better_time=`date "+%Y-%m-%d %H:%M:%S"`
376 dirname="`basename "${arglist[$i]}" | sed -e 's/-/_/g' -e 's/ /_/g'`"
377 fulldirname="$trashdir/$current_time-$dirname"
378 mkdir -p "$fulldirname"
379 echo "Move: ${arglist[$i]} -> $fulldirname"
380 "${prefixlist[@]}" mv "${arglist[$i]}" "$fulldirname"
381 if [ "$?" = "0" ]
382 then
383 echo "$better_time" > "$fulldirname/information.date"
384 echo "$original_path" > "$fulldirname/information.path"
385 else
386 rmdir "$fulldirname"
387 fi
388 i=$i+1
389 shift
390 done
391 unset arglist
392 unset prefixlist
393 }
394
395 function trash_rm ()
396 {
397 split_arguments "$@"
398 local -i i=0
399 while [ "${arglist[$i]}" ]
400 do
401 arglist[$i]="-e ${arglist[$i]}p"
402 i=$i+1
403 done
404 trash_dirname=`find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} `
405 echo 'Type rm -rf $trash_dirname to remove them.'
406 unset arglist
407 unset prefixlist
408 }
409
410 function trash_ls ()
411 {
412 local -i i=1
413 local oneline
414 find "$trashdir" -mindepth 1 -maxdepth 1 | sort | {
415 while read oneline
416 do
417 printf "%6d %s %s\n" "$i" \
418 "`cat "$oneline/information.date"`" \
419 "`cat "$oneline/information.path"`"
420 i=$i+1
421 done
422 } | $PAGER
423 }
424
425 function trash_pushd ()
426 {
427 [ -z "$1" ] && return 1
428 pushd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
429 }
430
431 function trash_cd ()
432 {
433 [ -z "$1" ] && return 1
434 cd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
435 }
436
437 function trash_recover ()
438 {
439 [ -z "$1" ] && return 1
440 split_arguments "$@"
441 local -i i=0
442 while [ "${arglist[$i]}" ]
443 do
444 arglist[$i]="-e ${arglist[$i]}p"
445 i=$i+1
446 done
447 find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} | {
448 while read oneline
449 do
450 local fromfile="$oneline/`basename "$(cat "$oneline/information.path")"`"
451 local tofile="`dirname "$(cat "$oneline/information.path")"`"
452 if [ -e "`cat "$oneline/information.path"`" ]
453 then
454 echo "Destination file exists."
455 continue
456 fi
457 echo "Move: $fromfile -> $tofile"
458 "${prefixlist[@]}" mv -f "$fromfile" "$tofile"
459 if [ "$?" = "0" ]
460 then
461 echo "Remove: $oneline"
462 \rm -rf "$oneline"
463 fi
464 done
465 }
466 unset arglist
467 unset prefixlist
468 }
469
470 function trash_count ()
471 {
472 find "$trashdir" -mindepth 1 -maxdepth 1 | wc | awk '{print $2}'
473 }
474
475 function trash_du ()
476 {
477 split_arguments "$@"
478 local oneline
479 find "$trashdir" -maxdepth 1 -mindepth 1 | sort | {
480 while read oneline
481 do
482 echo "'$oneline'"
483 done
484 } | xargs -n 10 "${prefixlist[@]}" du -s
485 unset arglist
486 unset prefixlist
487 }
488
489
490
491 function split_arguments ()
492 {
493 local argcount=$#
494 local -i i=0
495 local prefix_start=0
496 while [ "$1" ]
497 do
498 if [ "$prefix_start" == "0" ]
499 then
500 if [ "$1" = "--" ]
501 then
502 prefix_start=1
503 i=0
504 shift
505 continue
506 else
507 arglist[$i]="$1"
508 fi
509 else
510 prefixlist[$i]="$1"
511 fi
512 i=$i+1
513 shift
514 done
515 }
516
517 function check_important_files ()
518 {
519 IMPORTANT_FILES="$HOME/.screenrc $HOME/.vimrc"
520 for i in $IMPORTANT_FILES
521 do
522 [ '!' -f "$i" ] && printf "\e[1;31mWarning\e[m: \e[1;33m$i\e[m does not exist.\n"
523 done
524 }
525
526
527
528 function split_path_core ()
529 {
530 echo "$current_path" | {
531 while read -d : oneline
532 do
533 [ '!' "$oneline" = '^' ] && echo "$oneline"
534 done
535 [ '!' "$oneline" = '^' ] && echo "$oneline"
536 }
537 unset oneline
538 }
539
540 function split_path ()
541 {
542 coproc split_path_core
543 readarray -t -u ${COPROC[0]} patharr
544 wait $COPROC_PID
545 }
546
547 function update_path ()
548 {
549 current_path=''
550 local -i i=0
551 local firsttime="yes"
552 while [ "${patharr[$i]}" ]
553 do
554 if [ '!' "${patharr[$i]}" = "^" ]
555 then
556 if [ "$firsttime" ]
557 then
558 firsttime=''
559 else
560 current_path+=':'
561 fi
562 current_path+="${patharr[$i]}"
563 fi
564 i=$i+1
565 done
566 }
567
568 function path_editor ()
569 {
570 path_editor_core
571 }
572
573 function ldpath_editor ()
574 {
575 path_editor_core ld
576 }
577
578 function path_editor_core ()
579 {
580 if [ "$1" = "ld" ]
581 then
582 export current_path="$LD_LIBRARY_PATH"
583 else
584 export current_path="$PATH"
585 fi
586 local should_continue="yes"
587 local command
588 local command_sub
589 local command_sub2
590 local -i i
591 while [ "$should_continue" ]
592 do
593 split_path
594 i=0
595 echo "========================================"
596 while [ "${patharr[$i]}" ]
597 do
598 echo "$i: ${patharr[$i]}"
599 i=$i+1
600 done
601 [ "$i" = '0' ] && echo "(Empty or not declared)"
602 echo "========================================"
603 read -e -p "[A]ppend/(D)elete/(E)dit/(M)ove/(R)eset/(Q)uit ? " command
604 case "$command" in
605 ''|A|a)
606 read -e -p "Type a new entry: " patharr[$i]
607 update_path
608 ;;
609 D|d)
610 read -e -p "Index: " command_sub
611 patharr[$command_sub]='^'
612 update_path
613 ;;
614 E|e)
615 read -e -p "Index: " command_sub
616 read -e -p "Modify this entry: " -i "${patharr[$command_sub]}" patharr[$command_sub]
617 update_path
618 ;;
619 M|m)
620 read -e -p "From: " command_sub
621 read -e -p "To: " command_sub2
622 swaptmp="${patharr[$command_sub]}"
623 patharr[$command_sub]="${patharr[$command_sub2]}"
624 patharr[$command_sub2]="$swaptmp"
625 unset swaptmp
626 update_path
627 ;;
628 R|r)
629 if [ "$1" = "ld" ]
630 then
631 current_path="$LD_LIBRARY_PATH"
632 else
633 current_path="$PATH"
634 fi
635 ;;
636 Q|q)
637 if [ "$1" = "ld" ]
638 then
639 export LD_LIBRARY_PATH="$current_path"
640 echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
641 history -s "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
642 else
643 export PATH="$current_path"
644 echo "PATH=$PATH"
645 history -s "PATH=$PATH"
646 fi
647 should_continue=''
648 ;;
649 *)
650 printf " \e[33m*** Unknown command ***\e[m \n"
651 ;;
652 esac
653 done
654 unset patharr
655 unset current_path
656 }
657
658
659
660 function backup_file ()
661 {
662 split_arguments "$@"
663 local current_time=`date +%Y%m%d`
664 local rootfilename
665 local -i i=0
666 local -i j
667 while [ "${arglist[$i]}" ]
668 do
669 if [ '!' -f "${arglist[$i]}" ]
670 then
671 printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
672 i=$i+1
673 continue
674 fi
675 rootfilename="${arglist[$i]}.$current_time"
676 if [ -e "$rootfilename" ]
677 then
678 j=0
679 while [ "$j" -lt "10" ]
680 do
681 if [ -e "$rootfilename.$j" ]
682 then
683 j=$j+1
684 continue
685 else
686 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
687 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
688 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
689 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
690 break
691 fi
692 done
693 if [ '!' "$j" -lt "10" ]
694 then
695 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
696 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
697 fi
698 else
699 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
700 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
701 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
702 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
703 fi
704 i=$i+1
705 done
706 unset arglist
707 unset prefixlist
708 }
709
710 function keep_sudo_credential ()
711 {
712 if [ "$1" ]
713 then
714 update_sudo_interval="$1"
715 else
716 update_sudo_interval="280"
717 fi
718 while true
719 do
720 sudo -v
721 sleep "$update_sudo_interval"
722 done
723 }
724
725 function get_memory_info ()
726 {
727 if [ "`uname`" = "Linux" ]
728 then
729 local meminfoline="`free -m | \grep -i mem`"
730 local swapinfoline="`free -m | \grep -i swap`"
731 local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
732 local memused="`echo "$meminfoline" | awk '{print $3}'`"
733 local membuf="`echo "$meminfoline" | awk '{print $6}'`"
734 local memcache="`echo "$meminfoline" | awk '{print $7}'`"
735 local memprog=$(($memused-$membuf-$memcache))
736 local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
737 local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
738 echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
739 echo "Detail:"
740 echo " Used: `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
741 echo " Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
742 echo " Cached: `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
743 if [ "$swaptotal" = "0" ]
744 then
745 echo "Swap: not available"
746 else
747 echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
748 fi
749 else
750 echo "Current operating system is not Linux."
751 fi
752 }
753
754 function set_console_title ()
755 {
756 case "$TERM" in
757 screen)
758 printf "\033]0;"
759 echo -n "$*"
760 printf "\033\\"
761 ;;
762 xterm*)
763 printf "\033]0;"
764 echo -n "$*"
765 printf "\007"
766 ;;
767 *)
768 echo "Your terminal may not have the hardstatus line."
769 echo "Note: TERM=$TERM"
770 ;;
771 esac
772 }
773
774 function mvfile ()
775 {
776 local nocheck=0
777 [ "$1" = "-n" ] && nocheck=1 && shift
778 split_arguments "$@"
779 local -i i=0
780 while [ "${arglist[$i]}" ]
781 do
782 if [ "$nocheck" = "0" ] && [ '!' -e "${arglist[$i]}" ]
783 then
784 printf "\e[33mWarning\e[m: ${arglist[$i]} does not exist. (Use -n to override)\n"
785 i=$i+1
786 continue
787 fi
788 echo "Old name: ${arglist[$i]}"
789 read -p "New name: " -e -i "${arglist[$i]}" new_file_name
790 if [ "$new_file_name" ] && [ "${arglist[$i]}" != "$new_file_name" ]
791 then
792 "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
793 history -s "${prefixlist[@]}" mv -iv "${arglist[$i]}" "$new_file_name"
794 fi
795 i=$i+1
796 done
797 unset arglist
798 unset prefixlist
799 unset new_file_name
800 }
801
802 function createdir_askmode ()
803 {
804 newdir_mode="$2"
805 if mkdir -p "$1"
806 then
807 echo "Directory $1 is created."
808 printf "Change the mode of the directory... "
809 read -i "$newdir_mode" -p ">>> Mode: " -e newdir_mode
810 chmod "$newdir_mode" "$1"
811 else
812 echo "Cannot create directory $1!"
813 return 1
814 fi
815 }
816
817
818
819 alias helpf='help_function'
820 alias helpm='help_myself'
821
822 function print_iconv ()
823 {
824 [ "$1" = "$2" ] && cat && return 0
825 iconv -f "$1" -t "$2"
826 }
827
828 function help_myself ()
829 {
830 echo "argc = $#"
831 echo "argv[0] = $0"
832 i=1
833 while [ "$1" ]
834 do
835 echo "argv[$i] = $1"
836 i=$(($i+1))
837 shift
838 done
839 }
840
841 function help_function ()
842 {
843 [ "$#" = "0" ] && {
844 cat << ENDHELPMSG
845 <<< Help >>>
846 help_myself [arguments ...] (helpm)
847 help_function [functions ...] (helpf)
848 <<< Group: Background >>>
849 bgrun command [arguments ...] (bgr)
850 bglist [--full] (bgl, bgls)
851 bgview [number] (bgv)
852 bgclean [all | numbers ...] (bgrm)
853 bgcount (bgc)
854 bgdu
855 <<< Group: Trash >>>
856 trash_mv [filename ...] [-- sudo_prefix ...] (trash_put, trash_add)
857 trash_ls (trash_list)
858 trash_cd [number]
859 trash_pushd [number]
860 trash_recover [number] [-- sudo_prefix ...] (trash_restore, trash_rc)
861 trash_rm numbers ... (trash_drop, trash_clean)
862 trash_count (trash_ct)
863 trash_du [-- sudo_prefix ...]
864 <<< Group: PATH Editor >>>
865 path_editor
866 ldpath_editor
867 x split_path
868 x split_path_core
869 x update_path
870 x path_editor_core
871 <<< Other >>>
872 backup_file filename ... [-- sudo_prefix ]
873 check_dmesg seconds
874 check_important_files
875 compile_all [-n] filename ...
876 convert_to_html filename ...
877 keep_sudo_credential [seconds]
878 mkscreenacl username ...
879 mvfile [-n] filename ... [-- sudo_prefix]
880 prehistory_backup
881 set_console_title
882 x createdir_askmode dirname
883 x split_arguments [arguments ...]
884 ENDHELPMSG
885 } && return 0
886 local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
887 local -i i
888 while [ "$1" ]
889 do
890 case "$1" in
891 help_myself|helpm)
892 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
893 help_myself
894 一個測試命令列的小函式
895 ENDHELPMSG
896 ;;
897 help_function|helpf)
898 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
899 help_function
900 顯示 .bash_include 提供的額外函式清單
901 註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
902 ENDHELPMSG
903 ;;
904 bgrun|bgr)
905 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
906 bgrun command [arguments ...]
907 執行指令 command 並將輸出導入檔案
908 註:此函式會自動以目前時間和指令名稱為檔案命名
909 ENDHELPMSG
910 ;;
911 bglist|bgl|bgls)
912 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
913 bglist [--full]
914 列出所有使用 bgrun 執行的指令
915 若加上 --full 選項,則可同時察看時間
916 ENDHELPMSG
917 ;;
918 bgview|bgv)
919 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
920 bgview [number]
921 顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
922 若 number > 0,
923 表示第 number 個指令 (此數值可由 bglist 函式取得)
924 若 number <= 0,
925 表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
926 ENDHELPMSG
927 ;;
928 bgclean|bgrm)
929 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
930 bgclean [all | numbers ...]
931 bgclean all 可清除所有指令的輸出檔
932 bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
933 ENDHELPMSG
934 ;;
935 bgcount|bgc)
936 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
937 bgcount
938 顯示指令輸出檔總數
939 ENDHELPMSG
940 ;;
941 bgdu)
942 cat << ENDHELPMSG | print_iconv "UTF-8" "$current_charset"
943 bgdu
944 顯示每個指令輸出檔的檔案大小 (單位:KB)
945 ENDHELPMSG
946 ;;
947 *)
948 echo "Help message for $1 is not found"
949 ;;
950 esac
951 shift
952 done
953 }
954
955
956
957
958
959 umask 0022
960
961 if [ "$interactive_shell" ]
962 then
963 echo "Running interactive shell configuration"
964 check_important_files
965 startcolor
966 prehistory_backup
967 bind '"\e[A":history-search-backward'
968 bind '"\e[B":history-search-forward'
969 if [ -z "$PROMPT_COMMAND" ] && [ -e "$HOME/.bash_title" ]; then
970 case "$TERM" in
971 xterm*)
972 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
973 ;;
974 screen)
975 PROMPT_COMMAND='printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
976 ;;
977 esac
978 fi
979 fi
980
981 if [ "`uname`" = "Linux" ]
982 then
983 [ "$interactive_shell" ] && echo "Setting special things for Linux"
984 REALPATH_PROGRAM="readlink -f"
985 ulimit -c unlimited
986 fi
987
988 [ "$interactive_shell" ] && echo "Setting shell options"
989
990 shopt -s histappend
991 shopt -s checkwinsize
992 shopt -s checkjobs
993 shopt -s checkhash
994 shopt -s cmdhist
995 shopt -s mailwarn
996
997 [ "$interactive_shell" ] && {
998 echo "Done"
999 if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
1000 then
1001 printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
1002 fi
1003 }
1004