Não é necessário codificar bash :
case $string in
(*[![:blank:]]*) echo "string is not blank";;
("") echo "string is empty";;
(*) echo "string is blank"
esac
Meu código abaixo não funciona:
stringZ=" "
if [[ "$stringZ" == ^[[:blank:]][[:blank:]]*$ ]];then
echo string is blank
else
echo string is not blank
fi
Resultado:
string is not blank # wrong
Como posso testar isso?
De man bash :
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)).
Portanto, o operador de correspondência de expressões regulares deve ser =~ :
if [[ "$stringZ" =~ ^[[:blank:]][[:blank:]]*$ ]];then
echo string is blank
else
echo string is not blank
fi
Você pode reduzir a verbosidade da expressão regular usando + quantifier (ou seja, entidade anterior 1 ou mais vezes):
if [[ "$stringZ" =~ ^[[:blank:]]+$ ]]; then
Tags shell test shell-script string