MST

星途 面试题库

面试题:Bash中curl与wget的基础使用对比

请阐述在Bash脚本里,curl和wget在下载单个文件时各自的常用参数,以及在处理重定向和认证方面的不同之处,并给出简单的示例代码。
25.2万 热度难度
编程语言Bash

知识考点

AI 面试

面试题答案

一键面试

curl常用参数

  • -O:将服务器上的文件下载到本地,并使用服务器上的文件名。
  • -o:指定下载后的本地文件名。

wget常用参数

  • -O:指定下载后的本地文件名。

处理重定向

  • curl:默认会跟随重定向,可使用-L参数强制跟随重定向(一般默认行为就可满足)。示例:
curl -O -L http://example.com/redirected_file
  • wget:默认跟随重定向。示例:
wget -O local_file http://example.com/redirected_file

认证

  • curl:使用-u参数进行基本认证,格式为-u username:password。示例:
curl -u user:pass -O http://example.com/protected_file
  • wget:使用--user--password参数进行认证。示例:
wget --user=user --password=pass -O local_protected_file http://example.com/protected_file