Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable does not wait for curl to finish downloading

I have an executable which is supposed to download a zip file from a link and unzip it.

#!/bin/bash

echo “Downloading project resources from Storage...”

if curl -O -J -L https://mylink.com; then
    unzip filename.zip
else

    echo "Something went wrong"
fi;
echo "Done!"

However, somehow my script does not wait for curl to finish downloading before unzipping the file. What am I doing wrong?

Output:

MacBook-Pro:~ ej$ /Users/ej/Desktop/untitled\ folder\ 2/Setup ; exit;
“Downloading project resources from Storage...”
unzip:  cannot find or open file.zip, file.zip.zip or file.zip.ZIP.
Done!
logout
Saving session...  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

And in the end no file has been downloaded nor unzipped inside the folder the executable has been ran in.

Edit 2:

ls -l outputs the following after I run the curl command in the terminal:

-rwxr--r--@ 1 ej  staff       390  2 Apr 21:42 Setup
-rw-r--r--  1 ej  staff  86368932  2 Apr 22:19 file.zip?alt=media

which bash outputs the following:

/bin/bash

P.S. Is curl preinstalled on MacBooks?

like image 723
John Doe Avatar asked Oct 26 '25 20:10

John Doe


2 Answers

Would suggest the following changes to the script:

#!/usr/bin/env bash

echo "Downloading project resources from Storage..."

filename="outfile.zip"

if curl --silent -o "${PWD}/${filename}" -L "https://mylink.com"; then
    unzip "${filename}"
    if [[ -f "${PWD}/${filename}" ]]; then
        echo "Removing the file.."
        rm -f "${PWD}/${filename}"
    fi
else
    echo "Something went wrong"
fi
echo "Done!"

like image 196
akskap Avatar answered Oct 28 '25 11:10

akskap


you can use this:

curl -O -J -L https://mylink.com && unzip filename.zip

this will uzip file if curl execute successfully.

like image 33
Ali Hallaji Avatar answered Oct 28 '25 09:10

Ali Hallaji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!