Trying to figure out how to pass results of one function to another (as a variable). Below is the script I am working on:
#!/usr/local/bin/bash
set -x
_creds=/path/access
_pgpw=$(getPW)
getPW(){
masterpw=$(grep -E 'url.*myfooapp.com' -A4 ${_creds}/access.json \
| grep "pwd" \
| awk '{split($0,a,":"); print a[2]}' \
| grep -o '".*"' \
| tr -d '"')
}
runQuery(){
PGPASSWORD="${_pgpw}" \
psql -h myfooapp.com -U masteruser -d dev -p 5439
echo "CLUSTER: ${_cluster}"
}
runQuery
set -x
What I need is PGPASSWORD to get the results from function getPW and its returning empty. I'm not sure if I did this correctly with the variable _pgpw=$(getPW) with trying to call a function. Please advise on best way to accomplish this. Thanks
getPWcreates a global variable namedmasterpw; it doesn't emit any stdout at all. However,_pgpw=$(getPW)is assigning the empty stdout ofgetPWto_pgpw.. so you're ending up with a populated variable namedmasterpw, and an empty variable named_pgpw. Or, rather, that's what you would be ending up with if you put the function definition above the invocation.getPWto be assigned to_pgpw, then take out themasterpw=$(from the front of the function definition's body, and the)from the end, to stop redirecting that content into a FIFO used to assignmasterpw, and thus away from the stdout content available for the command substitution used to assign_pgpw.