| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Sample .bashrc and |
| 6 | .bash_profile Files</TITLE |
| 7 | ><META |
| 8 | NAME="GENERATOR" |
| 9 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 10 | "><LINK |
| 11 | REL="HOME" |
| 12 | TITLE="Advanced Bash-Scripting Guide" |
| 13 | HREF="index.html"><LINK |
| 14 | REL="PREVIOUS" |
| 15 | TITLE="History Commands" |
| 16 | HREF="histcommands.html"><LINK |
| 17 | REL="NEXT" |
| 18 | TITLE="Converting DOS Batch Files to Shell Scripts" |
| 19 | HREF="dosbatch.html"><META |
| 20 | HTTP-EQUIV="Content-Style-Type" |
| 21 | CONTENT="text/css"><LINK |
| 22 | REL="stylesheet" |
| 23 | HREF="common/kde-common.css" |
| 24 | TYPE="text/css"><META |
| 25 | HTTP-EQUIV="Content-Type" |
| 26 | CONTENT="text/html; charset=iso-8859-1"><META |
| 27 | HTTP-EQUIV="Content-Language" |
| 28 | CONTENT="en"><LINK |
| 29 | REL="stylesheet" |
| 30 | HREF="common/kde-localised.css" |
| 31 | TYPE="text/css" |
| 32 | TITLE="KDE-English"><LINK |
| 33 | REL="stylesheet" |
| 34 | HREF="common/kde-default.css" |
| 35 | TYPE="text/css" |
| 36 | TITLE="KDE-Default"></HEAD |
| 37 | ><BODY |
| 38 | CLASS="APPENDIX" |
| 39 | BGCOLOR="#FFFFFF" |
| 40 | TEXT="#000000" |
| 41 | LINK="#AA0000" |
| 42 | VLINK="#AA0055" |
| 43 | ALINK="#AA0000" |
| 44 | STYLE="font-family: sans-serif;" |
| 45 | ><DIV |
| 46 | CLASS="NAVHEADER" |
| 47 | ><TABLE |
| 48 | SUMMARY="Header navigation table" |
| 49 | WIDTH="100%" |
| 50 | BORDER="0" |
| 51 | CELLPADDING="0" |
| 52 | CELLSPACING="0" |
| 53 | ><TR |
| 54 | ><TH |
| 55 | COLSPAN="3" |
| 56 | ALIGN="center" |
| 57 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 58 | ></TR |
| 59 | ><TR |
| 60 | ><TD |
| 61 | WIDTH="10%" |
| 62 | ALIGN="left" |
| 63 | VALIGN="bottom" |
| 64 | ><A |
| 65 | HREF="histcommands.html" |
| 66 | ACCESSKEY="P" |
| 67 | >Prev</A |
| 68 | ></TD |
| 69 | ><TD |
| 70 | WIDTH="80%" |
| 71 | ALIGN="center" |
| 72 | VALIGN="bottom" |
| 73 | ></TD |
| 74 | ><TD |
| 75 | WIDTH="10%" |
| 76 | ALIGN="right" |
| 77 | VALIGN="bottom" |
| 78 | ><A |
| 79 | HREF="dosbatch.html" |
| 80 | ACCESSKEY="N" |
| 81 | >Next</A |
| 82 | ></TD |
| 83 | ></TR |
| 84 | ></TABLE |
| 85 | ><HR |
| 86 | ALIGN="LEFT" |
| 87 | WIDTH="100%"></DIV |
| 88 | ><DIV |
| 89 | CLASS="APPENDIX" |
| 90 | ><H1 |
| 91 | ><A |
| 92 | NAME="SAMPLE-BASHRC" |
| 93 | ></A |
| 94 | >Appendix M. Sample <TT |
| 95 | CLASS="FILENAME" |
| 96 | >.bashrc</TT |
| 97 | > and |
| 98 | <TT |
| 99 | CLASS="FILENAME" |
| 100 | >.bash_profile</TT |
| 101 | > Files</H1 |
| 102 | ><P |
| 103 | >The <TT |
| 104 | CLASS="FILENAME" |
| 105 | >~/.bashrc</TT |
| 106 | > file determines the |
| 107 | behavior of interactive shells. A good look at this file can |
| 108 | lead to a better understanding of Bash.</P |
| 109 | ><P |
| 110 | ><A |
| 111 | HREF="mailto:[email protected]" |
| 112 | TARGET="_top" |
| 113 | >Emmanuel |
| 114 | Rouat</A |
| 115 | > contributed the following very elaborate |
| 116 | <TT |
| 117 | CLASS="FILENAME" |
| 118 | >.bashrc</TT |
| 119 | > file, written for a Linux system. |
| 120 | He welcomes reader feedback on it.</P |
| 121 | ><P |
| 122 | >Study the file carefully, and feel free to reuse code |
| 123 | snippets and functions from it in your own |
| 124 | <TT |
| 125 | CLASS="FILENAME" |
| 126 | >.bashrc</TT |
| 127 | > file or even in your scripts.</P |
| 128 | ><DIV |
| 129 | CLASS="EXAMPLE" |
| 130 | ><HR><A |
| 131 | NAME="BASHRC" |
| 132 | ></A |
| 133 | ><P |
| 134 | ><B |
| 135 | >Example M-1. Sample <TT |
| 136 | CLASS="FILENAME" |
| 137 | >.bashrc</TT |
| 138 | > file</B |
| 139 | ></P |
| 140 | ><TABLE |
| 141 | BORDER="0" |
| 142 | BGCOLOR="#E0E0E0" |
| 143 | WIDTH="100%" |
| 144 | ><TR |
| 145 | ><TD |
| 146 | ><PRE |
| 147 | CLASS="PROGRAMLISTING" |
| 148 | > 1 # =============================================================== # |
| 149 | 2 # |
| 150 | 3 # PERSONAL $HOME/.bashrc FILE for bash-3.0 (or later) |
| 151 | 4 # By Emmanuel Rouat [no-email] |
| 152 | 5 # |
| 153 | 6 # Last modified: Tue Nov 20 22:04:47 CET 2012 |
| 154 | 7 |
| 155 | 8 # This file is normally read by interactive shells only. |
| 156 | 9 #+ Here is the place to define your aliases, functions and |
| 157 | 10 #+ other interactive features like your prompt. |
| 158 | 11 # |
| 159 | 12 # The majority of the code here assumes you are on a GNU |
| 160 | 13 #+ system (most likely a Linux box) and is often based on code |
| 161 | 14 #+ found on Usenet or Internet. |
| 162 | 15 # |
| 163 | 16 # See for instance: |
| 164 | 17 # http://tldp.org/LDP/abs/html/index.html |
| 165 | 18 # http://www.caliban.org/bash |
| 166 | 19 # http://www.shelldorado.com/scripts/categories.html |
| 167 | 20 # http://www.dotfiles.org |
| 168 | 21 # |
| 169 | 22 # The choice of colors was done for a shell with a dark background |
| 170 | 23 #+ (white on black), and this is usually also suited for pure text-mode |
| 171 | 24 #+ consoles (no X server available). If you use a white background, |
| 172 | 25 #+ you'll have to do some other choices for readability. |
| 173 | 26 # |
| 174 | 27 # This bashrc file is a bit overcrowded. |
| 175 | 28 # Remember, it is just just an example. |
| 176 | 29 # Tailor it to your needs. |
| 177 | 30 # |
| 178 | 31 # =============================================================== # |
| 179 | 32 |
| 180 | 33 # --> Comments added by HOWTO author. |
| 181 | 34 |
| 182 | 35 # If not running interactively, don't do anything |
| 183 | 36 [ -z "$PS1" ] && return |
| 184 | 37 |
| 185 | 38 |
| 186 | 39 #------------------------------------------------------------- |
| 187 | 40 # Source global definitions (if any) |
| 188 | 41 #------------------------------------------------------------- |
| 189 | 42 |
| 190 | 43 |
| 191 | 44 if [ -f /etc/bashrc ]; then |
| 192 | 45 . /etc/bashrc # --> Read /etc/bashrc, if present. |
| 193 | 46 fi |
| 194 | 47 |
| 195 | 48 |
| 196 | 49 #-------------------------------------------------------------- |
| 197 | 50 # Automatic setting of $DISPLAY (if not set already). |
| 198 | 51 # This works for me - your mileage may vary. . . . |
| 199 | 52 # The problem is that different types of terminals give |
| 200 | 53 #+ different answers to 'who am i' (rxvt in particular can be |
| 201 | 54 #+ troublesome) - however this code seems to work in a majority |
| 202 | 55 #+ of cases. |
| 203 | 56 #-------------------------------------------------------------- |
| 204 | 57 |
| 205 | 58 function get_xserver () |
| 206 | 59 { |
| 207 | 60 case $TERM in |
| 208 | 61 xterm ) |
| 209 | 62 XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' ) |
| 210 | 63 # Ane-Pieter Wieringa suggests the following alternative: |
| 211 | 64 # I_AM=$(who am i) |
| 212 | 65 # SERVER=${I_AM#*(} |
| 213 | 66 # SERVER=${SERVER%*)} |
| 214 | 67 XSERVER=${XSERVER%%:*} |
| 215 | 68 ;; |
| 216 | 69 aterm | rxvt) |
| 217 | 70 # Find some code that works here. ... |
| 218 | 71 ;; |
| 219 | 72 esac |
| 220 | 73 } |
| 221 | 74 |
| 222 | 75 if [ -z ${DISPLAY:=""} ]; then |
| 223 | 76 get_xserver |
| 224 | 77 if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) || |
| 225 | 78 ${XSERVER} == "unix" ]]; then |
| 226 | 79 DISPLAY=":0.0" # Display on local host. |
| 227 | 80 else |
| 228 | 81 DISPLAY=${XSERVER}:0.0 # Display on remote host. |
| 229 | 82 fi |
| 230 | 83 fi |
| 231 | 84 |
| 232 | 85 export DISPLAY |
| 233 | 86 |
| 234 | 87 #------------------------------------------------------------- |
| 235 | 88 # Some settings |
| 236 | 89 #------------------------------------------------------------- |
| 237 | 90 |
| 238 | 91 #set -o nounset # These two options are useful for debugging. |
| 239 | 92 #set -o xtrace |
| 240 | 93 alias debug="set -o nounset; set -o xtrace" |
| 241 | 94 |
| 242 | 95 ulimit -S -c 0 # Don't want coredumps. |
| 243 | 96 set -o notify |
| 244 | 97 set -o noclobber |
| 245 | 98 set -o ignoreeof |
| 246 | 99 |
| 247 | 100 |
| 248 | 101 # Enable options: |
| 249 | 102 shopt -s cdspell |
| 250 | 103 shopt -s cdable_vars |
| 251 | 104 shopt -s checkhash |
| 252 | 105 shopt -s checkwinsize |
| 253 | 106 shopt -s sourcepath |
| 254 | 107 shopt -s no_empty_cmd_completion |
| 255 | 108 shopt -s cmdhist |
| 256 | 109 shopt -s histappend histreedit histverify |
| 257 | 110 shopt -s extglob # Necessary for programmable completion. |
| 258 | 111 |
| 259 | 112 # Disable options: |
| 260 | 113 shopt -u mailwarn |
| 261 | 114 unset MAILCHECK # Don't want my shell to warn me of incoming mail. |
| 262 | 115 |
| 263 | 116 |
| 264 | 117 #------------------------------------------------------------- |
| 265 | 118 # Greeting, motd etc. ... |
| 266 | 119 #------------------------------------------------------------- |
| 267 | 120 |
| 268 | 121 # Color definitions (taken from Color Bash Prompt HowTo). |
| 269 | 122 # Some colors might look different of some terminals. |
| 270 | 123 # For example, I see 'Bold Red' as 'orange' on my screen, |
| 271 | 124 # hence the 'Green' 'BRed' 'Red' sequence I often use in my prompt. |
| 272 | 125 |
| 273 | 126 |
| 274 | 127 # Normal Colors |
| 275 | 128 Black='\e[0;30m' # Black |
| 276 | 129 Red='\e[0;31m' # Red |
| 277 | 130 Green='\e[0;32m' # Green |
| 278 | 131 Yellow='\e[0;33m' # Yellow |
| 279 | 132 Blue='\e[0;34m' # Blue |
| 280 | 133 Purple='\e[0;35m' # Purple |
| 281 | 134 Cyan='\e[0;36m' # Cyan |
| 282 | 135 White='\e[0;37m' # White |
| 283 | 136 |
| 284 | 137 # Bold |
| 285 | 138 BBlack='\e[1;30m' # Black |
| 286 | 139 BRed='\e[1;31m' # Red |
| 287 | 140 BGreen='\e[1;32m' # Green |
| 288 | 141 BYellow='\e[1;33m' # Yellow |
| 289 | 142 BBlue='\e[1;34m' # Blue |
| 290 | 143 BPurple='\e[1;35m' # Purple |
| 291 | 144 BCyan='\e[1;36m' # Cyan |
| 292 | 145 BWhite='\e[1;37m' # White |
| 293 | 146 |
| 294 | 147 # Background |
| 295 | 148 On_Black='\e[40m' # Black |
| 296 | 149 On_Red='\e[41m' # Red |
| 297 | 150 On_Green='\e[42m' # Green |
| 298 | 151 On_Yellow='\e[43m' # Yellow |
| 299 | 152 On_Blue='\e[44m' # Blue |
| 300 | 153 On_Purple='\e[45m' # Purple |
| 301 | 154 On_Cyan='\e[46m' # Cyan |
| 302 | 155 On_White='\e[47m' # White |
| 303 | 156 |
| 304 | 157 NC="\e[m" # Color Reset |
| 305 | 158 |
| 306 | 159 |
| 307 | 160 ALERT=${BWhite}${On_Red} # Bold White on red background |
| 308 | 161 |
| 309 | 162 |
| 310 | 163 |
| 311 | 164 echo -e "${BCyan}This is BASH ${BRed}${BASH_VERSION%.*}${BCyan}\ |
| 312 | 165 - DISPLAY on ${BRed}$DISPLAY${NC}\n" |
| 313 | 166 date |
| 314 | 167 if [ -x /usr/games/fortune ]; then |
| 315 | 168 /usr/games/fortune -s # Makes our day a bit more fun.... :-) |
| 316 | 169 fi |
| 317 | 170 |
| 318 | 171 function _exit() # Function to run upon exit of shell. |
| 319 | 172 { |
| 320 | 173 echo -e "${BRed}Hasta la vista, baby${NC}" |
| 321 | 174 } |
| 322 | 175 trap _exit EXIT |
| 323 | 176 |
| 324 | 177 #------------------------------------------------------------- |
| 325 | 178 # Shell Prompt - for many examples, see: |
| 326 | 179 # http://www.debian-administration.org/articles/205 |
| 327 | 180 # http://www.askapache.com/linux/bash-power-prompt.html |
| 328 | 181 # http://tldp.org/HOWTO/Bash-Prompt-HOWTO |
| 329 | 182 # https://github.com/nojhan/liquidprompt |
| 330 | 183 #------------------------------------------------------------- |
| 331 | 184 # Current Format: [TIME USER@HOST PWD] > |
| 332 | 185 # TIME: |
| 333 | 186 # Green == machine load is low |
| 334 | 187 # Orange == machine load is medium |
| 335 | 188 # Red == machine load is high |
| 336 | 189 # ALERT == machine load is very high |
| 337 | 190 # USER: |
| 338 | 191 # Cyan == normal user |
| 339 | 192 # Orange == SU to user |
| 340 | 193 # Red == root |
| 341 | 194 # HOST: |
| 342 | 195 # Cyan == local session |
| 343 | 196 # Green == secured remote connection (via ssh) |
| 344 | 197 # Red == unsecured remote connection |
| 345 | 198 # PWD: |
| 346 | 199 # Green == more than 10% free disk space |
| 347 | 200 # Orange == less than 10% free disk space |
| 348 | 201 # ALERT == less than 5% free disk space |
| 349 | 202 # Red == current user does not have write privileges |
| 350 | 203 # Cyan == current filesystem is size zero (like /proc) |
| 351 | 204 # >: |
| 352 | 205 # White == no background or suspended jobs in this shell |
| 353 | 206 # Cyan == at least one background job in this shell |
| 354 | 207 # Orange == at least one suspended job in this shell |
| 355 | 208 # |
| 356 | 209 # Command is added to the history file each time you hit enter, |
| 357 | 210 # so it's available to all shells (using 'history -a'). |
| 358 | 211 |
| 359 | 212 |
| 360 | 213 # Test connection type: |
| 361 | 214 if [ -n "${SSH_CONNECTION}" ]; then |
| 362 | 215 CNX=${Green} # Connected on remote machine, via ssh (good). |
| 363 | 216 elif [[ "${DISPLAY%%:0*}" != "" ]]; then |
| 364 | 217 CNX=${ALERT} # Connected on remote machine, not via ssh (bad). |
| 365 | 218 else |
| 366 | 219 CNX=${BCyan} # Connected on local machine. |
| 367 | 220 fi |
| 368 | 221 |
| 369 | 222 # Test user type: |
| 370 | 223 if [[ ${USER} == "root" ]]; then |
| 371 | 224 SU=${Red} # User is root. |
| 372 | 225 elif [[ ${USER} != $(logname) ]]; then |
| 373 | 226 SU=${BRed} # User is not login user. |
| 374 | 227 else |
| 375 | 228 SU=${BCyan} # User is normal (well ... most of us are). |
| 376 | 229 fi |
| 377 | 230 |
| 378 | 231 |
| 379 | 232 |
| 380 | 233 NCPU=$(grep -c 'processor' /proc/cpuinfo) # Number of CPUs |
| 381 | 234 SLOAD=$(( 100*${NCPU} )) # Small load |
| 382 | 235 MLOAD=$(( 200*${NCPU} )) # Medium load |
| 383 | 236 XLOAD=$(( 400*${NCPU} )) # Xlarge load |
| 384 | 237 |
| 385 | 238 # Returns system load as percentage, i.e., '40' rather than '0.40)'. |
| 386 | 239 function load() |
| 387 | 240 { |
| 388 | 241 local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.') |
| 389 | 242 # System load of the current host. |
| 390 | 243 echo $((10#$SYSLOAD)) # Convert to decimal. |
| 391 | 244 } |
| 392 | 245 |
| 393 | 246 # Returns a color indicating system load. |
| 394 | 247 function load_color() |
| 395 | 248 { |
| 396 | 249 local SYSLOAD=$(load) |
| 397 | 250 if [ ${SYSLOAD} -gt ${XLOAD} ]; then |
| 398 | 251 echo -en ${ALERT} |
| 399 | 252 elif [ ${SYSLOAD} -gt ${MLOAD} ]; then |
| 400 | 253 echo -en ${Red} |
| 401 | 254 elif [ ${SYSLOAD} -gt ${SLOAD} ]; then |
| 402 | 255 echo -en ${BRed} |
| 403 | 256 else |
| 404 | 257 echo -en ${Green} |
| 405 | 258 fi |
| 406 | 259 } |
| 407 | 260 |
| 408 | 261 # Returns a color according to free disk space in $PWD. |
| 409 | 262 function disk_color() |
| 410 | 263 { |
| 411 | 264 if [ ! -w "${PWD}" ] ; then |
| 412 | 265 echo -en ${Red} |
| 413 | 266 # No 'write' privilege in the current directory. |
| 414 | 267 elif [ -s "${PWD}" ] ; then |
| 415 | 268 local used=$(command df -P "$PWD" | |
| 416 | 269 awk 'END {print $5} {sub(/%/,"")}') |
| 417 | 270 if [ ${used} -gt 95 ]; then |
| 418 | 271 echo -en ${ALERT} # Disk almost full (>95%). |
| 419 | 272 elif [ ${used} -gt 90 ]; then |
| 420 | 273 echo -en ${BRed} # Free disk space almost gone. |
| 421 | 274 else |
| 422 | 275 echo -en ${Green} # Free disk space is ok. |
| 423 | 276 fi |
| 424 | 277 else |
| 425 | 278 echo -en ${Cyan} |
| 426 | 279 # Current directory is size '0' (like /proc, /sys etc). |
| 427 | 280 fi |
| 428 | 281 } |
| 429 | 282 |
| 430 | 283 # Returns a color according to running/suspended jobs. |
| 431 | 284 function job_color() |
| 432 | 285 { |
| 433 | 286 if [ $(jobs -s | wc -l) -gt "0" ]; then |
| 434 | 287 echo -en ${BRed} |
| 435 | 288 elif [ $(jobs -r | wc -l) -gt "0" ] ; then |
| 436 | 289 echo -en ${BCyan} |
| 437 | 290 fi |
| 438 | 291 } |
| 439 | 292 |
| 440 | 293 # Adds some text in the terminal frame (if applicable). |
| 441 | 294 |
| 442 | 295 |
| 443 | 296 # Now we construct the prompt. |
| 444 | 297 PROMPT_COMMAND="history -a" |
| 445 | 298 case ${TERM} in |
| 446 | 299 *term | rxvt | linux) |
| 447 | 300 PS1="\[\$(load_color)\][\A\[${NC}\] " |
| 448 | 301 # Time of day (with load info): |
| 449 | 302 PS1="\[\$(load_color)\][\A\[${NC}\] " |
| 450 | 303 # User@Host (with connection type info): |
| 451 | 304 PS1=${PS1}"\[${SU}\]\u\[${NC}\]@\[${CNX}\]\h\[${NC}\] " |
| 452 | 305 # PWD (with 'disk space' info): |
| 453 | 306 PS1=${PS1}"\[\$(disk_color)\]\W]\[${NC}\] " |
| 454 | 307 # Prompt (with 'job' info): |
| 455 | 308 PS1=${PS1}"\[\$(job_color)\]>\[${NC}\] " |
| 456 | 309 # Set title of current xterm: |
| 457 | 310 PS1=${PS1}"\[\e]0;[\u@\h] \w\a\]" |
| 458 | 311 ;; |
| 459 | 312 *) |
| 460 | 313 PS1="(\A \u@\h \W) > " # --> PS1="(\A \u@\h \w) > " |
| 461 | 314 # --> Shows full pathname of current dir. |
| 462 | 315 ;; |
| 463 | 316 esac |
| 464 | 317 |
| 465 | 318 |
| 466 | 319 |
| 467 | 320 export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n' |
| 468 | 321 export HISTIGNORE="&:bg:fg:ll:h" |
| 469 | 322 export HISTTIMEFORMAT="$(echo -e ${BCyan})[%d/%m %H:%M:%S]$(echo -e ${NC}) " |
| 470 | 323 export HISTCONTROL=ignoredups |
| 471 | 324 export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts |
| 472 | 325 |
| 473 | 326 |
| 474 | 327 #============================================================ |
| 475 | 328 # |
| 476 | 329 # ALIASES AND FUNCTIONS |
| 477 | 330 # |
| 478 | 331 # Arguably, some functions defined here are quite big. |
| 479 | 332 # If you want to make this file smaller, these functions can |
| 480 | 333 #+ be converted into scripts and removed from here. |
| 481 | 334 # |
| 482 | 335 #============================================================ |
| 483 | 336 |
| 484 | 337 #------------------- |
| 485 | 338 # Personnal Aliases |
| 486 | 339 #------------------- |
| 487 | 340 |
| 488 | 341 alias rm='rm -i' |
| 489 | 342 alias cp='cp -i' |
| 490 | 343 alias mv='mv -i' |
| 491 | 344 # -> Prevents accidentally clobbering files. |
| 492 | 345 alias mkdir='mkdir -p' |
| 493 | 346 |
| 494 | 347 alias h='history' |
| 495 | 348 alias j='jobs -l' |
| 496 | 349 alias which='type -a' |
| 497 | 350 alias ..='cd ..' |
| 498 | 351 |
| 499 | 352 # Pretty-print of some PATH variables: |
| 500 | 353 alias path='echo -e ${PATH//:/\\n}' |
| 501 | 354 alias libpath='echo -e ${LD_LIBRARY_PATH//:/\\n}' |
| 502 | 355 |
| 503 | 356 |
| 504 | 357 alias du='du -kh' # Makes a more readable output. |
| 505 | 358 alias df='df -kTh' |
| 506 | 359 |
| 507 | 360 #------------------------------------------------------------- |
| 508 | 361 # The 'ls' family (this assumes you use a recent GNU ls). |
| 509 | 362 #------------------------------------------------------------- |
| 510 | 363 # Add colors for filetype and human-readable sizes by default on 'ls': |
| 511 | 364 alias ls='ls -h --color' |
| 512 | 365 alias lx='ls -lXB' # Sort by extension. |
| 513 | 366 alias lk='ls -lSr' # Sort by size, biggest last. |
| 514 | 367 alias lt='ls -ltr' # Sort by date, most recent last. |
| 515 | 368 alias lc='ls -ltcr' # Sort by/show change time,most recent last. |
| 516 | 369 alias lu='ls -ltur' # Sort by/show access time,most recent last. |
| 517 | 370 |
| 518 | 371 # The ubiquitous 'll': directories first, with alphanumeric sorting: |
| 519 | 372 alias ll="ls -lv --group-directories-first" |
| 520 | 373 alias lm='ll |more' # Pipe through 'more' |
| 521 | 374 alias lr='ll -R' # Recursive ls. |
| 522 | 375 alias la='ll -A' # Show hidden files. |
| 523 | 376 alias tree='tree -Csuh' # Nice alternative to 'recursive ls' ... |
| 524 | 377 |
| 525 | 378 |
| 526 | 379 #------------------------------------------------------------- |
| 527 | 380 # Tailoring 'less' |
| 528 | 381 #------------------------------------------------------------- |
| 529 | 382 |
| 530 | 383 alias more='less' |
| 531 | 384 export PAGER=less |
| 532 | 385 export LESSCHARSET='latin1' |
| 533 | 386 export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-' |
| 534 | 387 # Use this if lesspipe.sh exists. |
| 535 | 388 export LESS='-i -N -w -z-4 -g -e -M -X -F -R -P%t?f%f \ |
| 536 | 389 :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...' |
| 537 | 390 |
| 538 | 391 # LESS man page colors (makes Man pages more readable). |
| 539 | 392 export LESS_TERMCAP_mb=$'\E[01;31m' |
| 540 | 393 export LESS_TERMCAP_md=$'\E[01;31m' |
| 541 | 394 export LESS_TERMCAP_me=$'\E[0m' |
| 542 | 395 export LESS_TERMCAP_se=$'\E[0m' |
| 543 | 396 export LESS_TERMCAP_so=$'\E[01;44;33m' |
| 544 | 397 export LESS_TERMCAP_ue=$'\E[0m' |
| 545 | 398 export LESS_TERMCAP_us=$'\E[01;32m' |
| 546 | 399 |
| 547 | 400 |
| 548 | 401 #------------------------------------------------------------- |
| 549 | 402 # Spelling typos - highly personnal and keyboard-dependent :-) |
| 550 | 403 #------------------------------------------------------------- |
| 551 | 404 |
| 552 | 405 alias xs='cd' |
| 553 | 406 alias vf='cd' |
| 554 | 407 alias moer='more' |
| 555 | 408 alias moew='more' |
| 556 | 409 alias kk='ll' |
| 557 | 410 |
| 558 | 411 |
| 559 | 412 #------------------------------------------------------------- |
| 560 | 413 # A few fun ones |
| 561 | 414 #------------------------------------------------------------- |
| 562 | 415 |
| 563 | 416 # Adds some text in the terminal frame (if applicable). |
| 564 | 417 |
| 565 | 418 function xtitle() |
| 566 | 419 { |
| 567 | 420 case "$TERM" in |
| 568 | 421 *term* | rxvt) |
| 569 | 422 echo -en "\e]0;$*\a" ;; |
| 570 | 423 *) ;; |
| 571 | 424 esac |
| 572 | 425 } |
| 573 | 426 |
| 574 | 427 |
| 575 | 428 # Aliases that use xtitle |
| 576 | 429 alias top='xtitle Processes on $HOST && top' |
| 577 | 430 alias make='xtitle Making $(basename $PWD) ; make' |
| 578 | 431 |
| 579 | 432 # .. and functions |
| 580 | 433 function man() |
| 581 | 434 { |
| 582 | 435 for i ; do |
| 583 | 436 xtitle The $(basename $1|tr -d .[:digit:]) manual |
| 584 | 437 command man -a "$i" |
| 585 | 438 done |
| 586 | 439 } |
| 587 | 440 |
| 588 | 441 |
| 589 | 442 #------------------------------------------------------------- |
| 590 | 443 # Make the following commands run in background automatically: |
| 591 | 444 #------------------------------------------------------------- |
| 592 | 445 |
| 593 | 446 function te() # wrapper around xemacs/gnuserv |
| 594 | 447 { |
| 595 | 448 if [ "$(gnuclient -batch -eval t 2>&-)" == "t" ]; then |
| 596 | 449 gnuclient -q "$@"; |
| 597 | 450 else |
| 598 | 451 ( xemacs "$@" &); |
| 599 | 452 fi |
| 600 | 453 } |
| 601 | 454 |
| 602 | 455 function soffice() { command soffice "$@" & } |
| 603 | 456 function firefox() { command firefox "$@" & } |
| 604 | 457 function xpdf() { command xpdf "$@" & } |
| 605 | 458 |
| 606 | 459 |
| 607 | 460 #------------------------------------------------------------- |
| 608 | 461 # File & strings related functions: |
| 609 | 462 #------------------------------------------------------------- |
| 610 | 463 |
| 611 | 464 |
| 612 | 465 # Find a file with a pattern in name: |
| 613 | 466 function ff() { find . -type f -iname '*'"$*"'*' -ls ; } |
| 614 | 467 |
| 615 | 468 # Find a file with pattern $1 in name and Execute $2 on it: |
| 616 | 469 function fe() { find . -type f -iname '*'"${1:-}"'*' \ |
| 617 | 470 -exec ${2:-file} {} \; ; } |
| 618 | 471 |
| 619 | 472 # Find a pattern in a set of files and highlight them: |
| 620 | 473 #+ (needs a recent version of egrep). |
| 621 | 474 function fstr() |
| 622 | 475 { |
| 623 | 476 OPTIND=1 |
| 624 | 477 local mycase="" |
| 625 | 478 local usage="fstr: find string in files. |
| 626 | 479 Usage: fstr [-i] \"pattern\" [\"filename pattern\"] " |
| 627 | 480 while getopts :it opt |
| 628 | 481 do |
| 629 | 482 case "$opt" in |
| 630 | 483 i) mycase="-i " ;; |
| 631 | 484 *) echo "$usage"; return ;; |
| 632 | 485 esac |
| 633 | 486 done |
| 634 | 487 shift $(( $OPTIND - 1 )) |
| 635 | 488 if [ "$#" -lt 1 ]; then |
| 636 | 489 echo "$usage" |
| 637 | 490 return; |
| 638 | 491 fi |
| 639 | 492 find . -type f -name "${2:-*}" -print0 | \ |
| 640 | 493 xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more |
| 641 | 494 |
| 642 | 495 } |
| 643 | 496 |
| 644 | 497 |
| 645 | 498 function swap() |
| 646 | 499 { # Swap 2 filenames around, if they exist (from Uzi's bashrc). |
| 647 | 500 local TMPFILE=tmp.$$ |
| 648 | 501 |
| 649 | 502 [ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1 |
| 650 | 503 [ ! -e $1 ] && echo "swap: $1 does not exist" && return 1 |
| 651 | 504 [ ! -e $2 ] && echo "swap: $2 does not exist" && return 1 |
| 652 | 505 |
| 653 | 506 mv "$1" $TMPFILE |
| 654 | 507 mv "$2" "$1" |
| 655 | 508 mv $TMPFILE "$2" |
| 656 | 509 } |
| 657 | 510 |
| 658 | 511 function extract() # Handy Extract Program |
| 659 | 512 { |
| 660 | 513 if [ -f $1 ] ; then |
| 661 | 514 case $1 in |
| 662 | 515 *.tar.bz2) tar xvjf $1 ;; |
| 663 | 516 *.tar.gz) tar xvzf $1 ;; |
| 664 | 517 *.bz2) bunzip2 $1 ;; |
| 665 | 518 *.rar) unrar x $1 ;; |
| 666 | 519 *.gz) gunzip $1 ;; |
| 667 | 520 *.tar) tar xvf $1 ;; |
| 668 | 521 *.tbz2) tar xvjf $1 ;; |
| 669 | 522 *.tgz) tar xvzf $1 ;; |
| 670 | 523 *.zip) unzip $1 ;; |
| 671 | 524 *.Z) uncompress $1 ;; |
| 672 | 525 *.7z) 7z x $1 ;; |
| 673 | 526 *) echo "'$1' cannot be extracted via >extract<" ;; |
| 674 | 527 esac |
| 675 | 528 else |
| 676 | 529 echo "'$1' is not a valid file!" |
| 677 | 530 fi |
| 678 | 531 } |
| 679 | 532 |
| 680 | 533 |
| 681 | 534 # Creates an archive (*.tar.gz) from given directory. |
| 682 | 535 function maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; } |
| 683 | 536 |
| 684 | 537 # Create a ZIP archive of a file or folder. |
| 685 | 538 function makezip() { zip -r "${1%%/}.zip" "$1" ; } |
| 686 | 539 |
| 687 | 540 # Make your directories and files access rights sane. |
| 688 | 541 function sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;} |
| 689 | 542 |
| 690 | 543 #------------------------------------------------------------- |
| 691 | 544 # Process/system related functions: |
| 692 | 545 #------------------------------------------------------------- |
| 693 | 546 |
| 694 | 547 |
| 695 | 548 function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; } |
| 696 | 549 function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; } |
| 697 | 550 |
| 698 | 551 |
| 699 | 552 function killps() # kill by process name |
| 700 | 553 { |
| 701 | 554 local pid pname sig="-TERM" # default signal |
| 702 | 555 if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then |
| 703 | 556 echo "Usage: killps [-SIGNAL] pattern" |
| 704 | 557 return; |
| 705 | 558 fi |
| 706 | 559 if [ $# = 2 ]; then sig=$1 ; fi |
| 707 | 560 for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} ) |
| 708 | 561 do |
| 709 | 562 pname=$(my_ps | awk '$1~var { print $5 }' var=$pid ) |
| 710 | 563 if ask "Kill process $pid <$pname> with signal $sig?" |
| 711 | 564 then kill $sig $pid |
| 712 | 565 fi |
| 713 | 566 done |
| 714 | 567 } |
| 715 | 568 |
| 716 | 569 function mydf() # Pretty-print of 'df' output. |
| 717 | 570 { # Inspired by 'dfc' utility. |
| 718 | 571 for fs ; do |
| 719 | 572 |
| 720 | 573 if [ ! -d $fs ] |
| 721 | 574 then |
| 722 | 575 echo -e $fs" :No such file or directory" ; continue |
| 723 | 576 fi |
| 724 | 577 |
| 725 | 578 local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') ) |
| 726 | 579 local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') ) |
| 727 | 580 local nbstars=$(( 20 * ${info[1]} / ${info[0]} )) |
| 728 | 581 local out="[" |
| 729 | 582 for ((j=0;j<20;j++)); do |
| 730 | 583 if [ ${j} -lt ${nbstars} ]; then |
| 731 | 584 out=$out"*" |
| 732 | 585 else |
| 733 | 586 out=$out"-" |
| 734 | 587 fi |
| 735 | 588 done |
| 736 | 589 out=${info[2]}" "$out"] ("$free" free on "$fs")" |
| 737 | 590 echo -e $out |
| 738 | 591 done |
| 739 | 592 } |
| 740 | 593 |
| 741 | 594 |
| 742 | 595 function my_ip() # Get IP adress on ethernet. |
| 743 | 596 { |
| 744 | 597 MY_IP=$(/sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' | |
| 745 | 598 sed -e s/addr://) |
| 746 | 599 echo ${MY_IP:-"Not connected"} |
| 747 | 600 } |
| 748 | 601 |
| 749 | 602 function ii() # Get current host related info. |
| 750 | 603 { |
| 751 | 604 echo -e "\nYou are logged on ${BRed}$HOST" |
| 752 | 605 echo -e "\n${BRed}Additionnal information:$NC " ; uname -a |
| 753 | 606 echo -e "\n${BRed}Users logged on:$NC " ; w -hs | |
| 754 | 607 cut -d " " -f1 | sort | uniq |
| 755 | 608 echo -e "\n${BRed}Current date :$NC " ; date |
| 756 | 609 echo -e "\n${BRed}Machine stats :$NC " ; uptime |
| 757 | 610 echo -e "\n${BRed}Memory stats :$NC " ; free |
| 758 | 611 echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME |
| 759 | 612 echo -e "\n${BRed}Local IP Address :$NC" ; my_ip |
| 760 | 613 echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet; |
| 761 | 614 echo |
| 762 | 615 } |
| 763 | 616 |
| 764 | 617 #------------------------------------------------------------- |
| 765 | 618 # Misc utilities: |
| 766 | 619 #------------------------------------------------------------- |
| 767 | 620 |
| 768 | 621 function repeat() # Repeat n times command. |
| 769 | 622 { |
| 770 | 623 local i max |
| 771 | 624 max=$1; shift; |
| 772 | 625 for ((i=1; i <= max ; i++)); do # --> C-like syntax |
| 773 | 626 eval "$@"; |
| 774 | 627 done |
| 775 | 628 } |
| 776 | 629 |
| 777 | 630 |
| 778 | 631 function ask() # See 'killps' for example of use. |
| 779 | 632 { |
| 780 | 633 echo -n "$@" '[y/n] ' ; read ans |
| 781 | 634 case "$ans" in |
| 782 | 635 y*|Y*) return 0 ;; |
| 783 | 636 *) return 1 ;; |
| 784 | 637 esac |
| 785 | 638 } |
| 786 | 639 |
| 787 | 640 function corename() # Get name of app that created a corefile. |
| 788 | 641 { |
| 789 | 642 for file ; do |
| 790 | 643 echo -n $file : ; gdb --core=$file --batch | head -1 |
| 791 | 644 done |
| 792 | 645 } |
| 793 | 646 |
| 794 | 647 |
| 795 | 648 |
| 796 | 649 #========================================================================= |
| 797 | 650 # |
| 798 | 651 # PROGRAMMABLE COMPLETION SECTION |
| 799 | 652 # Most are taken from the bash 2.05 documentation and from Ian McDonald's |
| 800 | 653 # 'Bash completion' package (http://www.caliban.org/bash/#completion) |
| 801 | 654 # You will in fact need bash more recent then 3.0 for some features. |
| 802 | 655 # |
| 803 | 656 # Note that most linux distributions now provide many completions |
| 804 | 657 # 'out of the box' - however, you might need to make your own one day, |
| 805 | 658 # so I kept those here as examples. |
| 806 | 659 #========================================================================= |
| 807 | 660 |
| 808 | 661 if [ "${BASH_VERSION%.*}" \< "3.0" ]; then |
| 809 | 662 echo "You will need to upgrade to version 3.0 for full \ |
| 810 | 663 programmable completion features" |
| 811 | 664 return |
| 812 | 665 fi |
| 813 | 666 |
| 814 | 667 shopt -s extglob # Necessary. |
| 815 | 668 |
| 816 | 669 complete -A hostname rsh rcp telnet rlogin ftp ping disk |
| 817 | 670 complete -A export printenv |
| 818 | 671 complete -A variable export local readonly unset |
| 819 | 672 complete -A enabled builtin |
| 820 | 673 complete -A alias alias unalias |
| 821 | 674 complete -A function function |
| 822 | 675 complete -A user su mail finger |
| 823 | 676 |
| 824 | 677 complete -A helptopic help # Currently same as builtins. |
| 825 | 678 complete -A shopt shopt |
| 826 | 679 complete -A stopped -P '%' bg |
| 827 | 680 complete -A job -P '%' fg jobs disown |
| 828 | 681 |
| 829 | 682 complete -A directory mkdir rmdir |
| 830 | 683 complete -A directory -o default cd |
| 831 | 684 |
| 832 | 685 # Compression |
| 833 | 686 complete -f -o default -X '*.+(zip|ZIP)' zip |
| 834 | 687 complete -f -o default -X '!*.+(zip|ZIP)' unzip |
| 835 | 688 complete -f -o default -X '*.+(z|Z)' compress |
| 836 | 689 complete -f -o default -X '!*.+(z|Z)' uncompress |
| 837 | 690 complete -f -o default -X '*.+(gz|GZ)' gzip |
| 838 | 691 complete -f -o default -X '!*.+(gz|GZ)' gunzip |
| 839 | 692 complete -f -o default -X '*.+(bz2|BZ2)' bzip2 |
| 840 | 693 complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2 |
| 841 | 694 complete -f -o default -X '!*.+(zip|ZIP|z|Z|gz|GZ|bz2|BZ2)' extract |
| 842 | 695 |
| 843 | 696 |
| 844 | 697 # Documents - Postscript,pdf,dvi..... |
| 845 | 698 complete -f -o default -X '!*.+(ps|PS)' gs ghostview ps2pdf ps2ascii |
| 846 | 699 complete -f -o default -X \ |
| 847 | 700 '!*.+(dvi|DVI)' dvips dvipdf xdvi dviselect dvitype |
| 848 | 701 complete -f -o default -X '!*.+(pdf|PDF)' acroread pdf2ps |
| 849 | 702 complete -f -o default -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?\ |
| 850 | 703 (.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv |
| 851 | 704 complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf |
| 852 | 705 complete -f -o default -X '!*.tex' tex latex slitex |
| 853 | 706 complete -f -o default -X '!*.lyx' lyx |
| 854 | 707 complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps |
| 855 | 708 complete -f -o default -X \ |
| 856 | 709 '!*.+(doc|DOC|xls|XLS|ppt|PPT|sx?|SX?|csv|CSV|od?|OD?|ott|OTT)' soffice |
| 857 | 710 |
| 858 | 711 # Multimedia |
| 859 | 712 complete -f -o default -X \ |
| 860 | 713 '!*.+(gif|GIF|jp*g|JP*G|bmp|BMP|xpm|XPM|png|PNG)' xv gimp ee gqview |
| 861 | 714 complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321 |
| 862 | 715 complete -f -o default -X '!*.+(ogg|OGG)' ogg123 |
| 863 | 716 complete -f -o default -X \ |
| 864 | 717 '!*.@(mp[23]|MP[23]|ogg|OGG|wav|WAV|pls|\ |
| 865 | 718 m3u|xm|mod|s[3t]m|it|mtm|ult|flac)' xmms |
| 866 | 719 complete -f -o default -X '!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|\ |
| 867 | 720 asf|vob|VOB|bin|dat|vcd|ps|pes|fli|viv|rm|ram|yuv|mov|MOV|qt|\ |
| 868 | 721 QT|wmv|mp3|MP3|ogg|OGG|ogm|OGM|mp4|MP4|wav|WAV|asx|ASX)' xine |
| 869 | 722 |
| 870 | 723 |
| 871 | 724 |
| 872 | 725 complete -f -o default -X '!*.pl' perl perl5 |
| 873 | 726 |
| 874 | 727 |
| 875 | 728 # This is a 'universal' completion function - it works when commands have |
| 876 | 729 #+ a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a' |
| 877 | 730 # Needs the '-o' option of grep |
| 878 | 731 #+ (try the commented-out version if not available). |
| 879 | 732 |
| 880 | 733 # First, remove '=' from completion word separators |
| 881 | 734 #+ (this will allow completions like 'ls --color=auto' to work correctly). |
| 882 | 735 |
| 883 | 736 COMP_WORDBREAKS=${COMP_WORDBREAKS/=/} |
| 884 | 737 |
| 885 | 738 |
| 886 | 739 _get_longopts() |
| 887 | 740 { |
| 888 | 741 #$1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \ |
| 889 | 742 #grep ^"$2" |sort -u ; |
| 890 | 743 $1 --help | grep -o -e "--[^[:space:].,]*" | grep -e "$2" |sort -u |
| 891 | 744 } |
| 892 | 745 |
| 893 | 746 _longopts() |
| 894 | 747 { |
| 895 | 748 local cur |
| 896 | 749 cur=${COMP_WORDS[COMP_CWORD]} |
| 897 | 750 |
| 898 | 751 case "${cur:-*}" in |
| 899 | 752 -*) ;; |
| 900 | 753 *) return ;; |
| 901 | 754 esac |
| 902 | 755 |
| 903 | 756 case "$1" in |
| 904 | 757 \~*) eval cmd="$1" ;; |
| 905 | 758 *) cmd="$1" ;; |
| 906 | 759 esac |
| 907 | 760 COMPREPLY=( $(_get_longopts ${1} ${cur} ) ) |
| 908 | 761 } |
| 909 | 762 complete -o default -F _longopts configure bash |
| 910 | 763 complete -o default -F _longopts wget id info a2ps ls recode |
| 911 | 764 |
| 912 | 765 _tar() |
| 913 | 766 { |
| 914 | 767 local cur ext regex tar untar |
| 915 | 768 |
| 916 | 769 COMPREPLY=() |
| 917 | 770 cur=${COMP_WORDS[COMP_CWORD]} |
| 918 | 771 |
| 919 | 772 # If we want an option, return the possible long options. |
| 920 | 773 case "$cur" in |
| 921 | 774 -*) COMPREPLY=( $(_get_longopts $1 $cur ) ); return 0;; |
| 922 | 775 esac |
| 923 | 776 |
| 924 | 777 if [ $COMP_CWORD -eq 1 ]; then |
| 925 | 778 COMPREPLY=( $( compgen -W 'c t x u r d A' -- $cur ) ) |
| 926 | 779 return 0 |
| 927 | 780 fi |
| 928 | 781 |
| 929 | 782 case "${COMP_WORDS[1]}" in |
| 930 | 783 ?(-)c*f) |
| 931 | 784 COMPREPLY=( $( compgen -f $cur ) ) |
| 932 | 785 return 0 |
| 933 | 786 ;; |
| 934 | 787 +([^Izjy])f) |
| 935 | 788 ext='tar' |
| 936 | 789 regex=$ext |
| 937 | 790 ;; |
| 938 | 791 *z*f) |
| 939 | 792 ext='tar.gz' |
| 940 | 793 regex='t\(ar\.\)\(gz\|Z\)' |
| 941 | 794 ;; |
| 942 | 795 *[Ijy]*f) |
| 943 | 796 ext='t?(ar.)bz?(2)' |
| 944 | 797 regex='t\(ar\.\)bz2\?' |
| 945 | 798 ;; |
| 946 | 799 *) |
| 947 | 800 COMPREPLY=( $( compgen -f $cur ) ) |
| 948 | 801 return 0 |
| 949 | 802 ;; |
| 950 | 803 |
| 951 | 804 esac |
| 952 | 805 |
| 953 | 806 if [[ "$COMP_LINE" == tar*.$ext' '* ]]; then |
| 954 | 807 # Complete on files in tar file. |
| 955 | 808 # |
| 956 | 809 # Get name of tar file from command line. |
| 957 | 810 tar=$( echo "$COMP_LINE" | \ |
| 958 | 811 sed -e 's|^.* \([^ ]*'$regex'\) .*$|\1|' ) |
| 959 | 812 # Devise how to untar and list it. |
| 960 | 813 untar=t${COMP_WORDS[1]//[^Izjyf]/} |
| 961 | 814 |
| 962 | 815 COMPREPLY=( $( compgen -W "$( echo $( tar $untar $tar \ |
| 963 | 816 2>/dev/null ) )" -- "$cur" ) ) |
| 964 | 817 return 0 |
| 965 | 818 |
| 966 | 819 else |
| 967 | 820 # File completion on relevant files. |
| 968 | 821 COMPREPLY=( $( compgen -G $cur\*.$ext ) ) |
| 969 | 822 |
| 970 | 823 fi |
| 971 | 824 |
| 972 | 825 return 0 |
| 973 | 826 |
| 974 | 827 } |
| 975 | 828 |
| 976 | 829 complete -F _tar -o default tar |
| 977 | 830 |
| 978 | 831 _make() |
| 979 | 832 { |
| 980 | 833 local mdef makef makef_dir="." makef_inc gcmd cur prev i; |
| 981 | 834 COMPREPLY=(); |
| 982 | 835 cur=${COMP_WORDS[COMP_CWORD]}; |
| 983 | 836 prev=${COMP_WORDS[COMP_CWORD-1]}; |
| 984 | 837 case "$prev" in |
| 985 | 838 -*f) |
| 986 | 839 COMPREPLY=($(compgen -f $cur )); |
| 987 | 840 return 0 |
| 988 | 841 ;; |
| 989 | 842 esac; |
| 990 | 843 case "$cur" in |
| 991 | 844 -*) |
| 992 | 845 COMPREPLY=($(_get_longopts $1 $cur )); |
| 993 | 846 return 0 |
| 994 | 847 ;; |
| 995 | 848 esac; |
| 996 | 849 |
| 997 | 850 # ... make reads |
| 998 | 851 # GNUmakefile, |
| 999 | 852 # then makefile |
| 1000 | 853 # then Makefile ... |
| 1001 | 854 if [ -f ${makef_dir}/GNUmakefile ]; then |
| 1002 | 855 makef=${makef_dir}/GNUmakefile |
| 1003 | 856 elif [ -f ${makef_dir}/makefile ]; then |
| 1004 | 857 makef=${makef_dir}/makefile |
| 1005 | 858 elif [ -f ${makef_dir}/Makefile ]; then |
| 1006 | 859 makef=${makef_dir}/Makefile |
| 1007 | 860 else |
| 1008 | 861 makef=${makef_dir}/*.mk # Local convention. |
| 1009 | 862 fi |
| 1010 | 863 |
| 1011 | 864 |
| 1012 | 865 # Before we scan for targets, see if a Makefile name was |
| 1013 | 866 #+ specified with -f. |
| 1014 | 867 for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do |
| 1015 | 868 if [[ ${COMP_WORDS[i]} == -f ]]; then |
| 1016 | 869 # eval for tilde expansion |
| 1017 | 870 eval makef=${COMP_WORDS[i+1]} |
| 1018 | 871 break |
| 1019 | 872 fi |
| 1020 | 873 done |
| 1021 | 874 [ ! -f $makef ] && return 0 |
| 1022 | 875 |
| 1023 | 876 # Deal with included Makefiles. |
| 1024 | 877 makef_inc=$( grep -E '^-?include' $makef | |
| 1025 | 878 sed -e "s,^.* ,"$makef_dir"/," ) |
| 1026 | 879 for file in $makef_inc; do |
| 1027 | 880 [ -f $file ] && makef="$makef $file" |
| 1028 | 881 done |
| 1029 | 882 |
| 1030 | 883 |
| 1031 | 884 # If we have a partial word to complete, restrict completions |
| 1032 | 885 #+ to matches of that word. |
| 1033 | 886 if [ -n "$cur" ]; then gcmd='grep "^$cur"' ; else gcmd=cat ; fi |
| 1034 | 887 |
| 1035 | 888 COMPREPLY=( $( awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ \ |
| 1036 | 889 {split($1,A,/ /);for(i in A)print A[i]}' \ |
| 1037 | 890 $makef 2>/dev/null | eval $gcmd )) |
| 1038 | 891 |
| 1039 | 892 } |
| 1040 | 893 |
| 1041 | 894 complete -F _make -X '+($*|*.[cho])' make gmake pmake |
| 1042 | 895 |
| 1043 | 896 |
| 1044 | 897 |
| 1045 | 898 |
| 1046 | 899 _killall() |
| 1047 | 900 { |
| 1048 | 901 local cur prev |
| 1049 | 902 COMPREPLY=() |
| 1050 | 903 cur=${COMP_WORDS[COMP_CWORD]} |
| 1051 | 904 |
| 1052 | 905 # Get a list of processes |
| 1053 | 906 #+ (the first sed evaluation |
| 1054 | 907 #+ takes care of swapped out processes, the second |
| 1055 | 908 #+ takes care of getting the basename of the process). |
| 1056 | 909 COMPREPLY=( $( ps -u $USER -o comm | \ |
| 1057 | 910 sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \ |
| 1058 | 911 awk '{if ($0 ~ /^'$cur'/) print $0}' )) |
| 1059 | 912 |
| 1060 | 913 return 0 |
| 1061 | 914 } |
| 1062 | 915 |
| 1063 | 916 complete -F _killall killall killps |
| 1064 | 917 |
| 1065 | 918 |
| 1066 | 919 |
| 1067 | 920 # Local Variables: |
| 1068 | 921 # mode:shell-script |
| 1069 | 922 # sh-shell:bash |
| 1070 | 923 # End:</PRE |
| 1071 | ></TD |
| 1072 | ></TR |
| 1073 | ></TABLE |
| 1074 | ><HR></DIV |
| 1075 | ><P |
| 1076 | >And, here is a snippet from Andrzej Szelachowski's instructive |
| 1077 | <TT |
| 1078 | CLASS="FILENAME" |
| 1079 | >.bash_profile</TT |
| 1080 | > file.</P |
| 1081 | ><DIV |
| 1082 | CLASS="EXAMPLE" |
| 1083 | ><HR><A |
| 1084 | NAME="BASHPROF" |
| 1085 | ></A |
| 1086 | ><P |
| 1087 | ><B |
| 1088 | >Example M-2. <TT |
| 1089 | CLASS="FILENAME" |
| 1090 | >.bash_profile</TT |
| 1091 | > file</B |
| 1092 | ></P |
| 1093 | ><TABLE |
| 1094 | BORDER="0" |
| 1095 | BGCOLOR="#E0E0E0" |
| 1096 | WIDTH="100%" |
| 1097 | ><TR |
| 1098 | ><TD |
| 1099 | ><PRE |
| 1100 | CLASS="PROGRAMLISTING" |
| 1101 | > 1 # From Andrzej Szelachowski's ~/.bash_profile: |
| 1102 | 2 |
| 1103 | 3 |
| 1104 | 4 # Note that a variable may require special treatment |
| 1105 | 5 #+ if it will be exported. |
| 1106 | 6 |
| 1107 | 7 DARKGRAY='\e[1;30m' |
| 1108 | 8 LIGHTRED='\e[1;31m' |
| 1109 | 9 GREEN='\e[32m' |
| 1110 | 10 YELLOW='\e[1;33m' |
| 1111 | 11 LIGHTBLUE='\e[1;34m' |
| 1112 | 12 NC='\e[m' |
| 1113 | 13 |
| 1114 | 14 PCT="\`if [[ \$EUID -eq 0 ]]; then T='$LIGHTRED' ; else T='$LIGHTBLUE'; fi; |
| 1115 | 15 echo \$T \`" |
| 1116 | 16 |
| 1117 | 17 # For "literal" command substitution to be assigned to a variable, |
| 1118 | 18 #+ use escapes and double quotes: |
| 1119 | 19 #+ PCT="\` ... \`" . . . |
| 1120 | 20 # Otherwise, the value of PCT variable is assigned only once, |
| 1121 | 21 #+ when the variable is exported/read from .bash_profile, |
| 1122 | 22 #+ and it will not change afterwards even if the user ID changes. |
| 1123 | 23 |
| 1124 | 24 |
| 1125 | 25 PS1="\n$GREEN[\w] \n$DARKGRAY($PCT\t$DARKGRAY)-($PCT\u$DARKGRAY)-($PCT\! |
| 1126 | 26 $DARKGRAY)$YELLOW-> $NC" |
| 1127 | 27 |
| 1128 | 28 # Escape a variables whose value changes: |
| 1129 | 29 # if [[ \$EUID -eq 0 ]], |
| 1130 | 30 # Otherwise the value of the EUID variable will be assigned only once, |
| 1131 | 31 #+ as above. |
| 1132 | 32 |
| 1133 | 33 # When a variable is assigned, it should be called escaped: |
| 1134 | 34 #+ echo \$T, |
| 1135 | 35 # Otherwise the value of the T variable is taken from the moment the PCT |
| 1136 | 36 #+ variable is exported/read from .bash_profile. |
| 1137 | 37 # So, in this example it would be null. |
| 1138 | 38 |
| 1139 | 39 # When a variable's value contains a semicolon it should be strong quoted: |
| 1140 | 40 # T='$LIGHTRED', |
| 1141 | 41 # Otherwise, the semicolon will be interpreted as a command separator. |
| 1142 | 42 |
| 1143 | 43 |
| 1144 | 44 # Variables PCT and PS1 can be merged into a new PS1 variable: |
| 1145 | 45 |
| 1146 | 46 PS1="\`if [[ \$EUID -eq 0 ]]; then PCT='$LIGHTRED'; |
| 1147 | 47 else PCT='$LIGHTBLUE'; fi; |
| 1148 | 48 echo '\n$GREEN[\w] \n$DARKGRAY('\$PCT'\t$DARKGRAY)-\ |
| 1149 | 49 ('\$PCT'\u$DARKGRAY)-('\$PCT'\!$DARKGRAY)$YELLOW-> $NC'\`" |
| 1150 | 50 |
| 1151 | 51 # The trick is to use strong quoting for parts of old PS1 variable.</PRE |
| 1152 | ></TD |
| 1153 | ></TR |
| 1154 | ></TABLE |
| 1155 | ><HR></DIV |
| 1156 | ></DIV |
| 1157 | ><DIV |
| 1158 | CLASS="NAVFOOTER" |
| 1159 | ><HR |
| 1160 | ALIGN="LEFT" |
| 1161 | WIDTH="100%"><TABLE |
| 1162 | SUMMARY="Footer navigation table" |
| 1163 | WIDTH="100%" |
| 1164 | BORDER="0" |
| 1165 | CELLPADDING="0" |
| 1166 | CELLSPACING="0" |
| 1167 | ><TR |
| 1168 | ><TD |
| 1169 | WIDTH="33%" |
| 1170 | ALIGN="left" |
| 1171 | VALIGN="top" |
| 1172 | ><A |
| 1173 | HREF="histcommands.html" |
| 1174 | ACCESSKEY="P" |
| 1175 | >Prev</A |
| 1176 | ></TD |
| 1177 | ><TD |
| 1178 | WIDTH="34%" |
| 1179 | ALIGN="center" |
| 1180 | VALIGN="top" |
| 1181 | ><A |
| 1182 | HREF="index.html" |
| 1183 | ACCESSKEY="H" |
| 1184 | >Home</A |
| 1185 | ></TD |
| 1186 | ><TD |
| 1187 | WIDTH="33%" |
| 1188 | ALIGN="right" |
| 1189 | VALIGN="top" |
| 1190 | ><A |
| 1191 | HREF="dosbatch.html" |
| 1192 | ACCESSKEY="N" |
| 1193 | >Next</A |
| 1194 | ></TD |
| 1195 | ></TR |
| 1196 | ><TR |
| 1197 | ><TD |
| 1198 | WIDTH="33%" |
| 1199 | ALIGN="left" |
| 1200 | VALIGN="top" |
| 1201 | >History Commands</TD |
| 1202 | ><TD |
| 1203 | WIDTH="34%" |
| 1204 | ALIGN="center" |
| 1205 | VALIGN="top" |
| 1206 | > </TD |
| 1207 | ><TD |
| 1208 | WIDTH="33%" |
| 1209 | ALIGN="right" |
| 1210 | VALIGN="top" |
| 1211 | >Converting DOS Batch Files to Shell Scripts</TD |
| 1212 | ></TR |
| 1213 | ></TABLE |
| 1214 | ></DIV |
| 1215 | ></BODY |
| 1216 | ></HTML |
| 1217 | > |