0

I'm filling up an array from bash like this:

array[0]=$(awk '/Item/' logfile.log | awk '{print $21}' | awk -F'"' '{ print $2 }')

array[1]=$(awk '/Item/' logfile.log | awk '{print $26}' | awk -F'"' '{ print $2 }')

array[2]=$(awk '/Item/' logfile.log | awk '{print $31}' | awk -F'"' '{ print $2 }')

For some I get a value as a digit for others, there is no output so I believe this populates the array elements with a NULL.
I want to find the array elements that contain NULL and fill them with "0"

I have tried a few different things, but I can't seem to find the correct method here.

One thing I tried: if [[ ${array[$i]} ]]; then array[$i]=0;fi

I think I'm missing qoutes or brackets but just can't seem to hit on the correct syntax.

Thanks

1
  • What do you mean by NULL? Bash doesn't have pointers. Do you mean an empty string? Commented Jul 12, 2013 at 17:21

1 Answer 1

1

You just have the condition the wrong way. [[ something ]] checks that something is non-empty. Use [[ -z thing ]] to check if the string is empty instead:

array[0]="foo"
array[1]=""
array[2]="bar"
for i in {0..2}
do
    if [[ -z ${array[$i]} ]]; then array[$i]=0;fi
done
echo "${array[@]}"

This prints foo 0 bar

Sign up to request clarification or add additional context in comments.

1 Comment

yeah it was the -z that did the trick. BRain farts on Friday I guess. Thanks Much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.