⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample-bashrc.html

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