Curl , download file with user:pass in bash script

Hello,
My question is about curl command. (ubuntu14.04)
In terminal, I am able to download my mainfile with:

curl -u user1:pass1 http://11.22.33.44/*******

When I convert it into bash script like this:

#!/bin/bash
cd /root/scripts
computer_ip=11.22.33.44
curl -u $1:$2 http://$computer_ip/****** > mainfile
#curl -u $1:$2 -O mainfile http://$computer_ip/****** #this is also not working

gives nothing. I tried different ways like these:

curl -u '$1':'$2' http://$computer_ip/****** > mainfile
curl -u "$1":"$2" http://$computer_ip/****** > mainfile
curl -u ""$1":"$2"" http://$computer_ip/****** > mainfile

I run the script like:

./script.sh user1 pass1

Could you please tell me what I am missing?
Thanks in advance
Boris

Having you tried running the script with the set -x ? What does that come back with?
What happens if you remove > mainfile ? What do you get?
Have you tried running curl in verbose ( -v ) mode?

Hello Vgersh99,
I do not know how to run with set -x
If I remove > mainfile how may I redirect the output to a file so that I would be able to manage some other tasks at following steps of the script, which were not mentioned in my post ?
curl -v -u $1:$2 ****** is not giving an output.

Kind regards
Boris

#!/bin/bash
set -x 
cd /root/scripts
computer_ip=11.22.33.44
curl -u $1:$2 http://$computer_ip/******
#curl -u $1:$2 -O mainfile http://$computer_ip/****** #this is also not working

Remove > mainfile for debugging purposes - once debugged, you can put it back in.

Thank You Vgersh99,
Here you are:

+ cd /root/scripts
+ computer_ip=****************
+ curl -u : 'http://*******************'
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>401 Unauthorized</TITLE>
</HEAD><BODY>

I run it like ./script.sh user1 pass1

Well... 401 Unauthorized should be a good starting point.
If the script is as I posted with set -x , you should run it as ./script.sh user1 pass1

My typo error, yes I do run it like you posted..

Well... then somehow you either have hidden the user:pass from the set -x aren't output OR somehow user/pass are used for your curl invocation.

1 Like

Thank you Vgersh99,
I have just sorted out with your help :slight_smile:

Many thanks
Boris