Saltar al contenido

Bash comprobar si la cadena no contiene otra cadena

Solución:

Usar !=.

if [[ ${testmystring} != *"c0"* ]];then
    # testmystring does not contain c0
fi

Ver ayuda [[ for more information.

As mainframer said, you can use grep, but i would use exit status for testing, try this:

#!/bin/bash
# Test if anotherstring is contained in teststring
teststring="put you string here"
anotherstring="string"

echo ${teststring} | grep --quiet "${anotherstring}"
# Exit status 0 means anotherstring was found
# Exit status 1 means anotherstring was not found

if [ $? = 1 ]
luego echo "$ otra cadena no fue encontrada" fi

Bash permite que u use = ~ para probar si la subcadena está contenida. Ergo, el uso de negar permitirá probar lo contrario.

fullstring="123asdf123"
substringA=asdf
substringB=gdsaf
# test for contains asdf, gdsaf and for NOT CONTAINS gdsaf 
[[ $fullstring =~ $substring ]] && echo "found substring $substring in $fullstring"
[[ $fullstring =~ $substringB ]] && echo "found substring $substringB in $fullstring" || echo "failed to find"
[[ ! $fullstring =~ $substringB ]] && echo "did not find substring $substringB in $fullstring"
¡Haz clic para puntuar esta entrada!
(Votos: 3 Promedio: 4)


Tags : /

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *