0

I have an script which creates a folder named "data". Then it downloads a file using wget and these files (.zip format) are moved from the current directory to the folder "data". After that what I want is to unzip these files. I'm using unzip filename.zip and it works when I use it on the cmd, however I don't know why it's not working in the script.

Here is the script:

#!/bin/bash

mkdir data

wget http://187.191.75.115/gobmx/salud/datos_abiertos/datos_abiertos_covid19.zip && mv datos_abiertos_covid19.zip data && unzip datos_abiertos_covid19.zip
wget http://187.191.75.115/gobmx/salud/datos_abiertos/diccionario_datos_covid19.zip && mv diccionario_datos_covid19.zip data && unzip diccionario_datos_covid19.zip

datos_abiertos_covid19.zip and diccionario_datos_covid19.zip are the files I want to unzip once they are in my folder "data". I would really appreciate if someone can help me. Thanks in advance!

brenda
  • 656
  • 8
  • 24
  • `mv datos_abiertos_covid19.zip data` - you are moving/renaming the file – Jorengarenar Jul 13 '20 at 05:33
  • yep, and if there is no directory named data, `mv` will rename your zip files to data, a work around is to add a trailing slash to the directory, in this case `data/` so mv will error out a message that there is no directory named `data/`, as oppose to just renaming your files to data. – Jetchisel Jul 13 '20 at 05:35
  • @Jetchisel : There likely **is** a directory of this name (see the `mkdir` command in front). – user1934428 Jul 13 '20 at 08:38

2 Answers2

2

It fails because unzip foo.zip assumes foo.zip is in the current directory, but you just moved it to a subdirectory data. Interactively, you probably cd data first and that's why it works.

To make it work in your script, just have your script cd data as well:

#!/bin/bash
mkdir data
cd data || exit 1
wget http://187.191.75.115/gobmx/salud/datos_abiertos/datos_abiertos_covid19.zip && unzip datos_abiertos_covid19.zip

That way, the file is downloaded directly to the data directory so no mv is necessary, and the unzip command works as expected.

that other guy
  • 116,971
  • 11
  • 170
  • 194
0

My approach:

#!/bin/bash

set -e    # Exit if any command fails

mkdir data
pushd ./data >/dev/null

for i in 'datos_abiertos_covid19.zip' 'diccionario_datos_covid19.zip'; do
        # Don't unzip (or exit) if 'wget' fails, don't exit if 'unzip' fails
        wget "http://187.191.75.115/gobmx/salud/datos_abiertos/$i" -O "./$i" || continue
        unzip "./$i" || true
done

popd >/dev/null
  • The file names don't need to be quoted in this case, but I did so anyway, to emphasise you can/should do so if necessary
  • You could of course use variables for the file list, URL, download dir, etc. if you wanted to build a more general script for downloading zip files
  • I know it's marked bash, but worth mentioning: pushd and popd are not defined in POSIX, you can change those to cd ./data and cd .. for more portability. Obviously wget is not POSIX either, but very common (see this thread for interesting info on that topic)
dan
  • 4,846
  • 6
  • 15