📄 sample-bashrc.html
字号:
299 { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ; } 300 # find pattern in a set of filesand highlight them: 301 302 function fstr() 303 { 304 OPTIND=1 305 local case="" 306 local usage="fstr: find string in files. 307 Usage: fstr [-i] \"pattern\" [\"filename pattern\"] " 308 while getopts :it opt 309 do 310 case "$opt" in 311 i) case="-i " ;; 312 *) echo "$usage"; return;; 313 esac 314 done 315 shift $(( $OPTIND - 1 )) 316 if [ "$#" -lt 1 ]; then 317 echo "$usage" 318 return; 319 fi 320 local SMSO=$(tput smso) 321 local RMSO=$(tput rmso) 322 find . -type f -name "${2:-*}" -print0 | 323 xargs -0 grep -sn ${case} "$1" 2>&- | \ 324 sed "s/$1/${SMSO}\0${RMSO}/gI" | more 325 } 326 327 function cuttail() # Cut last n lines in file, 10 by default. 328 { 329 nlines=${2:-10} 330 sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1 331 } 332 333 function lowercase() # move filenames to lowercase 334 { 335 for file ; do 336 filename=${file##*/} 337 case "$filename" in 338 */*) dirname==${file%/*} ;; 339 *) dirname=.;; 340 esac 341 nf=$(echo $filename | tr A-Z a-z) 342 newname="${dirname}/${nf}" 343 if [ "$nf" != "$filename" ]; then 344 mv "$file" "$newname" 345 echo "lowercase: $file --> $newname" 346 else 347 echo "lowercase: $file not changed." 348 fi 349 done 350 } 351 352 function swap() # swap 2 filenames around 353 { 354 local TMPFILE=tmp.$$ 355 mv "$1" $TMPFILE 356 mv "$2" "$1" 357 mv $TMPFILE "$2" 358 } 359 360 361 #----------------------------------- 362 # Process/system related functions: 363 #----------------------------------- 364 365 function my_ps() 366 { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; } 367 368 function pp() 369 { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; } 370 371 # This function is roughly the same as 'killall' on linux 372 # but has no equivalent (that I know of) on Solaris 373 function killps() # kill by process name 374 { 375 local pid pname sig="-TERM" # default signal 376 if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then 377 echo "Usage: killps [-SIGNAL] pattern" 378 return; 379 fi 380 if [ $# = 2 ]; then sig=$1 ; fi 381 for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} ) ; do 382 pname=$(my_ps | awk '$1~var { print $5 }' var=$pid ) 383 if ask "Kill process $pid <$pname> with signal $sig?" 384 then kill $sig $pid 385 fi 386 done 387 } 388 389 function my_ip() # get IP adresses 390 { 391 MY_IP=$(/sbin/ifconfig ppp0 | awk '/inet/ { print $2 } ' | \ 392 sed -e s/addr://) 393 MY_ISP=$(/sbin/ifconfig ppp0 | awk '/P-t-P/ { print $3 } ' | \ 394 sed -e s/P-t-P://) 395 } 396 397 function ii() # get current host related info 398 { 399 echo -e "\nYou are logged on ${RED}$HOST" 400 echo -e "\nAdditionnal information:$NC " ; uname -a 401 echo -e "\n${RED}Users logged on:$NC " ; w -h 402 echo -e "\n${RED}Current date :$NC " ; date 403 echo -e "\n${RED}Machine stats :$NC " ; uptime 404 echo -e "\n${RED}Memory stats :$NC " ; free 405 my_ip 2>&- ; 406 echo -e "\n${RED}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"} 407 echo -e "\n${RED}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"} 408 echo 409 } 410 411 # Misc utilities: 412 413 function repeat() # repeat n times command 414 { 415 local i max 416 max=$1; shift; 417 for ((i=1; i <= max ; i++)); do # --> C-like syntax 418 eval "$@"; 419 done 420 } 421 422 function ask() 423 { 424 echo -n "$@" '[y/n] ' ; read ans 425 case "$ans" in 426 y*|Y*) return 0 ;; 427 *) return 1 ;; 428 esac 429 } 430 431 #======================================================================= 432 # 433 # PROGRAMMABLE COMPLETION - ONLY SINCE BASH-2.04 434 # Most are taken from the bash 2.05 documentation and from Ian McDonalds 435 # 'Bash completion' package 436 # (http://www.caliban.org/bash/index.shtml#completion) 437 # You will in fact need bash-2.05a for some features 438 # 439 #======================================================================= 440 441 if [ "${BASH_VERSION%.*}" \< "2.05" ]; then 442 echo "You will need to upgrade to version 2.05 \ 443 for programmable completion" 444 return 445 fi 446 447 shopt -s extglob # necessary 448 set +o nounset # otherwise some completions will fail 449 450 complete -A hostname rsh rcp telnet rlogin r ftp ping disk 451 complete -A export printenv 452 complete -A variable export local readonly unset 453 complete -A enabled builtin 454 complete -A alias alias unalias 455 complete -A function function 456 complete -A user su mail finger 457 458 complete -A helptopic help # currently same as builtins 459 complete -A shopt shopt 460 complete -A stopped -P '%' bg 461 complete -A job -P '%' fg jobs disown 462 463 complete -A directory mkdir rmdir 464 complete -A directory -o default cd 465 466 # Compression 467 complete -f -o default -X '*.+(zip|ZIP)' zip 468 complete -f -o default -X '!*.+(zip|ZIP)' unzip 469 complete -f -o default -X '*.+(z|Z)' compress 470 complete -f -o default -X '!*.+(z|Z)' uncompress 471 complete -f -o default -X '*.+(gz|GZ)' gzip 472 complete -f -o default -X '!*.+(gz|GZ)' gunzip 473 complete -f -o default -X '*.+(bz2|BZ2)' bzip2 474 complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2 475 # Postscript,pdf,dvi..... 476 complete -f -o default -X '!*.ps' gs ghostview ps2pdf ps2ascii 477 complete -f -o default -X '!*.dvi' dvips dvipdf xdvi dviselect dvitype 478 complete -f -o default -X '!*.pdf' acroread pdf2ps 479 complete -f -o default -X '!*.+(pdf|ps)' gv 480 complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf 481 complete -f -o default -X '!*.tex' tex latex slitex 482 complete -f -o default -X '!*.lyx' lyx 483 complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps 484 # Multimedia 485 complete -f -o default -X '!*.+(jp*g|gif|xpm|png|bmp)' xv gimp 486 complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321 487 complete -f -o default -X '!*.+(ogg|OGG)' ogg123 488 489 490 491 complete -f -o default -X '!*.pl' perl perl5 492 493 # This is a 'universal' completion function - it works when commands have 494 # a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a' 495 496 _get_longopts () 497 { 498 $1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \ 499 grep ^"$2" |sort -u ; 500 } 501 502 _longopts_func () 503 { 504 case "${2:-*}" in 505 -*) ;; 506 *) return ;; 507 esac 508 509 case "$1" in 510 \~*) eval cmd="$1" ;; 511 *) cmd="$1" ;; 512 esac 513 COMPREPLY=( $(_get_longopts ${1} ${2} ) ) 514 } 515 complete -o default -F _longopts_func configure bash 516 complete -o default -F _longopts_func wget id info a2ps ls recode 517 518 519 _make_targets () 520 { 521 local mdef makef gcmd cur prev i 522 523 COMPREPLY=() 524 cur=${COMP_WORDS[COMP_CWORD]} 525 prev=${COMP_WORDS[COMP_CWORD-1]} 526 527 # if prev argument is -f, return possible filename completions. 528 # we could be a little smarter here and return matches against 529 # `makefile Makefile *.mk', whatever exists 530 case "$prev" in 531 -*f) COMPREPLY=( $(compgen -f $cur ) ); return 0;; 532 esac 533 534 # if we want an option, return the possible posix options 535 case "$cur" in 536 -) COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;; 537 esac 538 539 # make reads `makefile' before `Makefile' 540 if [ -f makefile ]; then 541 mdef=makefile 542 elif [ -f Makefile ]; then 543 mdef=Makefile 544 else 545 mdef=*.mk # local convention 546 fi 547 548 # before we scan for targets, see if a makefile name was specified 549 # with -f 550 for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do 551 if [[ ${COMP_WORDS[i]} == -*f ]]; then 552 eval makef=${COMP_WORDS[i+1]} # eval for tilde expansion 553 break 554 fi 555 done 556 557 [ -z "$makef" ] && makef=$mdef 558 559 # if we have a partial word to complete, restrict completions to 560 # matches of that word 561 if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi 562 563 # if we don't want to use *.mk, we can take out the cat and use 564 # test -f $makef and input redirection 565 COMPREPLY=( $(cat $makef 2>/dev/null | \ 566 awk 'BEGIN {FS=":"} /^[^.# ][^=]*:/ {print $1}' \ 567 | tr -s ' ' '\012' | sort -u | eval $gcmd ) ) 568 } 569 570 complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake 571 572 573 # cvs(1) completion 574 _cvs () 575 { 576 local cur prev 577 COMPREPLY=() 578 cur=${COMP_WORDS[COMP_CWORD]} 579 prev=${COMP_WORDS[COMP_CWORD-1]} 580 581 if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then 582 COMPREPLY=( $( compgen -W 'add admin checkout commit diff \ 583 export history import log rdiff release remove rtag status \ 584 tag update' $cur )) 585 else 586 COMPREPLY=( $( compgen -f $cur )) 587 fi 588 return 0 589 } 590 complete -F _cvs cvs 591 592 _killall () 593 { 594 local cur prev 595 COMPREPLY=() 596 cur=${COMP_WORDS[COMP_CWORD]} 597 598 # get a list of processes (the first sed evaluation 599 # takes care of swapped out processes, the second 600 # takes care of getting the basename of the process) 601 COMPREPLY=( $( /usr/bin/ps -u $USER -o comm | \ 602 sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \ 603 awk '{if ($0 ~ /^'$cur'/) print $0}' )) 604 605 return 0 606 } 607 608 complete -F _killall killall killps 609 610 611 # A meta-command completion function for commands like sudo(8), which 612 # need to first complete on a command, 613 # then complete according to that command's own 614 # completion definition - currently not quite foolproof 615 # (e.g. mount and umount don't work properly), 616 # but still quite useful -- 617 # By Ian McDonald, modified by me. 618 619 _my_command() 620 { 621 local cur func cline cspec 622 623 COMPREPLY=() 624 cur=${COMP_WORDS[COMP_CWORD]} 625 626 if [ $COMP_CWORD = 1 ]; then 627 COMPREPLY=( $( compgen -c $cur ) ) 628 elif complete -p ${COMP_WORDS[1]} &>/dev/null; then 629 cspec=$( complete -p ${COMP_WORDS[1]} ) 630 if [ "${cspec%%-F *}" != "${cspec}" ]; then 631 # complete -F <function> 632 # 633 # COMP_CWORD and COMP_WORDS() are not read-only, 634 # so we can set them before handing off to regular 635 # completion routine 636 637 # set current token number to 1 less than now 638 COMP_CWORD=$(( $COMP_CWORD - 1 )) 639 # get function name 640 func=${cspec#*-F } 641 func=${func%% *} 642 # get current command line minus initial command 643 cline="${COMP_LINE#$1 }" 644 # split current command line tokens into array 645 COMP_WORDS=( $cline ) 646 $func $cline 647 elif [ "${cspec#*-[abcdefgjkvu]}" != "" ]; then 648 # complete -[abcdefgjkvu] 649 #func=$( echo $cspec | sed -e 's/^.*\(-[abcdefgjkvu]\).*$/\1/' ) 650 func=$( echo $cspec | sed -e 's/^complete//' -e 's/[^ ]*$//' ) 651 COMPREPLY=( $( eval compgen $func $cur ) ) 652 elif [ "${cspec#*-A}" != "$cspec" ]; then 653 # complete -A <type> 654 func=${cspec#*-A } 655 func=${func%% *} 656 COMPREPLY=( $( compgen -A $func $cur ) ) 657 fi 658 else 659 COMPREPLY=( $( compgen -f $cur ) ) 660 fi 661 } 662 663 664 complete -o default -F _my_command nohup exec eval \ 665 trace truss strace sotruss gdb 666 complete -o default -F _my_command command type which man nice 667 668 # Local Variables: 669 # mode:shell-script 670 # sh-shell:bash 671 # End:</PRE></TD></TR></TABLE><HR></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="histcommands.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="dosbatch.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">History Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Converting DOS Batch Files to Shell Scripts</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -