1

I'm trying to test if an archive has all its files. All I need to do is a simple unzip and then make. The shell keeps trying to interpret arguments like -aoq as a program. Other errors exist as well, but I'll spare readers each and every way not to do it. Here are some of the failed attempts:

Failed:

RESULT=$(unzip -aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/")
if [[ "$RESULT" -eq "0" ]]; then ... fi;

RESULT=$(unzip (-aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/"))
if [[ "$RESULT" -eq "0" ]]; then ... fi;

RESULT=$(unzip ("-aoq" "cryptopp.zip" "-d" "$TMP/cryptopp-zip/"))
if [[ "$RESULT" -eq "0" ]]; then ... fi;

RESULT=$(unzip "-aoq cryptopp563.zip -d $TMP/cryptopp563-zip/")
if [[ "$RESULT" -eq "0" ]]; then ... fi;

RESULT=$(unzip "{-aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/"}")
if [[ "$RESULT" -eq "0" ]]; then ... fi;

RESULT=$(unzip "(-aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/")")
if [[ "$RESULT" -eq "0" ]]; then
...

If I see one more question and answer that says "just use parenthesis", "just use quotes" or "just use curly braces" I think I am going to scream...

How do I call unzip with arguments so that Bash does not try to interpret the arguments as commands?


Here's a couple of the more comical error messages:

unzip:  cannot find or open {-aoq cryptopp563.zip -d /tmp/cryptopp563-zip/}, {-aoq cryptopp563.zip -d
/tmp/cryptopp563-zip/}.zip or {-aoq cryptopp563.zip -d /tmp/cryptopp563-zip/}.ZIP.


unzip:  cannot find or open (-aoq cryptopp563.zip -d /tmp/cryptopp563-zip/), (-aoq cryptopp563.zip -d
/tmp/cryptopp563-zip/).zip or (-aoq cryptopp563.zip -d /tmp/cryptopp563-zip/).ZIP.

Here are a few questions/answers that did not work for me. I'm fairly certain I have visited U&L.SE, Stack Overflow and Super User multiple times.

closed as off-topic by Julie Pelletier, mdpc, G-Man, Anthon, Eric Renouf Sep 11 '16 at 12:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Julie Pelletier, mdpc, Anthon, Eric Renouf
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Is the text output of unzip really going to be 0, or did you mean to instead inspect $? for the exit code of unzip? – thrig Sep 9 '16 at 18:09
  • @thrig - originally I was trying to call like if [[ ( $(unzip ...) -eq "0") ]]; then ... fi. But I broke it out into two parts when trying to make it work. – user56041 Sep 9 '16 at 18:12
  • Then you broke it wrong as it will compare the command's output. – Julie Pelletier Sep 9 '16 at 18:13
  • What's the problem with your very first attempt? It looks like your best shot. – Julie Pelletier Sep 9 '16 at 18:14
  • 1
    @jww: I just tested your first command (RESULT=$(unzip -aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/")) and it works perfectly. Of course they're is nothing in $RESULT since the command does no output but it does extract the files to the expected location. – Julie Pelletier Sep 9 '16 at 18:21
5

The first one:

RESULT=$(unzip -aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/")

should run unzip just fine, and drop its output to the variable RESULT. However, unzip doesn't print much in its standard output (well, unless with unzip -l), so I think you actually want the return value. Which can be found in $? after the assignment and command substitution, or just after running the program as normal:

unzip -aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/"
if [ "$?" -eq 0 ] ; then echo ok ; fi

(And yes, you could just if unzip ... ; then ....)

You don't really have an array there, though, just a bunch of normal parameters to the command. This would make an array, print its length and pass it as arguments to unzip:

A=(-aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/")
echo ${#A[@]}
unzip "${A[@]}"   # note the quotes
  • Or just if unzip -aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/"; then echo ok; fi – heemayl Sep 9 '16 at 18:25
  • Or even just unzip -aoq cryptopp563.zip -d "$TMP/cryptopp563-zip/" && echo OK as long as we're shortening things :) – terdon Sep 9 '16 at 18:26