informatique:linux:bash
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
informatique:linux:bash [2021/07/30 13:37] – relecture partielle pteu | informatique:linux:bash [2022/06/08 13:50] – pteu | ||
---|---|---|---|
Line 104: | Line 104: | ||
Rappel : '' | Rappel : '' | ||
- | =====Debugger un script===== | + | ====-x==== |
- | + | ||
- | Pour vérifier la syntaxe d'un script sans lancer : '' | + | |
Pour le débugger un script, ajouter dans le script : | Pour le débugger un script, ajouter dans le script : | ||
Line 117: | Line 115: | ||
#!/bin/sh -v | #!/bin/sh -v | ||
</ | </ | ||
+ | |||
+ | ====-n==== | ||
+ | |||
+ | Pour vérifier la syntaxe d'un script sans le lancer (// | ||
+ | |||
+ | ====-o pipefail==== | ||
+ | |||
+ | Cette option permet de stopper l' | ||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | #set -eo pipefail | ||
+ | foo | echo test1 | ||
+ | echo test2 | ||
+ | #test1 | ||
+ | #line 3: foo: command not found | ||
+ | #test2 | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | set -eo pipefail | ||
+ | foo | echo test1 | ||
+ | echo test2 | ||
+ | #test1 | ||
+ | #line 3: foo: command not found | ||
+ | </ | ||
+ | |||
+ | ====-u==== | ||
+ | |||
+ | Avec cette option, bash va générer une erreur et stopper son exécution si une variable non déclarée est utilisée. Pour éviter cela, on utilisera la notation '' | ||
=====.bashrc===== | =====.bashrc===== | ||
Line 360: | Line 388: | ||
</ | </ | ||
+ | ====Désactiver l' | ||
+ | Pour afficher si une commande est un alias, on peut faire : '' | ||
+ | <code bash> | ||
+ | alias ls | ||
+ | alias ls='ls --color=auto' | ||
+ | </ | ||
+ | |||
+ | Et si on souhaite la lancer sans l' | ||
+ | <code bash> | ||
+ | ' | ||
+ | # ou | ||
+ | \ls | ||
+ | </ | ||
=====Liens/ | =====Liens/ | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
+ | Je pose ledit template ici, mais le lien ci-dessous vaut le détour car il explique chaque best-practice en détail ! | ||
+ | <code bash> | ||
+ | # | ||
+ | |||
+ | set -Eeuo pipefail | ||
+ | trap cleanup SIGINT SIGTERM ERR EXIT | ||
+ | |||
+ | script_dir=$(cd " | ||
+ | |||
+ | usage() { | ||
+ | cat << EOF # remove the space between << and EOF, this is due to web plugin issue | ||
+ | Usage: $(basename " | ||
+ | |||
+ | Script description here. | ||
+ | |||
+ | Available options: | ||
+ | |||
+ | -h, --help | ||
+ | -v, --verbose | ||
+ | -f, --flag | ||
+ | -p, --param | ||
+ | EOF | ||
+ | exit | ||
+ | } | ||
+ | |||
+ | cleanup() { | ||
+ | trap - SIGINT SIGTERM ERR EXIT | ||
+ | # script cleanup here | ||
+ | } | ||
+ | |||
+ | setup_colors() { | ||
+ | if [[ -t 2 ]] && [[ -z " | ||
+ | NOFORMAT=' | ||
+ | else | ||
+ | NOFORMAT='' | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | msg() { | ||
+ | echo >&2 -e " | ||
+ | } | ||
+ | |||
+ | die() { | ||
+ | local msg=$1 | ||
+ | local code=${2-1} # default exit status 1 | ||
+ | msg " | ||
+ | exit " | ||
+ | } | ||
+ | |||
+ | parse_params() { | ||
+ | # default values of variables set from params | ||
+ | flag=0 | ||
+ | param='' | ||
+ | |||
+ | while :; do | ||
+ | case " | ||
+ | -h | --help) usage ;; | ||
+ | -v | --verbose) set -x ;; | ||
+ | --no-color) NO_COLOR=1 ;; | ||
+ | -f | --flag) flag=1 ;; # example flag | ||
+ | -p | --param) # example named parameter | ||
+ | param=" | ||
+ | shift | ||
+ | ;; | ||
+ | -?*) die " | ||
+ | *) break ;; | ||
+ | esac | ||
+ | shift | ||
+ | done | ||
+ | |||
+ | args=(" | ||
+ | |||
+ | # check required params and arguments | ||
+ | [[ -z " | ||
+ | [[ ${#args[@]} -eq 0 ]] && die " | ||
+ | |||
+ | return 0 | ||
+ | } | ||
+ | |||
+ | parse_params " | ||
+ | setup_colors | ||
+ | |||
+ | # script logic here | ||
+ | |||
+ | msg " | ||
+ | msg "- flag: ${flag}" | ||
+ | msg "- param: ${param}" | ||
+ | msg "- arguments: ${args[*]-}" | ||
+ | </ |
informatique/linux/bash.txt · Last modified: 2023/01/09 10:52 by pteu