Vlad Cenan Vlad Cenan - 1 year ago 110
Bash Question

Get command output into variable

In bash I got all the names from services which are balanced from /etc/haproxy. But now I want to store all these intro a $srv variable in order to continue my healthcheck script.

for filename in /etc/haproxy/*.cfg; do
for ((i=0; i<=0; i++)); do
srv = $(echo $filename | awk -F'[/.]' '{print $4}')
echo $srv

Using echo $filename | awk -F'[/.]' '{print $4}' is showing the correct microservices names like:

Answer Source

You can't have spaces around the = in shell variable assignments. What you wrote is trying to run the srv command, not assign to the srv variable. It should be:

srv=$(echo $filename | awk -F'[/.]' '{print $4}')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download