Suponha que sua matriz seja $errors
, apenas verifique se a contagem de elementos é zero.
if [ ${#errors[@]} -eq 0 ]; then
echo "No errors, hooray"
else
echo "Oops, something went wrong..."
fi
Eu tenho uma matriz que é preenchida com diferentes mensagens de erro enquanto o meu script é executado.
Eu preciso de uma maneira de verificar se não está vazia no final do script e tomar uma ação específica, se for.
Eu já tentei tratá-lo como um VAR normal e usar -z para verificá-lo, mas isso não parece funcionar. Existe uma maneira de verificar se uma matriz está vazia ou não no Bash?
Obrigado
Suponha que sua matriz seja $errors
, apenas verifique se a contagem de elementos é zero.
if [ ${#errors[@]} -eq 0 ]; then
echo "No errors, hooray"
else
echo "Oops, something went wrong..."
fi
Você também pode considerar o array como uma variável simples. Dessa forma, apenas usando
if [ -z "$array" ]; then
echo "Array empty"
else
echo "Array non empty"
fi
ou usando o outro lado
if [ -n "$array" ]; then
echo "Array non empty"
else
echo "Array empty"
fi
O problema com essa solução é que, se uma matriz é declarada assim: array=('' foo)
. Essas verificações informarão a matriz como vazia, embora claramente não seja. (obrigado @musiphil!)
Usar [ -z "$array[@]" ]
claramente não é uma solução. Não especificar colchetes tenta interpretar $array
como uma cadeia ( [@]
é, nesse caso, uma cadeia literal simples) e, portanto, é sempre relatada como falsa: "é a cadeia literal [@]
empty?" Claramente não.
Eu geralmente uso expansão aritmética neste caso:
if (( ${#a[@]} )); then
echo not empty
fi
Eu verifiquei com bash-4.4.0
:
#!/usr/bin/env bash
set -eu
check() {
if [[ ${array[@]} ]]; then
echo not empty
else
echo empty
fi
}
check # empty
array=(a b c d)
check # not empty
array=()
check # empty
e bash-4.1.5
:
#!/usr/bin/env bash
set -eu
check() {
if [[ ${array[@]:+${array[@]}} ]]; then
echo non-empty
else
echo empty
fi
}
check # empty
array=(a b c d)
check # not empty
array=()
check # empty
No último caso, você precisa da seguinte construção:
${array[@]:+${array[@]}}
para não falhar em array vazio ou não definido. Isso é se você faz set -eu
como eu costumo fazer. Isso fornece uma verificação de erros mais rigorosa. De os documentos :
-e
Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists), or a compound command (see Compound Commands) returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command’s return status is being inverted with !. If a compound command other than a subshell returns a non-zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits.
This option applies to the shell environment and each subshell environment separately (see Command Execution Environment), and may cause subshells to exit before executing all the commands in the subshell.
If a compound command or shell function executes in a context where -e is being ignored, none of the commands executed within the compound command or function body will be affected by the -e setting, even if -e is set and a command returns a failure status. If a compound command or shell function sets -e while executing in a context where -e is ignored, that setting will not have any effect until the compound command or the command containing the function call completes.
-u
Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion. An error message will be written to the standard error, and a non-interactive shell will exit.
Se você não precisa disso, sinta-se à vontade para omitir :+${array[@]}
part.
Além disso, observe que é essencial usar o operador [[
aqui, com [
:
$ cat 1.sh
#!/usr/bin/env bash
set -eu
array=(a b c d)
if [ "${array[@]}" ]; then
echo non-empty
else
echo empty
fi
$ ./1.sh
_/1.sh: line 4: [: too many arguments
empty
No meu caso, a segunda resposta não foi suficiente porque pode haver espaços em branco. Eu vim junto com:
if [ "$(echo -ne ${opts} | wc -m)" -eq 0 ]; then
echo "No options"
else
echo "Options found"
fi
Eu prefiro usar colchetes duplos:
if [[ !${array[@]} ]]
then
echo "Array is empty"
else
echo "Array is not empty"
fi
Double Brackets: https://stackoverflow.com/questions/669452/is-preferable-over-in-bash