1

I need to unzip a file in my nodejs script:

var exec = require('child_process').exec;

var execOptions = { timeout: 15000 };

exec('bash unzip.sh', execOptions, function (error) {
   ...
});

In unzip.sh I do:

set -e
unzip my_file.zip -d /var/tmp/my_dir

The destination directory has some quota limits for its size. And sometimes I got:

write error (disk full?).  Continue? (y/n)

And then after 15 sec the exec() fails with timeout (this is OK).

How can I tell unzip to do not prompt user and fail immediately, to avoid 15 sec timeouts?

I've tried unzip my_file.zip -d /var/tmp/my_dir 2> /dev/null but this does not work.

2

I did not find the option that tells unzip to switch to non-interactive mode, but I found a workaround. I know this looks ugly, but try it:

/bin/echo "n" | /usr/bin/unzip -o $zipfile -d $dir

It will send "n" as a reply to unzip's question.

1
  • Looks like a nice approach. And if you ever need to pass n more than once then yes n | some_command … seems handy. – Kamil Maciorowski Nov 16 '17 at 19:59
2

unzip -qq $file -d $dest works like a charm.

0

I'd recommend to refer to the options that can be set with the unzip command. Therefore type man unzip or unzip --help.

Mostly the -f option forces the operation without prompting you. The -q option reduces output. A mix of these two and perhaps some other options you'll find in the reference I mentioned above can help you get the options right.

1
  • the -f option in unzip is "-f freshen existing files, create none" I think that perhaps the -o option is more suitable "-o overwrite files WITHOUT prompting" – Simon B Oct 10 '17 at 8:45
0

Another solution is to use a closed file handle as the standard in. Unzip will work as always but if it prompts for input it will automatically close.

$ ls
spacefiller  test.zip
$ unzip test.zip
Archive:  test.zip
  inflating: test.bin
test.bin:  write error (disk full?).  Continue? (y/n/^C) ^C$
$ rm test.bin
$ unzip test.zip <&-
Archive:  test.zip
  inflating: test.bin
test.bin:  write error (disk full?).  Continue? (y/n/^C)
warning:  test.bin is probably truncated
$ echo $!
24130

This solution will work for other similar problems as well.

unzip -qq test.zip

As above will work similarly with a similar exit number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.