help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Invisible contents of a variable array?


From: Roger
Subject: Re: Invisible contents of a variable array?
Date: Fri, 20 Jan 2023 23:23:35 -0500

> On Fri, Jan 20, 2023 at 09:19:30PM -0500, Greg Wooledge wrote:
>On Fri, Jan 20, 2023 at 08:54:47PM -0500, Roger wrote:
>> I have a list of files and folders assigned to variable _files[] array.
>> 
>> For calculating the total size of a list of files and folders, I am using du 
>> with _files[@], I then get a division by zero warning.
>> 
>> du --bytes --total "${_files[@]}" | tail --lines=1 | cut --fields=1
>> du: invalid zero-length file name
>> 
>> Printing the contents of the variable _files[@] array only obviously prints 
>> visible files/folders.
>
>You're gonna need to show us how you populated the array, and what's
>actually in the array (declare -p _files).
>
>unicorn:~$ du --bytes --total ""
>du: invalid zero-length file name
>0      total
>
>It's pretty obvious you've got an empty string in the array, but without
>showing us how you populated it, we can't help you much.

Bingo!  declare -p was the magic for showing _files[0] as a the null string!

declare -a _files=([0]="" [1]="/home/roger/src" [2]="README.md")


Without pasting a whole mess of code, what I'm doing is using getopts for 
parsing the dash arguments.  Then shifting for parsing the additional specified 
files/folders.

# remove already processed getopts parameters.
shift $((${OPTIND}-1))
declare _file=""
declare -a -g _files=""

# then loop over the additional specified files
i=1
for _file do
    echo "  DEBUG: var FILE ${i}: ${_file}"
    _files[$i]="${_file}"
    i=$((i + 1))
done

When the above is executed, all I see are _files[1] and _files[2] set. So 
likely the initial space bewteen the last arg and the first file specied?

OH! I see, should initial with i=0 rather than i=1.  Solved.

THANKS!

Roger

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]