Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IFS read in loop bash

I would like to iterate on a loop, my variable $_INFO on which I loop contains:

tcp/443
tcp/80
udp/54

I tried to parse my variable with this loop

for val in "$_INFO"; do
   IFS=/ read _PROTOCOL _PORT <<< $_INFO
   echo "protocol: $_PROTOCOL"
   echo "port: $_PORT"
done

I receive in output :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

protocol: tcp
port: 443 tcp/80 udp/54

While I would like to receive in output:

protocol: tcp
port: 443
protocol: tcp
port: 80
protocol: udp
port: 54

Any pointers in the right direction would be of help. Thanks

>Solution :

If awk is ok:

$ echo "$_INFO" | awk -F\/ '{printf "protocol: %s\nport: %d\n", $1, $2}'
protocol: tcp
port: 443
protocol: tcp
port: 80
protocol: udp
port: 54
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading