How to check if array has values?
by ddenial from LinuxQuestions.org on (#56VPA)
Hello all,
In my bash script, I want to check if array has values. If it does, I want to pass it to a function.
This is a sample code of that.
Code:#!/bin/bash
declare -a array=( "This is a" "test" "of something" )
if [[ -n "${array}" ]] ; then
echo "Has content"
else
echo "Hmmmm"
fiThe works fine. But if a check with shellcheck, it throws error
Code:$ shellcheck t2.sh
In t2.sh line 5:
if [[ -n "${array}" ]] ; then
^------^ SC2128: Expanding an array without an index only gives the first element.
For more information:
https://www.shellcheck.net/wiki/SC2128 -- Expanding an array without an ind...Visiting the error page https://www.shellcheck.net/wiki/SC2128 doesnt help either. It just says that by using this way I'll only get first value in array. Thats OK, I just want to check if its null.
Is there another way to check if array is null. This should also satisfy shellcheck.
Thanks


In my bash script, I want to check if array has values. If it does, I want to pass it to a function.
This is a sample code of that.
Code:#!/bin/bash
declare -a array=( "This is a" "test" "of something" )
if [[ -n "${array}" ]] ; then
echo "Has content"
else
echo "Hmmmm"
fiThe works fine. But if a check with shellcheck, it throws error
Code:$ shellcheck t2.sh
In t2.sh line 5:
if [[ -n "${array}" ]] ; then
^------^ SC2128: Expanding an array without an index only gives the first element.
For more information:
https://www.shellcheck.net/wiki/SC2128 -- Expanding an array without an ind...Visiting the error page https://www.shellcheck.net/wiki/SC2128 doesnt help either. It just says that by using this way I'll only get first value in array. Thats OK, I just want to check if its null.
Is there another way to check if array is null. This should also satisfy shellcheck.
Thanks