[5157] | 1 | #!/bin/bash |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | #§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ |
---|
| 5 | # |
---|
| 6 | # This template for Bash coding pair with Emacs Lisp package 'bash-font-lock.el' |
---|
| 7 | # to enhance default shell syntax fontification. This package is based on regexs |
---|
| 8 | # to match examples of concsyntax showed below. |
---|
| 9 | # All this is not mandatory and probably not optimized with other correct rules |
---|
| 10 | # Feel free to add your tips, modify it at your convenience or give your feedback |
---|
| 11 | # to improve it. |
---|
| 12 | # |
---|
| 13 | #§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | # To automatically use it with a bash script, copy it to your ~/.emacs.d and |
---|
| 17 | # add '(load "~/.emacs.d/bash-font-lock.el")' in your .emacs configuration file |
---|
| 18 | #-------------------------------------------------------------------------------- |
---|
| 19 | # If a command is not highlighted, add its name to 'bash-builtins' list |
---|
| 20 | # " " function "" " " , " " "" "" 'bash-functions' "" |
---|
| 21 | |
---|
| 22 | # UNIX built-in commands sample |
---|
| 23 | #------------------------------- |
---|
| 24 | .; alias; bg; bind; builtin; caller; compgen; complete; declare; dirs; disown |
---|
| 25 | enable; fc; fg; help; history; jobs; kill; let; local; popd; printf; pushd |
---|
| 26 | shopt; source; suspend; typeset; unalias; eval; export; getopts; newgrp; pwd |
---|
| 27 | read; readonly; times; ulimit; command; hash; test; type; cd; echo; eval; set |
---|
| 28 | shift; umask; unset; wait |
---|
| 29 | |
---|
| 30 | # Common bash built-in commands already added to 'bash-builtins' list |
---|
| 31 | #--------------------------------------------------------------------- |
---|
| 32 | awk basename cat cp cut date diff dirname env find grep head ls make mkdir mv rm |
---|
| 33 | sed sort svn tail tee touch uniq xargs |
---|
| 34 | |
---|
| 35 | # Variables |
---|
| 36 | #----------- |
---|
| 37 | # Specials parameters (see Bash manual for details, `man bash`) |
---|
| 38 | $0 $# $* $@ $? $! $_ $$ $- |
---|
| 39 | # Positionnal parameters (ordered arguments given to run script "$0") |
---|
| 40 | $1 $2 $3 ... |
---|
| 41 | $* == "$1${IFS}$2${IFS}$3..." # Single word , recommended use for string |
---|
| 42 | $@ == "$1" "$2" "$3"... # Separate words, recommended use for array |
---|
| 43 | # Identify locals against GLOBALS or ENVIRONMENT variables with case sensitive |
---|
| 44 | TEMP=${temp_0123}; temp=${TEMP_0123}; export TEMP=${TEMP_0123} |
---|
| 45 | # Possibles variable assignation syntax |
---|
| 46 | temp='temp'; temp=$1 # Simple |
---|
| 47 | length_temp=${#temp} # Length of string |
---|
| 48 | temp=$(( 1 + 1 )) # Integer arithmetic evaluation |
---|
| 49 | temp=${........} # String operations |
---|
| 50 | temp=$(test ....) # Regular syntax |
---|
| 51 | temp=$( test ... ) # Highlight sub-shell '( ... )' & command or function call |
---|
| 52 | temp=`test .....` # Backquotes not recommended to avoid complete highlighting |
---|
| 53 | |
---|
| 54 | # Arrays |
---|
| 55 | #-------- |
---|
| 56 | # Initialisation |
---|
| 57 | declare -a array # Explicit |
---|
| 58 | array=([0]='zero' [1]='one' ...) # Implicit with index assignement |
---|
| 59 | array[0]='zero'; array[9]='ten' # Implicit or add element to array at an index |
---|
| 60 | # Curly brackets are essential to work with arrays |
---|
| 61 | # Last index of an array |
---|
| 62 | IDX=${#temp_0123[@]}; idx=${#TEMP_0123[@]} |
---|
| 63 | # Get last element of an array (${#array[@]}) |
---|
| 64 | LAST_ELMNT=array[${#array[@]}]; last_elmnt=ARRAY[${#ARRAY[@]}] |
---|
| 65 | # Remove an element or entire array |
---|
| 66 | unset array[9]; unset array[@] |
---|
| 67 | |
---|
| 68 | # Strings |
---|
| 69 | #--------- |
---|
| 70 | # Single quotes are recommended to identified entire characters string instead |
---|
| 71 | # of doubles quotes or initialize variable |
---|
| 72 | echo 'The name of the script is '$0' with following arguments '$* |
---|
| 73 | # Doubles quotes should only be used when it's necessary to interpret escaping |
---|
| 74 | # character or to perform parameter substitution |
---|
| 75 | printf "The value of PI is %8.6f.\n" $PI; sed "s/3.1415/$PI/" temp.txt |
---|
| 76 | |
---|
| 77 | # Function |
---|
| 78 | #---------- |
---|
| 79 | # 'function' word is not not mandatory at declaration if you have double |
---|
| 80 | # brackets '()' right after name. A function has to be declared before its call |
---|
| 81 | # so should be placed at the beginning of the main script. A clever solution is |
---|
| 82 | # to gather similar functions in kind of a 'module' file which will be sourced |
---|
| 83 | # from main script. |
---|
| 84 | # 2 possibles syntax: |
---|
| 85 | function fake_func { local temp=''; ...; return ...; } |
---|
| 86 | fake_func() { |
---|
| 87 | local temp='' # Declare variable as local, if not his attribute is global |
---|
| 88 | ... |
---|
| 89 | return ... # Function can only return an integer (stderr by default) |
---|
| 90 | # export result by a global variable to bypass it |
---|
| 91 | } |
---|
| 92 | # Function call (with or without argument) |
---|
| 93 | temp=$( fake_func $1 $2 ); fake_func $1 $2; fake_func |
---|
| 94 | |
---|
| 95 | # Tests operators differs with type of test (arithmetic or string comparison |
---|
| 96 | # for number/characters, file attributs), see manual for test with `man test` |
---|
| 97 | #---------------------------------------------------------------------------- |
---|
| 98 | # Possible syntax : literal 'test' or compact syntax with brackets/parenthesis |
---|
| 99 | # at the ends |
---|
| 100 | # '[ ... ]' & '[[ ... ]]' are almost identical (simple and extended test) |
---|
| 101 | # With ' != ' & ' == ' operators, right string is considered as regex |
---|
| 102 | # "" '~=' " , " "" " "" " extended " |
---|
| 103 | [ $temp ~= "..." ] && [[ ! -e temp.txt ]] || (( $temp >= 0 )) |
---|
| 104 | |
---|
| 105 | # For compound commands, prefer the use of command block '{ ...; }' instead of |
---|
| 106 | # a sub-shell '( ... )', keep in mind that despite sub-shell inherit from its |
---|
| 107 | # run script all variables declared as locals are lost at the end of execution |
---|
| 108 | |
---|
| 109 | # To cut a long sequence, put the escape character '\' at the end of line and |
---|
| 110 | # continue on next line (possible on several lines) |
---|
| 111 | printf "This is a very very very long sentence that I have to cut in order to \ |
---|
| 112 | be less than 80 characters for a line of code but I don't have to call \ |
---|
| 113 | the same command several times.\n " |
---|
| 114 | # A pipe ' | ' cannot be put at the end of a line, even if you have a '\' |
---|
| 115 | cat temp.txt | cut -d' ' -f-5 | sort -kr3n | uniq -c | sort | head -n25 \ |
---|
| 116 | | awk '$3 >= 1024 {print $4}' \ |
---|
| 117 | | xargs -t -i() mv () $HOST@$HOSTNAME:${REP_STORAGE} |
---|
| 118 | |
---|
| 119 | # 'if ...; then ...; fi' |
---|
| 120 | #------------------------ |
---|
| 121 | # Very short syntax with commands block '{ ...; }' |
---|
| 122 | [ ... ] && { ...; ...; } |
---|
| 123 | # Short syntax with commands block '{ ...; }' |
---|
| 124 | [ ... ] && { ...; \ |
---|
| 125 | ...; } |
---|
| 126 | # Regular syntax |
---|
| 127 | if [ ... ]; then |
---|
| 128 | ... |
---|
| 129 | fi |
---|
| 130 | |
---|
| 131 | # 'if ...; then ...; else ...; fi' |
---|
| 132 | #---------------------------------- |
---|
| 133 | # Short syntax with commands block '{ ... }' |
---|
| 134 | { [ ... ] && ...; } || { ...; ...; } |
---|
| 135 | # Regular syntax |
---|
| 136 | if [ ... ]; then |
---|
| 137 | ... |
---|
| 138 | else |
---|
| 139 | ... |
---|
| 140 | fi |
---|
| 141 | |
---|
| 142 | # 'case ... in ...) ... ;; ... esac' |
---|
| 143 | #----------------------------------- |
---|
| 144 | case ... in |
---|
| 145 | # Very short syntax |
---|
| 146 | ...) ...;; ...) ...;; ...) ...;; |
---|
| 147 | # Short syntax |
---|
| 148 | ...) ...; ...; ...;; |
---|
| 149 | # Regular syntax |
---|
| 150 | ...) |
---|
| 151 | ... |
---|
| 152 | ;; |
---|
| 153 | esac |
---|
| 154 | |
---|
| 155 | # List font lock faces with effective highlighting (can be customized) |
---|
| 156 | #--------------------------------------------------------------------- |
---|
| 157 | font-lock-warning-face |
---|
| 158 | # for a construct that is peculiar, or that greatly changes the meaning of |
---|
| 159 | #other text |
---|
| 160 | font-lock-function-name-face |
---|
| 161 | # for the name of a function being defined or declared |
---|
| 162 | font-lock-variable-name-face |
---|
| 163 | # for the name of a variable being defined or declared |
---|
| 164 | font-lock-keyword-face |
---|
| 165 | # for a keyword with special syntactic significance, like ‘for’ and ‘if’ in C. |
---|
| 166 | font-lock-comment-face |
---|
| 167 | # for comments |
---|
| 168 | font-lock-comment-delimiter-face |
---|
| 169 | # for comments delimiters, like ‘/*’ and ‘*/’ in C. On most terminals, this |
---|
| 170 | #inherits from font-lock-comment-face |
---|
| 171 | font-lock-type-face |
---|
| 172 | # for the names of user-defined data types |
---|
| 173 | font-lock-constant-face |
---|
| 174 | # for the names of constants, like ‘NULL’ in C |
---|
| 175 | font-lock-builtin-face |
---|
| 176 | # for the names of built-in functions |
---|
| 177 | font-lock-preprocessor-face |
---|
| 178 | # for preprocessor commands. This inherits, by default, from |
---|
| 179 | #font-lock-builtin-face |
---|
| 180 | font-lock-string-face |
---|
| 181 | # for string constants |
---|
| 182 | font-lock-doc-face |
---|
| 183 | # for documentation strings in the code. This inherits, by default, from |
---|
| 184 | #font-lock-string-face |
---|
| 185 | font-lock-negation-char-face |
---|
| 186 | # for easily-overlooked negation characters |
---|