Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a minimal headless *nix which does not have any command line utilities for downloading files (e.g. no curl, wget, etc). I only have bash.

How can I download a file?

Ideally, I would like a solution that would work across a wide range of *nix.

share|improve this question

4 Answers 4

up vote 19 down vote accepted

If you have bash 2.04 or above with the /dev/tcp pseudo-device enabled, you can download a file from bash itself.

Paste the following code directly into a bash shell (you don't need to save the code into a file for executing):

function __wget() {
    : ${DEBUG:=0}
    local URL=$1
    local tag="Connection: close"
    local mark=0

    if [ -z "${URL}" ]; then
        printf "Usage: %s \"URL\" [e.g.: %s http://www.google.com/]" \
               "${FUNCNAME[0]}" "${FUNCNAME[0]}"
        return 1;
    fi
    read proto server path <<<$(echo ${URL//// })
    DOC=/${path// //}
    HOST=${server//:*}
    PORT=${server//*:}
    [[ x"${HOST}" == x"${PORT}" ]] && PORT=80
    [[ $DEBUG -eq 1 ]] && echo "HOST=$HOST"
    [[ $DEBUG -eq 1 ]] && echo "PORT=$PORT"
    [[ $DEBUG -eq 1 ]] && echo "DOC =$DOC"

    exec 3<>/dev/tcp/${HOST}/$PORT
    echo -en "GET ${DOC} HTTP/1.1\r\nHost: ${HOST}\r\n${tag}\r\n\r\n" >&3
    while read line; do
        [[ $mark -eq 1 ]] && echo $line
        if [[ "${line}" =~ "${tag}" ]]; then
            mark=1
        fi
    done <&3
    exec 3>&-
}

Then you can execute it as from the shell as follows:

__wget http://example.iana.org/

Source: Moreaki's answer upgrading and installing packages through the cygwin command line?

share|improve this answer
1  
So you answered your own question at the same time as you asked it. That's an interesting time machine you have ;) –  Meer Borg Jul 22 '13 at 7:59
1  
@MeerBorg - when you ask a question, look for the tick box 'answer your own question' - blog.stackoverflow.com/2011/07/… –  SHC Jul 22 '13 at 8:08
    
Vote for your own answer! But can you explain it? –  eccstartup Jul 22 '13 at 9:16
    
@eestartup - I don't think you can vote for your own answer. Can I explain the code? Not yet! But it does work on cygwin. –  SHC Jul 22 '13 at 9:24
1  
Just a note: This won't work with some configurations of Bash. I believe Debian configures this feature out of their distribution of Bash. –  paraxor Jul 22 '13 at 15:57

Use lynx.

It is pretty common for most of Unix/Linux.

lynx -dump http://www.google.com

-dump: dump the first file to stdout and exit

man lynx

Or netcat:

/usr/bin/printf 'GET / \n' | nc www.google.com 80

Or telnet:

(echo 'GET /'; echo ""; sleep 1; ) | telnet www.google.com 80
share|improve this answer
    
The OP has "*nix which does not have any command line utilities for downloading files", so no lynx for sure. –  Celada Jul 25 at 14:06

If you have this package libwww-perl

You can simply use:

/usr/bin/GET
share|improve this answer

If you want to download a file and save its contents, you can use this function:

function __wget() {
  local URL=$2
  local tag="Connection: close"

  if [ -z "${URL}" ]; then
    printf "Usage: %s \"URL\" [e.g.: %s http://www.google.com/]" \
           "${FUNCNAME[0]}" "${FUNCNAME[0]}"
    return 1;
  fi

  read proto server path <<<$(echo ${URL//// })
  DOC=/${path// //}
  HOST=${server//:*}
  PORT=${server//*:}
  [[ x"${HOST}" == x"${PORT}" ]] && PORT=80

  exec 3<>/dev/tcp/${HOST}/$PORT
  echo -en "GET ${DOC} HTTP/1.1\r\nHost: ${HOST}\r\n${tag}\r\n\r\n" >&3
  sed  -e '1,/^.$/d' <&3 > $1
}

then execute it as follows:

__wget google.png https://www.google.com/images/srpr/logo11w.png
share|improve this answer
1  
Your answer is basically identical to the accepted answer from over a year ago. –  Celada Jul 25 at 14:08
    
@Celeda accepted answer outputs the downloaded file into output stream but my answer outputs its content into a file. I just improved that answer! –  hafez Jul 25 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.