bashshell

bash - Extract proxy credentials from property


How could I extract the username and password from a properties file, test_properties.sh, shown below:

#!/bin/bash

# Various irrelevant stuff
https_proxy="http://username:password@proxy-host:port"

such that I'd end up with two variables, e.g. proxy_user=username and proxy_pw=password?

Thanks in advance.


Solution

  • Combining read and awk you could do something like this:

    $ https_proxy="$(grep "https_proxy" test_properties.sh | cut -d'=' -f2-)"
    $ read -r proxy_user proxy_password <<<$(awk -F[/@:] '{print $4 " " $5}' <<<"$https_proxy")
    $ printf "%s\n%s\n" $proxy_user $proxy_password
    username
    password