Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
| linux:bash:estructurasdecontrol [23/04/2017 04:47] – [comprobación atributos de fichero (Test Operators, Files)] mate | linux:bash:estructurasdecontrol [31/05/2023 02:25] (actual) – [if/else] mate | ||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| = Estructuras de control y bucles | = Estructuras de control y bucles | ||
| ~~NOTOC~~ | ~~NOTOC~~ | ||
| - | == estructuras | ||
| - | * if/ | ||
| - | if condicion | ||
| - | then | ||
| - | comandos | ||
| - | elif condicion | ||
| - | comandos | ||
| - | else | ||
| - | comandos | ||
| - | fi | ||
| - | DIRECTORIO="/ | ||
| - | COMANDO="/ | ||
| - | |||
| - | if $COMANDO then | ||
| - | echo " | ||
| - | else | ||
| - | echo " | ||
| - | fi | ||
| - | </ | ||
| - | * for< | ||
| - | for nombre [in lista] | ||
| - | do | ||
| - |  | ||
| - | done | ||
| - | |||
| - | LISTA=" | ||
| - | for directorio in $LISTA | ||
| - | do | ||
| - | ls $directorio | ||
| - | done | ||
| - | </ | ||
| - | * while< | ||
| - | while condición | ||
| - | do | ||
| - |  | ||
| - | done | ||
| - | |||
| - | NUM=0 | ||
| - | while [ $NUM -gt 10]; do | ||
| - | let NUM=$NUM+1 | ||
| - | done | ||
| - | </ | ||
| - | * while '' | ||
| - | * until< | ||
| - | until condición | ||
| - | do | ||
| - |  | ||
| - | done | ||
| - | |||
| - | NUM=0 | ||
| - | until [ $NUM -gt 10]; do | ||
| - | let NUM=$NUM+1 | ||
| - | done | ||
| - | </ | ||
| - | * case< | ||
| - | case expresion in | ||
| - | case1) | ||
| - |  | ||
| - | case2) | ||
| - |  | ||
| - | ... | ||
| - | esac | ||
| - | |||
| - | for NUM in 0 1 2 3 | ||
| - | do | ||
| - | case $NUM in | ||
| - | 0) echo " | ||
| - | 1) echo " | ||
| - | 2) echo " | ||
| - | 3) echo " | ||
| - | esac | ||
| - | done | ||
| - | </ | ||
| - | * select | ||
| == comparaciones de cadenas alfanuméricas (Test Operators, Binary Comparison) | == comparaciones de cadenas alfanuméricas (Test Operators, Binary Comparison) | ||
| Línia 114: | Línia 40: | ||
| * **-t** fichero -> fichero existe y está asociado a un terminal | * **-t** fichero -> fichero existe y está asociado a un terminal | ||
| * **-N** fichero -> fichero existe y ha sido modificado desde su última lectura | * **-N** fichero -> fichero existe y ha sido modificado desde su última lectura | ||
| + | |||
| * **-r** fichero -> tiene permiso de lectura | * **-r** fichero -> tiene permiso de lectura | ||
| Línia 121: | Línia 48: | ||
| * **-u** fichero -> tiene el SUID | * **-u** fichero -> tiene el SUID | ||
| * **-k** fichero -> tiene el " | * **-k** fichero -> tiene el " | ||
| + | |||
| * **-O** fichero -> eres el OWNER del fichero | * **-O** fichero -> eres el OWNER del fichero | ||
| * **-G** fichero -> el GRUPO del fichero es el mismo que el tuyo | * **-G** fichero -> el GRUPO del fichero es el mismo que el tuyo | ||
| + | |||
| * fichero1 **-nt** fichero2 -> el fichero1 es más reciente que el fichero2 | * fichero1 **-nt** fichero2 -> el fichero1 es más reciente que el fichero2 | ||
| * fichero1 **-ot** fichero2 -> el fichero1 es más antiguio que el fichero2 | * fichero1 **-ot** fichero2 -> el fichero1 es más antiguio que el fichero2 | ||
| * fichero1 **-ef** fichero2 -> el fichero1 y el fichero2 son HARD LINKS al mismo fichero | * fichero1 **-ef** fichero2 -> el fichero1 y el fichero2 son HARD LINKS al mismo fichero | ||
| + | |||
| * '' | * '' | ||
| Línia 138: | Línia 68: | ||
| /vía: [[http:// | /vía: [[http:// | ||
| + | |||
| + | == estructuras | ||
| + | === if/else | ||
| + | [[https:// | ||
| + | {{: | ||
| + | |||
| + | <code bash> | ||
| + | if condicion | ||
| + | then | ||
| + | comandos | ||
| + | elif condicion | ||
| + | comandos | ||
| + | else | ||
| + | comandos | ||
| + | fi</ | ||
| + | <code bash; example> | ||
| + | COMANDO="/ | ||
| + | |||
| + | if $COMANDO then | ||
| + | echo " | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | </ | ||
| + | === if (ternary operator) | ||
| + | <code bash; example> | ||
| + | # a = b==5 ? c : d</ | ||
| + | <code bash; example> | ||
| + | <code bash; example> | ||
| + | === for | ||
| + | <code bash> | ||
| + | for nombre [in lista] | ||
| + | do | ||
| + |  | ||
| + | done | ||
| + | </ | ||
| + | <code bash; example> | ||
| + | LISTA=" | ||
| + | for directorio in $LISTA | ||
| + | do | ||
| + | ls $directorio | ||
| + | done | ||
| + | </ | ||
| + | <code bash; example> | ||
| + | === while | ||
| + | <code bash> | ||
| + | while condición | ||
| + | do | ||
| + |  | ||
| + | done | ||
| + | </ | ||
| + | <code bash; example> | ||
| + | NUM=0 | ||
| + | while [ $NUM -gt 10]; do | ||
| + | let NUM=$NUM+1 | ||
| + | done | ||
| + | </ | ||
| + | === while | ||
| + | <code bash; example> | ||
| + | === until | ||
| + | <code bash> | ||
| + | until condición | ||
| + | do | ||
| + |  | ||
| + | done | ||
| + | </ | ||
| + | <code bash; example> | ||
| + | NUM=0 | ||
| + | until [ $NUM -gt 10]; do | ||
| + | let NUM=$NUM+1 | ||
| + | done | ||
| + | </ | ||
| + | === case | ||
| + | <code bash> | ||
| + | case expresion in | ||
| + | case1) | ||
| + |  | ||
| + | case2) | ||
| + |  | ||
| + | ... | ||
| + | esac | ||
| + | </ | ||
| + | <code bash; example> | ||
| + | for NUM in 0 1 2 3 | ||
| + | do | ||
| + | case $NUM in | ||
| + | 0) echo " | ||
| + | 1) echo " | ||
| + | 2) echo " | ||
| + | 3) echo " | ||
| + | esac | ||
| + | done | ||
| + | </ | ||
| + | * select | ||
| + | * break (y nested break): [[https:// | ||