informatique:linux:bash
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| informatique:linux:bash [2021/07/30 13:37] – relecture partielle pteu | informatique:linux:bash [2023/01/09 10:52] (current) – [-e] pteu | ||
|---|---|---|---|
| Line 89: | Line 89: | ||
| ====-e==== | ====-e==== | ||
| - | **bash** se termine | + | Avec l' |
| - | Cependant | + | |
| - | * soit on gère l' | + | Cependant |
| - | * soit, plus compliqué mais plus pertinent, on créé une fonction " | + | * soit on gère l' |
| + | * soit, plus compliqué mais plus pertinent, on créé une fonction " | ||
| <code bash> | <code bash> | ||
| #!/bin/bash -e | #!/bin/bash -e | ||
| Line 104: | Line 105: | ||
| 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 116: | ||
| #!/bin/sh -v | #!/bin/sh -v | ||
| </ | </ | ||
| + | |||
| + | ====-n==== | ||
| + | |||
| + | Pour vérifier la syntaxe d'un script sans le lancer (// | ||
| + | |||
| + | ====-o pipefail==== | ||
| + | |||
| + | Par défaut, après une suite de pipe ('' | ||
| + | |||
| + | Combiné avec '' | ||
| + | |||
| + | Par exemple: | ||
| + | <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 393: | ||
| </ | </ | ||
| + | ====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.1627652224.txt.gz · Last modified: 2021/07/30 13:37 by pteu