0

Hi I am writing a auto script in test.sh , attempting to download a file. It works fine when I use all hard code string. But it does not work with variables. Belong are my code example:

#!/bin/bash
USER="admin"
PWD="adminpass"


curl -v -k  -u ${USER}:${PWD} ${NEXUS_URL}/${SP1}/60/${SP1}-60.zip --output ${SP1}-60.zip

Above code not working not able to download my file, but if I put it as :

curl -v -k  -u "admin":"adminpass" ${NEXUS_URL}/${SP1}/60/${SP1}-60.zip 
--output ${SP1}-60.zip

Then it works. So how do I get the variable credential working with this curl command? Thanks

sefirosu
  • 2,558
  • 7
  • 44
  • 69
  • Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jul 25 '18 at 13:45
  • 4
    Don't use `PWD` as a variable name, it is special to the shell and contains the present working directory. In general, prefer lowercase variable names, exactly to reduce the probability of name clashes like this. – Benjamin W. Jul 25 '18 at 14:14

1 Answers1

0

Option 1

The parameter expansion will not include the double quotes. You can use:

#!/bin/bash

USER='"'admin'"'     #single quote, double quote, single quote
PASS='"'adminpass'"'

curl -v -k  -u ${USER}:${PASS} ${NEXUS_URL}/${SP1}/60/${SP1}-60.zip

Option 2

Alternatively, you can create a .netrc file and use curl -n as follows:

Documentation from https://ec.haxx.se/usingcurl-netrc.html

  1. Create .netrc containing the following and place it in your home directory.

    machine http://something.com
    login admin
    password adminpass
    
  2. Run the command

    curl -n -k --output ${SP1}-60.zip
    

    curl will automatically look for the .netrc file. You can also specify the file path with curl --netrc-file <netrc_file_path>

GNUzilla
  • 116
  • 4