Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | Prochaine révision Les deux révisions suivantes | ||
linux:bash [12-03-2020 11:47] edmc73 [Faire un prompt] |
linux:bash [08-09-2020 16:28] edmc73 [Exemple de condition] |
||
---|---|---|---|
Ligne 343: | Ligne 343: | ||
</ | </ | ||
+ | Tester le contenu d'une chaine avec du regex | ||
+ | Tester une date yyyymmdd de facon très simple | ||
+ | |||
+ | <code bash> | ||
+ | [[ $date =~ ^[0-9]{8}$ ]] && echo " | ||
+ | </ | ||
+ | |||
+ | ou plus complet | ||
+ | |||
+ | <code bash> | ||
+ | [[ $date =~ ^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$ ]] && echo " | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | --year-- | ||
+ | # | ||
+ | # start of line or 10, | ||
+ | # or 30, 31 | ||
+ | </ | ||
+ | |||
+ | That is, you can define a regex in Bash matching the format you want. This way you can do: | ||
+ | |||
+ | <code bash> | ||
+ | [[ $date =~ ^regex$ ]] && echo " | ||
+ | </ | ||
+ | where commands after && are executed if the test is successful, and commands after || are executed if the test is unsuccessful. | ||
+ | |||
+ | Note this is based on the solution by Aleks-Daniel Jakimenko in User input date format verification in bash. | ||
+ | |||
+ | In other shells you can use grep. If your shell is POSIX compliant, do | ||
+ | |||
+ | <code bash> | ||
+ | (echo " | ||
+ | </ | ||
+ | In fish, which is not POSIX-compliant, | ||
+ | |||
+ | <code bash> | ||
+ | echo " | ||
+ | </ | ||
===== Boucle ===== | ===== Boucle ===== | ||