hhmx.de

Föderation EN Fr 11.04.2025 22:01:06

Interesting #ShellScripting quandary:

I'm uncertain how shells delimit filename variables internally.

I have a shell (bash) function that loops through a set of PDF files, opening them in zathura in a "suckless" tabbed window (so that the PDFs always open within the same window, so I can just hit q to zip through them one-at-a-time.

Here is my variable declaration:

local files=${*-*.[pP][dD][fF]}

But for simplicity, you could just imagine it to be:

files=*

Then I'm doing a

for f in $files; do

I'm wanting to print a status line for each item viewed, so I have an idea how many more there are to go, so I'm using this:

echo "[[$f]] ($count/$tot)"

And of course, there's a ((count++)) at the beginning of the loop.

But how to get the total ($tot)??

echo $files |wc -l will always result in 1, as does echo "$files" |wc -l

What I ended up doing was just creating a

for f in $files; do ((tot++)); done

One-liner loop before the actual loop, just to get a total.

Is there a better way?

Am I missing something really obvious?

Föderation · Fr 11.04.2025 22:06:33

use arrays.

$ files = ("file1" "file2" "file3")

$ echo ${files[0]}
file1

$ echo ${[@]} # get count
3

Föderation EN Fr 11.04.2025 22:10:01

@khm

Wouldn't that require a for loop to load the array, though?

Or is there an easy way to do something like

typeset -a files=*

(that would actually read it into an array, rather than schlorping it all into ${files[0]})?

Föderation · Fr 11.04.2025 22:55:32

files=(*-*.[pP][dD][fF]) should load the array

glob's gonna glob