Recherche caractere

yohann190996 -  
UnGnU Messages postés 1468 Statut Contributeur -
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

1 réponse

  1. UnGnU Messages postés 1468 Statut Contributeur 158
     
    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