Vlad Cenan Vlad Cenan - 4 months ago 27
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
done
done


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

Answer

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}')