get_netrc_user () {
awk -v machine="$1" '$2 == machine { print $4 }' netrc
}
get_netrc_user () {
awk -v machine="$1" '$2 == machine { print $6 }' netrc
}
The first function fetches the username/login given a particular machine, and the second one fetches the password.
To use:
username=$( get_netrc_user 'ftp.test.net' )
password=$( get_netrc_pass 'ftp.test.net' )
The get_netrc_pass assumes that the password never contains any whitespace though. If it does, we can change it to be a bit more safe:
get_netrc_user () {
awk -v machine="$1" '$2 == machine { sub(".*password ", "", $0); print }' netrc
}
Now, instead of returning the 6th whitespace-delimited field from the file, we delete everything up to the string password (with a space at the end) and return what remains of the line. This would still fail if the password contains that string obviously.