bash - 如何让curl等待响应然后继续执行代码

标签 bash ubuntu curl

我有一个简单的代码:

HEADERS_FILE_NAME=headers.txt
FILE=body.xml

echo "start getting"
curl -D $HEADERS_FILE_NAME -o $FILE --fail http://<SOMEURLHERE>:7001/articles?indexingInfoId=4&format=xml 2>/dev/null
echo "end getting"

输出为:

start getting
end getting
root@prod_l04:~#   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  166M    0  166M    0     0  14.5M      0 --:--:--  0:00:11 --:--:-- 34.9M

问题:如何让curl等待收到响应后,然后执行“echo “end getting”

最佳答案

URL 不带引号并包含与号 (&);因此 shell 将该行读取为:

curl -D $HEADERS_FILE_NAME -o $FILE --fail http://:7001/articles?indexingInfoId=4 & format=xml 2>/dev/null

and thus runs curl at the background. Since format=xml 2>/dev/null is a valid command line you're not getting an error from it.

See
Bash Reference Manual § Quoting and,
Bash Reference Manual § Lists
for further information.

And here is a working, safe version of your script with proper quotation:

HEADERS_FILE_NAME='headers.txt'
FILE='body.xml'

echo 'start getting'
curl -D "$HEADERS_FILE_NAME" -o "$FILE" --fail 'http://<SOMEURLHERE>:7001/articles?indexingInfoId=4&format=xml' 2>/dev/null
echo 'end getting'

关于bash - 如何让curl等待响应然后继续执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283516/

相关文章:

ubuntu - 如何等待多个远程进程(通过 ssh)完成?

php - 是否可以使用 CURL 设置 cookie 内容?

java - HttpUrlConnection 身份验证不起作用

bash - 使用 bash 查找包含字符串的第一个文件夹名称

mysql - 使用 Bash 从表中更新用户密码

javascript - 从 Meteor 运行 bash 脚本服务器端时出现问题

bash - &>/dev/null 的 "opposite"

linux - 我应该在关闭主机之前关闭 lxc 容器吗?

ubuntu - (增量)重建 Debian/Ubuntu 软件包

windows - 为什么 Curl Powershell Windows 10 比命令提示符慢?