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

Christian Demsar vixsomnis at fastmail.com
Mon Apr 20 20:08:52 UTC 2015


On Mon, Apr 20, 2015, at 08:27 AM, Csányi Pál wrote:
> 2015-04-17 23:38 GMT+02:00 Maarten de Vries <maarten at de-vri.es>:
> >
> > 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
> 
> Thank you all for help.
> The script works, it can unzip archives recursively.
> 
> -- 
> Regards from Pal

Thanks for the tips Maarten.

I was thinking of trying to implement a more advanced version of this
script as an open source project, but there already is one.

https://aur.archlinux.org/packages/dtrx/

I tested it on a 7z{ xz{ txt }, tar.gz{ zip{ txt } } } and it worked
pretty much flawlessly, albeit leaving behind the extracted inner
archives (which would be annoying to delete if you had a mess of
different sorts of archives like the test file I used).

In any case, I recommend that over my script if you ever need to do some
intelligent archive extracting again.

-- 
vixsomnis


More information about the arch-general mailing list