可执行文件不等待curl完成下载

时间:2019-04-02 18:24:00

标签: bash shell unix command-line

我有一个可执行文件,应该从链接下载一个zip文件并将其解压缩。

#!/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!"

但是,我的脚本在解压缩文件之前不以某种方式等待curl完成下载。我在做什么错了?

输出:

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]

最后,没有文件被下载过,也没有在可执行文件所在的文件夹中解压缩。

编辑2:

在终端中运行curl命令后,

ls -l输出以下内容:

-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输出以下内容:

/bin/bash

P.S。在MacBooks上预装了curl吗?

2 个答案:

答案 0 :(得分:3)

建议对脚本进行以下更改:

#!/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!"

答案 1 :(得分:1)

您可以使用此:

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

如果curl成功执行,将使用uzip文件。