vog
vog

Reputation: 25677

How can I most portably download via HTTP from a shell script? Can I rely on curl or wget?

I want to write a portable shell script and runs on a variety of Linux systems, perhaps also other Unix systems (*BSD, macOS). That shell script needs to download files.

Which download tool is more to be expected to be available pre-installed by default? Curl or Wget?

Note that I'm not interested in which of both is currently "better" or more popular. Instead, I'm looking for the best long-term solution with as little code as possible.

Upvotes: 0

Views: 264

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 296039

If you need to support a broad range of systems -- from old installations (which are more likely to support wget) to future ones (which are more likely to support curl), you really should support both.

This can be done with a shell function which checks which tool is available:

# usage: retrieve_url url
# results will be written to stdout
retrieve_url() {
  if command -v curl >/dev/null 2>&1; then
    curl --fail "$@"
  elif command -v wget >/dev/null 2>&1; then
    wget -O- "$@"
  else
    echo "ERROR: Unable to find either curl or wget" >&2
    exit 1
  fi
}

Given this general approach, it should be possible to extend it to support other tools as well -- such as falling through to run an embedded Python script running only standard-library code. (Issues that may be encountered in building a Python download script, suitable for embedding, that runs anywhere between Python 2.5 and through future 3.x releases are best asked separately, tagged for the appropriate community).

Upvotes: 1

Related Questions