Debian Codegraph @

HTML/sample-bashrc.html

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