Recherche caractere

Fermé
yohann190996 - 1 juin 2016 à 14:11
UnGnU Messages postés 1158 Date d'inscription lundi 2 mai 2016 Statut Contributeur Dernière intervention 22 décembre 2020 - 1 juin 2016 à 15:07
Bonjour,

je voudrais savoir dans un script bash si ma variable a en 2ème caractère un B ou l ou O comment je peux faire

Merci
A voir également:

1 réponse

UnGnU Messages postés 1158 Date d'inscription lundi 2 mai 2016 Statut Contributeur Dernière intervention 22 décembre 2020 157
1 juin 2016 à 15:07
Salut,

Une façon de faire avec bash3.2 et supérieur :

$ cat m_test.sh
#! /bin/bash

reg="^.[BIO]"
while :
do
read -p "Entrez un nom : " nom
[[ ${nom} =~ ${reg} ]] && echo "Le second caractère est soit un B, soit un I ou encore un O"
done


$ ./m_test.sh
Entrez un nom : label
Entrez un nom : oBsede
Le second caractère est soit un B, soit un I ou encore un O
Entrez un nom : LILLA
Le second caractère est soit un B, soit un I ou encore un O
Entrez un nom : LiLLA
Entrez un nom : kOlanta
Le second caractère est soit un B, soit un I ou encore un O
Entrez un nom : kolanta
Entrez un nom : ^C

1