Skip to content

Instantly share code, notes, and snippets.

@brianoflan
Last active January 29, 2018 19:25
Show Gist options
  • Save brianoflan/cb793ca76b6279dc11cf1c90f905d01d to your computer and use it in GitHub Desktop.
Save brianoflan/cb793ca76b6279dc11cf1c90f905d01d to your computer and use it in GitHub Desktop.
Get a file from an URL (wget, curl)
#!/bin/bash
wget_curl_file() { # Args: from_URL, to_file. Optional arg: Log.
local log='' ;
log=$3 ;
[[ $log ]] || log=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ;
{ wget -qO $2 $1 2>$log || curl -s $1 > $2 ; } &> $log ;
local e=$?
[[ $e -eq 0 ]] || {
echo "ERROR: $e from wget or curl:" 1>&2
local sets=$-
set -x
tail $log 1>&2
set +x
set $sets
echo " (For more, see '$log'.)" 1>&2
}
return $e
}
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment