[arch-general] Re: Unzip archive in which are multiple sub zip archiers?

Maarten de Vries maarten at de-vri.es
Fri Apr 17 21:38:19 UTC 2015


On 04/17/2015 10:57 PM, Christian Demsar wrote:
> I'll try to use the globbing. That's new to me.
>
> Is there anything else you saw that was odd? This is the first non-trivial script I've written (recursion in bash!), so I'm not sure about the placement of the functions, etc.

At a second look, this check might also be a problem:

if [ $zipFiles ]; then

To be honest I'm not sure what bash will do with that when $zipFiles is 
an array. The check isn't really needed with an array since looping over 
an empty array won't do anything anyway. When you do need it you can get 
the number of elements in an array in bash with ${#array[@]} and test on 
that. Knowing how bash arrays work can be very useful in writing 
effective bash scripts. If you haven't yet I would recommend reading up 
on their specifics.

In bash (and some other shells), you might also prefer [[ ]] for tests 
over [ ]. It is somewhat less confusing in some situations, but be aware 
that it is not POSIX. Then again, arrays aren't POSIX either. Just be 
sure to use a proper bash shebang and not #!/bin/sh when you're using 
bash features (you already did this, but it's important so I'm 
emphasizing ;) ).

I also saw that you check for read permissions before trying to extract 
the zip file. It doesn't hurt, but it's generally better to just try 
doing what you want to do and detect errors by checking the exit code of 
programs you run. Otherwise you will likely forget to check for some 
uncommon error conditions. In some cases it can even happen that the 
error conditions are not present when you test for them, but they are 
later when you try to extract the file (or do something else with it) 
because something or someone changed something in between.

> Is there a way to declare the functions like in C? I couldn't figure that out from the section on functions in Advanced Bash Scripting.
If you mean first declare a function and define it later: no, you can't 
do that in bash. On the other hand, you can use a function B the body of 
function A before B is defined, as long as you don't call A until B is 
defined. So in general it doesn't really matter. For example, this will 
work fine:

    foo() {
         bar
    }

    bar() {
         echo "Hello world!"
    }

    foo


I hope my comments are useful. If not, feel free to ignore them ;)

Regards,
Maarten



More information about the arch-general mailing list